Time Limit:1000MS Memory Limit:32768KB
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.
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.
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
题意: 给你两串字符串,第一个是密文书,第一个字母代表a,第二个字母代表b,以此类推,下面的密文就是按照这个来翻译的,然后第二个字符串由密文和明文组成,然而并不知道这两者的长度,我们要做的就是把完整的,且是最短的密文+明文给输出出来。
思路: 既然第二个字符串是密文+明文,那么密文就一定是完整的,所以只要将密文+明文全部按照密文书来翻译一遍,变成明文+密文,然后再和密文+明文匹配,就可以知道密文的长度了,然后只要按照这个长度分别输出密文+明文就行了。
注意: 第二个字符串是密文+明文,设长度为len,那么密文的长度至少是(len+1)/2,注意,是(len+1)/2!!!!!,我一开始只是除以二,然后拼命WA,改成这个之后就A了,被自己蠢哭了······
#include<stdio.h> #include<iostream> #include<string.h> #include<math.h> #include<algorithm> #include<vector> #include<string> #include<queue> #include<map> #include<stack> #include<set> #define ll long long #define maxn 100010 const int mod=1e4+7; using namespace std; char nut[maxn]; char str[maxn]; char s[maxn]; int f[maxn]; int T,n; char cnt[30]; char ans[maxn]; void makenext(char *a) { memset(f,0,sizeof(f)); int len=strlen(a); int i=0; int j=-1; f[0]=-1; while(i<len) { if(j==-1||a[i]==a[j]) { i++; j++; f[i]=j; } else j=f[j]; } // for(int i=0;i<len;i++) cout<<f[i]<<" "; cout<<endl; } int makecmp(char *a,char *b) { makenext(b); int lena=strlen(a); //nut int lenb=strlen(b); //s int i=(lena+1)/2; //要加一啊!!!!! int j=0; while(i<lena) { if(j==-1||a[i]==b[j]) { i++; j++; } else j=f[j]; if(j>=lenb) break; } return j; } int main() { while(scanf("%d",&T)!=EOF) { while(T--) { scanf("%s",str); int lens=strlen(str); int k=97; for(int i=0;i<lens;i++) cnt[str[i]-'a']=k++; scanf("%s",nut); int lena=strlen(nut); int len=0; if(lena==1) len=1; else { for(int i=0;i<lena;i++) s[i]=cnt[nut[i]-'a']; len=lena-makecmp(nut,s); } for(int i=0;i<len*2;i++) { if(i<lena) printf("%c",nut[i]); else printf("%c",cnt[nut[i-len]-'a']); } printf("\n"); } } return 0; }