kmp hdu 4300
Clairewd’s message
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11284 Accepted Submission(s):
3983
http://acm.hdu.edu.cn/showproblem.php?pid=4300
Problem Description
Clairewd is a member of FBI. After several years
concealing in BUPT, she intercepted some important messages and she was
preparing for sending it to ykwd. They had agreed that each letter of these
messages would be transfered to another one according to a conversion
table.
Unfortunately, GFW(someone's name, not what you just think about) has detected their action. He also got their conversion table by some unknown methods before. Clairewd was so clever and vigilant that when she realized that somebody was monitoring their action, she just stopped transmitting messages.
But GFW knows that Clairewd would always firstly send the ciphertext and then plaintext(Note that they won't overlap each other). But he doesn't know how to separate the text because he has no idea about the whole message. However, he thinks that recovering the shortest possible text is not a hard task for you.
Now GFW will give you the intercepted text and the conversion table. You should help him work out this problem.
Unfortunately, GFW(someone's name, not what you just think about) has detected their action. He also got their conversion table by some unknown methods before. Clairewd was so clever and vigilant that when she realized that somebody was monitoring their action, she just stopped transmitting messages.
But GFW knows that Clairewd would always firstly send the ciphertext and then plaintext(Note that they won't overlap each other). But he doesn't know how to separate the text because he has no idea about the whole message. However, he thinks that recovering the shortest possible text is not a hard task for you.
Now GFW will give you the intercepted text and the conversion table. You should help him work out this problem.
Input
The first line contains only one integer T, which is
the number of test cases.
Each test case contains two lines. The first line of each test case is the conversion table S. S[i] is the ith latin letter's cryptographic letter. The second line is the intercepted text which has n letters that you should recover. It is possible that the text is complete.
T<= 100 ;
n<= 100000;
Each test case contains two lines. The first line of each test case is the conversion table S. S[i] is the ith latin letter's cryptographic letter. The second line is the intercepted text which has n letters that you should recover. It is possible that the text is complete.
Hint
Range
of test data:T<= 100 ;
n<= 100000;
Output
For each test case, output one line contains the
shorest possible complete text.
Sample Input
2
abcdefghijklmnopqrstuvwxyz
abcdab
qwertyuiopasdfghjklzxcvbnm
qwertabcde
Sample Output
abcdabcd
qwertabcde
这道题的意思是,先输入一个转义表,再输入一个字符串,前面是加密了的,后面是是加密的内容,加密的内容可能有缺陷,就是求完整的字符串,
就是
#include<stdio.h>//网上大佬的代码 #include<iostream> #include<string.h> #include<stack> using namespace std; const int maxn = 1e5+5; int Next[maxn]; char str[maxn]; char str2[maxn]; char mo[maxn]; int dp[maxn]; int mm[30]; int nn[maxn]; int n1,n2; void GetNext() { int i=0,j=-1; while(i<n2) { if(j==-1||mo[i]==mo[j]) {++i,++j,Next[i]=j;} else j=Next[j]; } return ; } int ans[maxn]; int main() { int cnt; int t; scanf("%d",&t); while(t--) { scanf("%s%s",str,mo); strcpy(str2,mo);//把str2初始化为mo相同的字符串; n2=strlen(mo); n1=strlen(str); for(int i=0;i<n1;i++)//对转换表进行处理; { mm[i]=str[i]-'a';//将字符串转化为数字数组; nn[str[i]-'a']=i;//记录每个字符的位置; } for( int i=0; i<(n2+1)/2; i++ )//遍历前半段,单数遍历到前半段加一; { mo[i]=nn[mo[i]-'a']+'a';//将前半段的字符串进行转义;十分巧妙; } Next[0]=-1; GetNext(); int ans=Next[n2];// while( ans>min( (n2+1)/2, n2-(n2+1)/2) )//找最小相同前后缀; { ans=Next[ans];//确保ans的值在前半段且在后半段内; } for(int i=0;i<n2-ans;i++)//ans为文本串的长度;n2-ans为密码的长度; printf("%c",str2[i]); for(int i=0;i<n2-ans;i++)//输出密码对应的文本串; printf("%c",nn[str2[i]-'a']+'a'); printf("\n"); } return 0; }
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int maxn=1e5+3; char str1[maxn],str2[maxn],str3[maxn]; int Next[maxn]; int len1,len2; int t; int sub[maxn]; void getNext(){ Next[0]=-1; int k=-1; int j=0; while(j<len2){ if(k==-1||str2[k]==str2[j]){ k++; j++; Next[j]=k; }else{ k=Next[k]; } } return; } int main(){ scanf("%d",&t); while(t--){ scanf("%s%s",str1,str2); strcpy(str3,str2); len1=strlen(str1); len2=strlen(str2); for(int i=0;i<len1;i++){ sub[str1[i]-'a']=i; } for(int i=0;i<(len2+1)/2;i++){ str2[i]=sub[str2[i]-'a']+'a'; } getNext(); int ans=Next[len2]; // printf("%d\n",ans); while(ans>min((len2+1)/2,len2-(len2+1)/2)){ ans=Next[ans]; } for(int i=0;i<len2-ans;i++){ printf("%c",str3[i]); } for(int i=0;i<len2-ans;i++){ printf("%c",sub[str3[i]-'a']+'a'); } printf("\n"); } return 0; }