HDU 5903 (DP)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1e3+;
int dp[maxn][maxn];
char s1[maxn];
char p[maxn];
int main()
{
int T;cin>>T;
while(T--)
{
int n,m;
scanf("%d %d",&n,&m);
scanf("%s",s1); //dp[i][j],i表示当前位置,j表示当前花费是否合法。
memset(dp,,sizeof(dp));
dp[n/][] = ;
for(int i=n/;i>=;i--)
{
if(s1[i]==s1[i+n/])
{
for(int j=m;j>=;j--)
{
if(dp[i+][j]) dp[i][j] = ; //不改
}
for(int j=m;j>=;j--)
{
if(dp[i+][j-]) dp[i][j] = ; //改两次
}
}
else
{
for(int j=m;j>=;j--)
{
if(dp[i+][j-]) dp[i][j] = ; //改一次
}
for(int j=m;j>=;j--)
{
if(dp[i+][j-]) dp[i][j] = ; //改两次
}
}
}
if(!dp[][m])
{
printf("Impossible\n");
continue;
}
for(int i=;i<n/;i++)
{
for(int j=;j<;j++)
{
int count1 = ;
char now = j+'a';
if(now!=s1[i]) count1++;
if(now!=s1[i+n/]) count1++;
if(dp[i+][m-count1])
{
p[i] = j+'a';
p[i+n/] = j+'a';
m = m-count1;
break;
}
}
}
p[n] = '\0';
printf("%s\n",p);
}
return ;
}
HDU 5903 (DP)的更多相关文章
- hdu 3016 dp+线段树
Man Down Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- HDU 5928 DP 凸包graham
给出点集,和不大于L长的绳子,问能包裹住的最多点数. 考虑每个点都作为左下角的起点跑一遍极角序求凸包,求的过程中用DP记录当前以j为当前末端为结束的的最小长度,其中一维作为背包的是凸包内侧点的数量.也 ...
- HDU 5903 - Square Distance [ DP ] ( BestCoder Round #87 1002 )
题意: 给一个字符串t ,求与这个序列刚好有m个位置字符不同的由两个相同的串拼接起来的字符串 s, 要求字典序最小的答案 分析: 把字符串折半,分成0 - n/2-1 和 n/2 - n-1 d ...
- HDU 5903 Square Distance (贪心+DP)
题意:一个字符串被称为square当且仅当它可以由两个相同的串连接而成. 例如, "abab", "aa"是square, 而"aaa", ...
- hdu 5903 Square Distance(dp)
Problem Description A string is called a square string if it can be obtained by concatenating two co ...
- HDU 1069 dp最长递增子序列
B - Monkey and Banana Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I6 ...
- HDU 1160 DP最长子序列
G - FatMouse's Speed Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- hdu 4826(dp + 记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4826 思路:dp[x][y][d]表示从方向到达点(x,y)所能得到的最大值,然后就是记忆化了. #i ...
- HDU 2861 (DP+打表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2861 题目大意:n个位置,m个人,分成k段,统计分法.S(n)=∑nk=0CknFibonacci(k ...
随机推荐
- NOTE07152246 JAVA 发展及JDK配置
一.软件工程师体系: 操作系统(Windows/Linux/Unix) - 数据库系统 - 中间件(WebSphere/Tomcat) - JAVA EE 1.操作系统为用户构建了一个平台.此平台上可 ...
- css3动画 9步
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 简析TCP的三次握手与四次分手【转】
转自 简析TCP的三次握手与四次分手 | 果冻想http://www.jellythink.com/archives/705 TCP是什么? 具体的关于TCP是什么,我不打算详细的说了:当你看到这篇文 ...
- Python字符串连接方式
python中有很多字符串连接方式,总结一下: 1 最原始的字符串连接方式:str1 + str22 python 新字符串连接语法:str1, str23 奇怪的字符串方式:str1 str24 % ...
- perl版 Webshell存活检测
原理: 检测url返回状态即可 源码: #!c:\\perl\\bin\\perl.exe use warnings; use strict; use LWP::UserAgent; $| = ; p ...
- json处理三部曲之第二曲:利用Jackson处理json
利用Jackson处理json需要导入的jar包(2以上版本的): <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.c ...
- nmon 安装
安装: mkdir /usr/local/nmon cd /usr/local/nmon wget http://sourceforge.net/projects/nmon/files/nmon_li ...
- OC-之NSDate、万年历
万年历计算星期-基姆拉尔森公式 #include /* * 基姆拉尔森计算公式* W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7*/int week(int ...
- HDU 5813 Elegant Construction
构造.从a[i]最小的开始放置,例如放置了a[p],那么还未放置的,还需要建边的那个点 需求量-1,然后把边连起来. #pragma comment(linker, "/STACK:1024 ...
- hud 2099
#include <stdio.h> #include <stdlib.h> int main() { int m,n,i,flag; ) { flag=; && ...