Problem Description
A string is called a square string if it can be obtained by concatenating two copies of the same string. For example, "abab", "aa" are square strings, while "aaa", "abba" are not.

Hamming distance between two strings of equal length is the number of positions at which the corresponding symbols are different.

Peter has a string s=s1s2...sn of even length. He wants to find a lexicographically smallest square string t=t1t2...tn that the hamming distance between s and t is exact m. In addition, both s and t should consist only of lowercase English letters.

 
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first contains two integers n and m (1≤n≤1000,0≤m≤n,n is even) -- the length of the string and the hamming distance. The second line contains the string s.

 
Output
For each test case, if there is no such square string, output "Impossible" (without the quotes). Otherwise, output the lexicographically smallest square string.

题意:给你一个长度为n的s串,一个数m,现在让你构造一个长度也为n的t串,使这个串是由两个相同的串拼起来的,并且和s串对应的位不同的数量为m。

而且使t的字典序尽量小。

一开始会想到贪心,但是显然单纯的贪心无法得到正确结果。于是要利用到dp

dp[i][count]表示取到i位有count个位置不一样是否成立。dp[i][count]+=dp[i+1][count-gg](gg表示要变的位置数)

这题要分几种情况

(1)s[i]==s[i+n/2],这个时候要么不变位置要么就变2次。

(2)s[i]!=s[i+n/2],这是要么变2次要么变1次。

由于要求字典序要最小,所以尽量要从首位开始变。于是递推要从n/2开始,这样才能保证前面的操作能使后续操纵成立。

#include <iostream>
#include <cstring>
using namespace std;
char s[1010];
int dp[1010][1010];
int main()
{
int t;
cin >> t;
while(t--) {
int n , m;
cin >> n >> m;
cin >> (s + 1);
int count = m;
memset(dp , 0 , sizeof(dp));
dp[n / 2 + 1][0] = 1;
for(int i = n / 2 ; i >= 1 ; i--) {
if(s[i] == s[i + n / 2]) {
for(int j = 0 ; j <= m ; j++) {
dp[i][j] += dp[i + 1][j];
}
for(int j = 2 ; j <= m ; j++) {
dp[i][j] += dp[i + 1][j - 2];
}
}
else {
for(int j = 1 ; j <= m ; j++) {
dp[i][j] += dp[i + 1][j - 1];
}
for(int j = 2 ; j <= m ; j++) {
dp[i][j] += dp[i + 1][j - 2];
}
}
}
if(!dp[1][m]) {
cout << "Impossible" << endl;
continue;
}
for(int i = 1 ; i <= n / 2 ; i++) {
for(int j = 0 ; j < 26 ; j++) {
int temp = (s[i] != j + 'a') + (s[i + n / 2] != j + 'a');
if(dp[i + 1][m - temp]) {
s[i] = s[i + n / 2] = j + 'a';
m -= temp;
break;
}
}
}
cout << s + 1 << endl;
}
return 0;
}

hdu 5903 Square Distance(dp)的更多相关文章

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

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

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

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

  3. HDU 5903 Square Distance

    $dp$预处理,贪心. 因为$t$串前半部分和后半部分是一样的,所以只要构造前一半就可以了. 因为要求字典序最小,所以肯定是从第一位开始贪心选择,$a,b,c,d,...z$,一个一个尝试过去,如果发 ...

  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 1398 Square Coins(DP)

    Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  6. [HDU5903]Square Distance(DP)

    题意:给一个字符串t ,求与这个序列刚好有m个位置字符不同的由两个相同的串拼接起来的字符串 s,要求字典序最小的答案. 分析:按照贪心的想法,肯定在前面让字母尽量小,尽可能的填a,但问题是不知道前面填 ...

  7. HDU 1011 树形背包(DP) Starship Troopers

    题目链接:  HDU 1011 树形背包(DP) Starship Troopers 题意:  地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...

  8. hdu 2296 aC自动机+dp(得到价值最大的字符串)

    Ring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  9. HDU 4778 状压DP

    一看就是状压,由于是类似博弈的游戏.游戏里的两人都是绝对聪明,那么先手的选择是能够确定最终局面的. 实际上是枚举最终局面情况,0代表是被Bob拿走的,1为Alice拿走的,当时Alice拿走且满足变换 ...

随机推荐

  1. AI and Robot

    Have you ever seen a movie called "Ex Machina"?  I like this movie very much. Artificial i ...

  2. dart的基本语法(一)

    Hello world ​ 安装dart的环境就不赘述了,无脑安装就可以了,安装过程中好像需要梯子(vpn),我装的时候失败好多次,我的梯子不能用了,准备不装了的时候,莫名其妙的装好了.迷の操作.惯例 ...

  3. -bash: redis: command not found

    在linux中安装redis,先是拉过去安装,然后通过命令:make   进行编译  编译完成以后通过命令 make install 完成安装:结果在进行启动linux的时候执行           ...

  4. 状压DP概念 及例题(洛谷 P1896 互不侵犯)

    状压DP 就是状态压缩DP.所谓状态压缩,就是将一些复杂的状态压缩起来,一般来说是压缩为一个二进制数,用01来表示某一元素的状态. 比如一排灯泡(5个) 我们可以用一串二进制01串来表示他们的状态 1 ...

  5. 对平底锅和垃圾的O奖论文的整理和学习[1](2018-02-08发布于知乎)

    今天和杉杉同志在Pacific Coffee坐了0.4天,目前两人都处于放空状态. 这种天气有暖气真的太棒了. 我今天看的论文是这两篇: MCM2013B题O奖论文MCM2016B题O奖论文 先说第一 ...

  6. 三角函数与JavaScript

     1. 三角函数 sin&(求对边与斜边的比值) cos&(邻边与斜边的比值) tan&(对边与邻边的比值) 2.JavaScript的函数的使用 Math.sin() Mat ...

  7. 自然语言处理(NLP)的一般处理流程!

    1. 什么是NLP 自然语言处理 (Natural Language Processing) 是人工智能(AI)的一个子领域.自然语言处理是研究在人与人交互中以及在人与计算机交互中的语言问题的一门学科 ...

  8. list 列表常用方法

    append(self, p_object)                    在列表末端追加一个新元素 insert(self, index, p_object)             在某个 ...

  9. 写论文的第一天 hadoop环境搭建

    毕设日志_____2019.1.23 实验集群环境搭建 三台阿里云服务器 公网ip 内网ip 120.79.63.130   node2 172.17.17.58 112.74.50.240   主节 ...

  10. 对已经存在的hbase表修改压缩方式

    业务上可能会遇到这种情况,在最初创建hbase表时候,未指定压缩方式,当数据导入之后,由rowkey带来的数据膨胀导致hdfs上的数据大小远远大于原始数据大小.所以这时候可能就不得不考虑使用压缩,但是 ...