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. poj 1205 :Water Treatment Plants (DP+高精度)

    题意:有n个城市,它们由一个污水处理系统连接着,每个城市可以选择 1.将左边城市过来的污水和右边城市过来的污水连同本身的污水排到河里  >V< 2.将左边来的污水连同自己的污水排到右边   ...

  2. 10个常用的linux的命令

    以下就是今天我们要介绍的Linux命令:  man  touch, cat and less  sort and grep  cut  sed  tar  find  diff  uniq  chmo ...

  3. PPT | Docker定义存储-让应用无痛运行

    编者注: 本文为9月27日晚上8点有容云平台存储架构师张朝潞在腾讯课堂中演讲的PPT,本次课堂为有容云主办的线上直播Docker Live时代●Online Meetup-第三期:Docker定义存储 ...

  4. jdk8与jdk7中hashMap的resize分析

    在分析代码之前,我们先抛出下面的问题: hashmap 扩容时每个 entry 需要再计算一次 hash 吗? 我们首先看看jdk7中的hashmap的resize实现 1 void resize(i ...

  5. 最全面的改造Zuul网关为Spring Cloud Gateway(包含Zuul核心实现和Spring Cloud Gateway核心实现)

    前言: 最近开发了Zuul网关的实现和Spring Cloud Gateway实现,对比Spring Cloud Gateway发现后者性能好支持场景也丰富.在高并发或者复杂的分布式下,后者限流和自定 ...

  6. kylin Retrieving hive dependency...

    由于公司环境配置hive默认连接hiveserver2 ,不管hive cli 还是beeline cli都默认使用beeline cli,连接hive需要输入账号密码; 启动kylin 时会Retr ...

  7. C语言数组排序——冒泡排序、选择排序、插入排序

    一.冒泡排序 原理解析:(以从小到大排序为例)在一排数字中,将第一个与第二个比较大小,如果后面的数比前面的小,则交换他们的位置. 然后比较第二.第三个……直到比较第n-1个和第n个,此时,每一次比较都 ...

  8. 5、数组的复制(test2.java、test3.java)

    对于数组的复制,在最开始的时候最容易犯的一个错误,那就是自己认为的申请一个数组,然后将已存在的数组赋值到新申请数组名上,这样是错误的,这样仅仅是将数组的地址复制了过去,并不是,将数组内的元素拷贝过去, ...

  9. 理解MySQL(二)--数据库事务

    1.事务:事务内的语句,要么全部执行成功,要么全部执行失败. a)      数据库事务四要素:ACID,原子性,一致性,隔离性,持久性. b)      原子性:一个事务必须被视为不可分割的最小单元 ...

  10. linux 配置ssh无密码登录不起作用的解决方案

    1.安装ssh 直接 sudo apt-get install openssh-server 2.查看ssh运行状态 ps -e | grep ssh 如果发现 sshd 和 ssh-agent 即表 ...