$dp$预处理,贪心。

因为$t$串前半部分和后半部分是一样的,所以只要构造前一半就可以了。

因为要求字典序最小,所以肯定是从第一位开始贪心选择,$a,b,c,d,...z$,一个一个尝试过去,如果发现某字符可行,那么该位就选择该字符。

第$i$位选择字符$X$可行的条件:

记这一位选择字符$X$的情况下,对$dis$的贡献为$Q$,$1$至$i-1$位对$dis$贡献和为$F$;

如果第$i+1$位至第$\frac{n}{2}$位,对$dis$的贡献可以凑出$m-Q-F$,那么该位选择$X$可行。

所以可以记$dp[i][j]$表示,第$i$位至第$\frac{n}{2}$位,$dis$为$j$是否可以被凑出,倒着$dp$一下就可以了。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<bitset>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c=getchar(); x=;
while(!isdigit(c)) c=getchar();
while(isdigit(c)) {x=x*+c-''; c=getchar();}
} const int maxn=;
char s[maxn],ans[maxn];
int T,n,m;
int a[maxn],b[maxn];
bool dp[][maxn]; int main()
{
scanf("%d",&T);
while(T--)
{
memset(dp,,sizeof dp);
scanf("%d%d",&n,&m); scanf("%s",s); for(int i=;i<=n/;i++) a[i]=s[i-]-'a'+;
for(int i=n/;i<=n-;i++) b[i-n/+]=s[i]-'a'+; dp[n/+][]=;
for(int i=n/;i>=;i--)
{
if(a[i]==b[i])
{
for(int j=;j<=;j++) dp[i][j]=dp[i+][j];
for(int j=;j<=;j++) if(dp[i+][j]==&&j+<=) dp[i][j+]=;
} else
{
for(int j=;j<=;j++)
{
if(dp[i+][j]==)
{
if(j+<=) dp[i][j+]=;
if(j+<=) dp[i][j+]=;
}
}
}
} bool fail=; int z=m;
for(int i=;i<=n/;i++)
{
bool xx=;
for(int j=;j<=;j++)
{
int num=;
if(a[i]!=j) num++; if(b[i]!=j) num++; if(z-num<) continue;
if(dp[i+][z-num])
{
ans[i]=j;
xx=; z=z-num; break;
}
}
if(xx==) fail=;
if(fail==) break;
} if(fail) printf("Impossible\n");
else
{
for(int i=;i<=n/;i++) printf("%c",ans[i]-+'a');
for(int i=;i<=n/;i++) printf("%c",ans[i]-+'a');
printf("\n");
}
}
return ;
}

HDU 5903 Square Distance的更多相关文章

  1. HDU 5903 Square Distance (贪心+DP)

    题意:一个字符串被称为square当且仅当它可以由两个相同的串连接而成. 例如, "abab", "aa"是square, 而"aaa", ...

  2. hdu 5903 Square Distance(dp)

    Problem Description A string is called a square string if it can be obtained by concatenating two co ...

  3. HDU 5903 - Square Distance [ DP ] ( BestCoder Round #87 1002 )

    题意: 给一个字符串t ,求与这个序列刚好有m个位置字符不同的由两个相同的串拼接起来的字符串 s, 要求字典序最小的答案    分析: 把字符串折半,分成0 - n/2-1 和 n/2 - n-1 d ...

  4. BestCoder Round #87 1002 Square Distance[DP 打印方案]

    Square Distance  Accepts: 73  Submissions: 598  Time Limit: 4000/2000 MS (Java/Others)  Memory Limit ...

  5. hdu 4712 Hamming Distance(随机函数暴力)

    http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...

  6. Chi Square Distance

    The chi squared distance d(x,y) is, as you already know, a distance between two histograms x=[x_1,.. ...

  7. hdu 5079 Square

    http://acm.hdu.edu.cn/showproblem.php?pid=5079 题意: n*n网格,每个格子可以涂黑色或白色,有的格子必须涂黑色 问最大白色正方形边长分别为0,1,2,… ...

  8. hdu 1398 Square Coins 分钱币问题

    Square Coins Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...

  9. 题解报告:hdu 1398 Square Coins(母函数或dp)

    Problem Description People in Silverland use square coins. Not only they have square shapes but also ...

随机推荐

  1. cocos2d(CCSprite 用贝塞尔做抛物线,足球精灵并且同时做旋转放大效果)

    今天刚学到Cocos2d中的动作哪一张,自己做了一个用贝塞尔曲线足球精灵实现同时放大旋转和抛物线动作. 使用 [CCSpawn actions:,,]链接这几个动作,同时做.与CCSequence(一 ...

  2. BroadcastReceiver基础总结

    BroadcastReceiver基础总结 BroadcastReceiver是Android四大组件之一,主要负责接收系统或其他程序发出的广播,在开发中,通常用做事件驱动的起源,比如开机就要开启一个 ...

  3. 用TableView做的新闻客户端展示页面

    用TableView做的新闻客户端展示页面 //  MyTableViewImageCell.m //  SildToDo // //  Created by WildCat on 13-8-18. ...

  4. Reeder Web版

    Reeder Web版 访Reeder界面效果 一直很欣赏触控手势的代码实现,所以最近折腾了个Javascript触控手势库--JTouch,效果还有诸多不完善之处,苦于硬件设备不完善,针对ie10的 ...

  5. 超级强悍的PHP代码编辑器PHPstorm及配置

    如何下载安装 官方网站:http://www.jetbrains.com/phpstorm/,本篇文章展示的是5.0版本以,所以,如果你的软件版本过高,可能可有误,所以,如果有问题,请在本站留言,做为 ...

  6. ASP.NET之旅—再一次与ASP谋面

    ASP对我来说已经不是新名词了,因为在一年以前就开始了ASP的学习,那时候虽然效果很不好,但是对ASP还是有了一些最基础的了解.ASP.NET是在ASP基础上增加了一些.NET的一些特性,基础的东西还 ...

  7. android 源码编译 问题 列表

    转自:http://www.cnblogs.com/xilinch/archive/2013/04/02/2996359.html make: *** [out/host/linux-x86/obj/ ...

  8. java Integer 源码学习

    转载自http://www.hollischuang.com/archives/1058 Integer 类在对象中包装了一个基本类型 int 的值.Integer 类型的对象包含一个 int 类型的 ...

  9. bios中断向量表

  10. java单词表01

    OO:object-oriented,面向对象OOP:object-orientedprogramming面向对象编程 JDK:Javadevelopmentkit,java开发工具包JVM:java ...