$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$一下就可以了。

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<algorithm>
  6. #include<vector>
  7. #include<map>
  8. #include<set>
  9. #include<queue>
  10. #include<stack>
  11. #include<bitset>
  12. #include<iostream>
  13. using namespace std;
  14. typedef long long LL;
  15. const double pi=acos(-1.0),eps=1e-;
  16. void File()
  17. {
  18. freopen("D:\\in.txt","r",stdin);
  19. freopen("D:\\out.txt","w",stdout);
  20. }
  21. template <class T>
  22. inline void read(T &x)
  23. {
  24. char c=getchar(); x=;
  25. while(!isdigit(c)) c=getchar();
  26. while(isdigit(c)) {x=x*+c-''; c=getchar();}
  27. }
  28.  
  29. const int maxn=;
  30. char s[maxn],ans[maxn];
  31. int T,n,m;
  32. int a[maxn],b[maxn];
  33. bool dp[][maxn];
  34.  
  35. int main()
  36. {
  37. scanf("%d",&T);
  38. while(T--)
  39. {
  40. memset(dp,,sizeof dp);
  41. scanf("%d%d",&n,&m);
  42.  
  43. scanf("%s",s);
  44.  
  45. for(int i=;i<=n/;i++) a[i]=s[i-]-'a'+;
  46. for(int i=n/;i<=n-;i++) b[i-n/+]=s[i]-'a'+;
  47.  
  48. dp[n/+][]=;
  49. for(int i=n/;i>=;i--)
  50. {
  51. if(a[i]==b[i])
  52. {
  53. for(int j=;j<=;j++) dp[i][j]=dp[i+][j];
  54. for(int j=;j<=;j++) if(dp[i+][j]==&&j+<=) dp[i][j+]=;
  55. }
  56.  
  57. else
  58. {
  59. for(int j=;j<=;j++)
  60. {
  61. if(dp[i+][j]==)
  62. {
  63. if(j+<=) dp[i][j+]=;
  64. if(j+<=) dp[i][j+]=;
  65. }
  66. }
  67. }
  68. }
  69.  
  70. bool fail=; int z=m;
  71. for(int i=;i<=n/;i++)
  72. {
  73. bool xx=;
  74. for(int j=;j<=;j++)
  75. {
  76. int num=;
  77. if(a[i]!=j) num++; if(b[i]!=j) num++;
  78.  
  79. if(z-num<) continue;
  80. if(dp[i+][z-num])
  81. {
  82. ans[i]=j;
  83. xx=; z=z-num; break;
  84. }
  85. }
  86. if(xx==) fail=;
  87. if(fail==) break;
  88. }
  89.  
  90. if(fail) printf("Impossible\n");
  91. else
  92. {
  93. for(int i=;i<=n/;i++) printf("%c",ans[i]-+'a');
  94. for(int i=;i<=n/;i++) printf("%c",ans[i]-+'a');
  95. printf("\n");
  96. }
  97. }
  98. return ;
  99. }

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. 旅游[SPFA或是最小生成树][简单算法的灵活题]

    旅行 [问题描述] Z 小镇是一个景色宜人的地方,吸引来自各地的观光客来此旅游观光.Z 小镇附近共有N 个景点(编号为1,2,3,…,N),这些景点被M 条道路连接着,所有道路都是双向的,两个景点之间 ...

  2. 2013.4.A

    =-=最近简直忙疯了.....两眼泪汪汪的...一个星期又磨磨蹭蹭的刷了一套 1.java_c 题1 Java vs C++ [问题描述] Java和C++两种语言的辩手都可以相互争论数小时去证明他们 ...

  3. 活动图activity diagram

    活动图activity diagram 系列文章 [UML]UML系列——用例图Use Case [UML]UML系列——用例图中的各种关系(include.extend) [UML]UML系列——类 ...

  4. ODP.NET Managed正式推出

    NET Oracle Developer的福音——ODP.NET Managed正式推出 在.NET平台下开发Oracle应用的小伙伴们肯定都知道一方面做Oracle开发和实施相比SqlServer要 ...

  5. Vnix的Logo设计

    又捣鼓了一下Logo,感觉Ascii Design碉堡了.下面贴出几款Logo以供观赏,欢迎投票. ## ## ## ## #### ## ## ## ## ### ## ## ## ## ## ## ...

  6. nc 简单的使用

    非常强大的网络工具nc netcat 下面自己总结了它的几种常用用法(参考了它的man): 1.聊天 ClientA: nc - ClientB: nc A'sIP 1234 2.数据传输 Clien ...

  7. WinDBG调试.NET程序示例

    WinDBG调试.NET程序示例 好不容易把环境打好了,一定要试试牛刀.我创建了一个极其简单的程序(如下).让我们期待会有好的结果吧,阿门! using System; using System.Co ...

  8. 安装dnvm

    打开powershell,运行:&{$Branch='dev';iex ((new-object net.webclient).DownloadString('https://raw.gith ...

  9. CKEditor4.x部署和配置

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

  10. iOS 开发之Target-action模式

    Target-action:目标-动作模式,它贯穿于iOS开发始终.但是对于初学者来说,还是被这种模式搞得一头雾水. 其实Target-action模式很简单,就是当某个事件发生时,调用那个对象中的那 ...