$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. 使用MMS(MongoDB Monitoring Service)监控MongoDB

    使用MMS(MongoDB Monitoring Service)监控MongoDB 一.MongoDB简介: MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可 ...

  2. [Usaco2008 Feb]Line连线游戏[暴力][水题]

    Description Farmer John最近发明了一个游戏,来考验自命不凡的贝茜.游戏开始的时 候,FJ会给贝茜一块画着N (2 <= N <= 200)个不重合的点的木板,其中第i ...

  3. linux下sshd_config的StrictModes参数

    今天在两台机器A和B上设置免密码登录,A机是Centos6.5,B机是Centos7,我想通过A机免密码登录到B机,在两台机器上设置好公钥和私钥后,在A机上通过ssh连接B机,每次都是要让我手动输入密 ...

  4. SET IDENTITY_INSERT详解

    声明:本博文摘自http://www.lmwlove.com/ac/ID500 自增列默认是不能插入显式值的,当我们试图给自增列插入值时,会报以下错误:当 IDENTITY_INSERT 设置为 OF ...

  5. Xilinx-Zynq Linux内核源码编译过程

    本文内容依据http://www.wiki.xilinx.com网址编写,编译所用操作系统为ubuntu 14 1.交叉编译环境的安装配置 1)http://www.wiki.xilinx.com/I ...

  6. jQuery Colorbox是一款弹出层

    jQuery Colorbox使用教程 jQuery Colorbox是一款弹出层,内容播放插件,效果极佳,最关键的是大小只有10KB,当然我主要是用来弹出图片啦,(之前介绍过jquery Fancy ...

  7. CKEditor4.x部署和配置

    CKEditor4.x && JSP 官网下载CKEditor,可选Basic, Standard, Full 解压放置其WebRoot下 JSP中引入以下文件: <script ...

  8. sqlserver 注释提取工具

    小程序大智慧,sqlserver 注释提取工具 开篇背景 我习惯在写表的创建脚本时将注释直接写在脚本里,比如 ? /*账套*/ CREATE TABLE [dbo].[AccountingBook] ...

  9. 淘宝ued - 前端智勇大闯关(第三季)答案(更新)

    淘宝ued - 前端智勇大闯关(第三季)答案(更新) 下午在微博上看到了淘宝智勇大闯关第三季的信息,感觉挺有意思的,于是就尝试做了下.附上题目地址: http://ued.campus.alibaba ...

  10. 1.C#基础学习笔记3---C#字符串(转义符和内存存储无关)

    技术qq交流群:JavaDream:251572072  教程下载,在线交流:创梦IT社区:www.credream.com ------------------------------------- ...