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.

题意:有一个字母映射的表作为加密表,有一段密文和一段明文连接成一个字符串,现在给出这个串的整个密文和部分明文,要恢复出最短可能的密文+明文。

用扩展KMP将串与本身依靠加密表进行处理,然后求出匹配的最长明文后缀,但是要注意,匹配长度必须小于等于串长。

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 
 6 const int maxn=1e5+5;
 7 
 8 char ct[27];
 9 char s[maxn],tmp[maxn];
10 int ext[maxn],nxt[maxn];
11 
12 void EKMP(char s[],char t[],int lens,int lent){
13     int i,j,p,l,k;
14     nxt[0]=lent;j=0;
15     while(j+1<lent&&t[j]==t[j+1])j++;
16     nxt[1]=j;
17     k=1;
18     for(i=2;i<lent;i++){
19         p=nxt[k]+k-1;
20         l=nxt[i-k];
21         if(i+l<p+1)nxt[i]=l;
22         else{
23             j=max(0,p-i+1);
24             while(i+j<lent&&t[i+j]==t[j])j++;
25             nxt[i]=j;
26             k=i;
27         }
28     }
29 
30     j=0;
31     while(j<lens&&j<lent&&s[j]==t[j])j++;
32     ext[0]=j;k=0;
33     for(i=1;i<lens;i++){
34         p=ext[k]+k-1;
35         l=nxt[i-k];
36         if(l+i<p+1)ext[i]=l;
37         else{
38             j=max(0,p-i+1);
39             while(i+j<lens&&j<lent&&s[i+j]==t[j])j++;
40             ext[i]=j;
41             k=i;
42         }
43     }
44 }
45 
46 int main(){
47     int T;
48     scanf("%d",&T);
49     while(T--){
50         scanf("%s%s",ct,s);
51         int len=strlen(s);
52         for(int i=0;i<len;++i)tmp[i]=ct[s[i]-'a'];
53         EKMP(tmp,s,len,len);
54         int i;
55         for(i=(len+1)/2;i<len;++i){
56             if(ext[i]==len-i){
57                 break;
58             }
59         }
60         i--;
61         for(int j=0;j<=i;++j)putchar(s[j]);
62         for(int j=0;j<=i;++j){
63             for(int k=0;k<26;++k){
64                 if(ct[k]==s[j]){
65                     putchar('a'+k);
66                     break;
67                 }
68             }
69         }
70         printf("\n");
71     }
72     return 0;
73 }
View Code