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

hamming distance是对应位置上字符不同的位数. 给定一行字符串和 m,输出字典序最小的字符串。

析:首先先用dp判断能不能形成这样的字符串,然后再打印出来,dp[i][j] 表示 i - 中间的数能不能改 j 个字符得到,最后打印时贪心,从最小的开始,如果能用结束。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e3 + 5;
const LL mod = 10000000000007;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int dp[maxn/2][maxn];
char s[maxn], ans[maxn]; int main(){
int T; cin >> T;
while(T--){
scanf("%d %d", &n, &m);
scanf("%s", s+1);
memset(dp, false, sizeof dp); int mid = n/2;
dp[mid+1][0] = true;
for(int i = mid; i >= 1; --i){
if(s[i] == s[n-mid+i]){
for(int j = 0; j <= m; ++j){
if(j > 1) dp[i][j] |= dp[i+1][j-2];
dp[i][j] |= dp[i+1][j];
}
}
else{
for(int j = 0; j <= m; ++j){
if(j) dp[i][j] |= dp[i+1][j-1];
if(j > 1) dp[i][j] |= dp[i+1][j-2];
}
}
} if(!dp[1][m]) puts("Impossible");
else{
int cnt = m;
for(int i = 1; i <= mid; ++i){
for(int j = 0; j < 26; ++j){
if(s[i] == 'a'+j || s[i+mid] == 'a'+j){
if(s[i] == s[i+mid]){
if(dp[i+1][cnt]) { ans[i] = ans[i+mid] = s[i]; break; }
}
else{
if(dp[i+1][cnt-1]){ ans[i] = ans[i+mid] = 'a' + j; --cnt; break; }
}
}
else{
if(dp[i+1][cnt-2]){ ans[i] = ans[i+mid] = 'a' + j; cnt -= 2; break; }
}
}
}
ans[n+1] = 0;
puts(ans+1);
}
}
return 0;
}

HDU 5903 Square Distance (贪心+DP)的更多相关文章

  1. hdu 5903 Square Distance(dp)

    Problem Description A string is called a square string if it can be obtained by concatenating two co ...

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

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

  3. HDU 5903 Square Distance

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

  4. hdu 1398 Square Coins(简单dp)

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

  5. HDU 4362 Dragon Ball 贪心DP

    Dragon Ball Problem Description   Sean has got a Treasure map which shows when and where the dragon ...

  6. HDU 1051 Wooden Sticks 贪心||DP

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  7. BestCoder Round #87 1002 Square Distance[DP 打印方案]

    Square Distance  Accepts: 73  Submissions: 598  Time Limit: 4000/2000 MS (Java/Others)  Memory Limit ...

  8. hdu 1257 最少拦截系统【贪心 || DP——LIS】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1257 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  9. 【BZOJ-3174】拯救小矮人 贪心 + DP

    3174: [Tjoi2013]拯救小矮人 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 686  Solved: 357[Submit][Status ...

随机推荐

  1. linux 常见名词及命令(二)

    pwd 用于显示当前的工作目录. cd 用于切换工作路径 cd - 切换到上一次的目录 cd ~ 切换到家目录 cd ~username 切换到其他用户的家目录 cd .. 切换到上级目录 ls 用于 ...

  2. 从零开始写STL—哈希表

    static const int _stl_num_primes = 28; template<typename T, typename Hash = xhash<T>> cl ...

  3. 系统性能不够原因可能是cpu不够,内存不够等等

    1.Linux系统可以通过top命令查看系统的CPU.内存.运行时间.交换分区.执行的线程等信息. 通过top命令可以有效的发现系统的缺陷出在哪里.是内存不够.CPU处理能力不够.IO读写过高. 2. ...

  4. Android 四大组件学习之Service五

    本节学习IntentService, 可能就有人问了. 什么是IntentService, IntentService有什么作用? 不是已经有了Service,那为什么还要引入IntentServic ...

  5. [MAT]使用MAT比較多个heap dump文件

    使用MAT比較多个heap dump文件 调试内存泄露时,有时候适时比較2个或多个heap dump文件是非常实用的.这时须要生成多个单独的HPROF文件. 以下是一些关于怎样在MAT里比較多个hea ...

  6. ASP.NET MVC 学习笔记-7.自定义配置信息 ASP.NET MVC 学习笔记-6.异步控制器 ASP.NET MVC 学习笔记-5.Controller与View的数据传递 ASP.NET MVC 学习笔记-4.ASP.NET MVC中Ajax的应用 ASP.NET MVC 学习笔记-3.面向对象设计原则

    ASP.NET MVC 学习笔记-7.自定义配置信息   ASP.NET程序中的web.config文件中,在appSettings这个配置节中能够保存一些配置,比如, 1 <appSettin ...

  7. C++11,控制台输出的一段小程序。

    #include <iostream> // std::cout, std::boolalpha, std::noboolalpha int main () { bool b = true ...

  8. 嵌入式开发之命令行---linux下的find文件查找命令与grep文件内容查找命令

    在使用linux时,经常需要进行文件查找.其中查找的命令主要有find和grep.两个命令是有区的. 区别:(1)find命令是根据文件的属性进行查找,如文件名,文件大小,所有者,所属组,是否为空,访 ...

  9. JAVA学习(七):方法重载与方法重写、thiskeyword和superkeyword

    方法重载与方法重写.thiskeyword和superkeyword 1.方法重载 重载可以使具有同样名称但不同数目和类型參数的类传递给方法. 注: 一是重载方法的參数列表必须与被重载的方法不同,而且 ...

  10. vim怎么把一个写的代码文件另存到任意文件夹里?

    比如你要保存到以下路径: D:\my_project\project001\ 那么有两个方法: 1. 直接保存 2. w D:\my_project\project001\xxx.xxx 3. 变更当 ...