HDU 5903 Square Distance
2016-09-26 20:41
323 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5903
转载链接:http://www.cnblogs.com/bin-gege/p/5906270.html
题意:
给你一个字符串,要你求出一个square串(由2个相同的t组成的字符串 T=t+t形式),使得汉明距离dis==m,至于字符串的汉明距离 为相同位置下不同的字符个数.
个人感想:
这道题我尼玛,我敲破脑袋也没想出来是dp,就算我知道是dp,但是也是第一次对这种dp那么陌生,
我一开始就是被那个恰好为m,卡住了,我在想dp怎么构造敲好为m,我以为在value上存值,,- -后来我发现我智障了,应该+1维度控制为m.
因为有一半的字符串是一样的我们折半来考虑即可.
其实莫非就4种状态
1.s[i]==s[i+half]
我们当前选的字符和他们不一样,产生+2的距离
我们当前选的字符和他们一样,产生+0的距离
2.s[i]!=s[i+half]
我们当前选的字符和他们不一样,产生+2的距离
我们当前选的字符和他们某个一样,产生+1的距离
那么久好办了,通过dp,状态转移就可以知道这个T能不能构造了。
我们得反面dp,因为这样我们才能求最小的字典序T.
因为正面dp的话,我想了一下,其实也是对的..但是呢,莫非就2种选择。
但是你仔细想想,可能如果从前面再进行选择的话,那么就可能出错.
例如:
10 8
acdaf taaod
答案是aabbaaabba.
但是我正写的是aaaaaaaaaa
可能在选择上要特别特别注意那些转移咯,因为转载可能是这个字符串不选而出来的结果,如果不细心处理,一样得出的答案为错的..
代码:
转载链接:http://www.cnblogs.com/bin-gege/p/5906270.html
题意:
给你一个字符串,要你求出一个square串(由2个相同的t组成的字符串 T=t+t形式),使得汉明距离dis==m,至于字符串的汉明距离 为相同位置下不同的字符个数.
个人感想:
这道题我尼玛,我敲破脑袋也没想出来是dp,就算我知道是dp,但是也是第一次对这种dp那么陌生,
我一开始就是被那个恰好为m,卡住了,我在想dp怎么构造敲好为m,我以为在value上存值,,- -后来我发现我智障了,应该+1维度控制为m.
因为有一半的字符串是一样的我们折半来考虑即可.
其实莫非就4种状态
1.s[i]==s[i+half]
我们当前选的字符和他们不一样,产生+2的距离
我们当前选的字符和他们一样,产生+0的距离
2.s[i]!=s[i+half]
我们当前选的字符和他们不一样,产生+2的距离
我们当前选的字符和他们某个一样,产生+1的距离
那么久好办了,通过dp,状态转移就可以知道这个T能不能构造了。
我们得反面dp,因为这样我们才能求最小的字典序T.
因为正面dp的话,我想了一下,其实也是对的..但是呢,莫非就2种选择。
但是你仔细想想,可能如果从前面再进行选择的话,那么就可能出错.
例如:
10 8
acdaf taaod
答案是aabbaaabba.
但是我正写的是aaaaaaaaaa
可能在选择上要特别特别注意那些转移咯,因为转载可能是这个字符串不选而出来的结果,如果不细心处理,一样得出的答案为错的..
代码:
/* Author:GavinjouElephant * Title: * Number: * main meanning: * * * */ #include <iostream> using namespace std; #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include <sstream> #include <cctype> #include <vector> #include <set> #include <cstdlib> #include <map> #include <queue> //#include<initializer_list> //#include <windows.h> //#include <fstream> //#include <conio.h> #define MaxN 0x7fffffff #define MinN -0x7fffffff #define lson 2*k #define rson 2*k+1 typedef long long ll; const int INF=0x3f3f3f3f; const int maxn=1005; int Scan()//读入整数外挂. { int res = 0, ch, flag = 0; if((ch = getchar()) == '-') //判断正负 flag = 1; else if(ch >= '0' && ch <= '9') //得到完整的数 res = ch - '0'; while((ch = getchar()) >= '0' && ch <= '9' ) res = res * 10 + ch - '0'; return flag ? -res : res; } void Out(int a) //输出外挂 { if(a>9) Out(a/10); putchar(a%10+'0'); } bool dp[maxn][maxn]; char s[maxn]; int T,n,m; void solve() { int ha=n/2; memset(dp,false,sizeof(dp)); dp[ha+1][0]=true; for(int i=ha;i>=1;i--) { if(s[i]==s[i+ha]) { for(int j=0;j<=m;j++)dp[i][j]|=dp[i+1][j];//不换那个字符,贡献+0; for(int j=2;j<=m;j++)dp[i][j]|=dp[i+1][j-2];//换了这个字符,贡献+2; } else { for(int j=1;j<=m;j++)dp[i][j]|=dp[i+1][j-1];//和某个字符一样,贡献+1; for(int j=2;j<=m;j++)dp[i][j]|=dp[i+1][j-2];//和2个字符都不一样,贡献+2; } } if(!dp[1][m]) { printf("Impossible\n"); return; } string S; int now=m; for(int i=1;i<=ha;i++) { for(int j=0;j<=25;j++) { int v=(s[i]!=(j+'a'))+(s[i+ha]!=(j+'a'));//判断每一个单词. if((now-v)>=0&&dp[i+1][now-v]) { now-=v; S+=(j+'a'); break; } } } cout<<S<<S<<endl; } int main() { #ifndef ONLINE_JUDGE freopen("coco.txt","r",stdin); freopen("lala.txt","w",stdout); #endif scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); scanf("%s",s+1); solve(); } return 0; }
相关文章推荐
- hdu_5903_Square Distance(dp)
- HDU 5903 - Square Distance [ DP ] ( BestCoder Round #87 1002 )
- HDU 5903 Square Distance (贪心+dp)
- HDU 5903 Square Distance (DP)
- HDU 5903 Square Distance (贪心+DP)
- HDU 5903 Square Distance
- HDU 5903 - Square Distance (贪心+dp)
- hdu 5903 Square Distance
- HDU 5904(Square Distance)
- hdoj 5903 Square Distance 【贪心 + 数学】
- hdoj-5903 Square Distance
- hdu 题目1518 Square (DFS)
- HDU 1518 Square
- hdu 1516 String Distance and Transform Process(编辑距离+记录路径)
- HDU 4394 Digital Square【搜索】
- HDU 1518 Square(DFS)
- Square hdu 1518
- Square Coins hdu 1398
- hdu 1518 Square
- hdu 1518 Square