$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. asp.net 加入验证码

    验证码生成页面代码(清理掉没用的html) using System; using System.Collections.Generic; using System.Linq; using Syste ...

  2. 大数据时代,我们为什么使用hadoop

    大数据时代,我们为什么使用hadoop 我们先来看看大数据时代, 什么叫大数据,“大”,说的并不仅是数据的“多”!不能用数据到了多少TB ,多少PB 来说. 对于大数据,可以用四个词来表示:大量,多样 ...

  3. 一种最坏情况线性运行时间的选择算法 - The missing worst-case linear-time Select algorithm in CLRS.

    一种最坏情况线性运行时间的选择算法 - The missing worst-case linear-time Select algorithm in CLRS. 选择算法也就是求一个无序数组中第K大( ...

  4. myeclipse 配置 resin-pro-4.0.34

    热部署: 在 resin.xml 文件下 增加 <host id="" root-directory="."> <!-- webapps ca ...

  5. CLR执行模型与常见的几个概念

    CLR执行模型与常见的几个概念 一.CLR执行模型 1.1从代码到程序集 CLR执行的模型是从代码开始,经过编译器生成托管模块,默认情况下可以让多个托管模块和资源生成程序集.下图为其大致过程. 1.2 ...

  6. Python闭包详解

    Python闭包详解 1 快速预览 以下是一段简单的闭包代码示例: def foo(): m=3 n=5 def bar(): a=4 return m+n+a return bar >> ...

  7. WCFRESTFul服务搭建及实现增删改查

    WCFRESTFul服务搭建及实现增删改查 RESTful Wcf是一种基于Http协议的服务架构风格,  RESTful 的服务通常是架构层面上的考虑. 因为它天生就具有很好的跨平台跨语言的集成能力 ...

  8. web register/validation/login system flowchart

    I spent several days on building a system about this. And make it work well with serveral thousand l ...

  9. easyUI datagrid 动态绑定列名称

    easyUI 基于Jquery ,所以需要引用Jquery文件 easyUI自带了很多样式文件,可以根据需要,引用相应的css文件. 其中datagrid是一个根据json数据,js前端生成前端显示的 ...

  10. java applet初探之计算器

    这里是用java写的一个计算器,用appllet的方式在浏览器中运行,如果电脑上装有java运行环境jre就能一试.将html代码保存为*.html(名称能够自定),applettest编译为clas ...