Clairewd’s message
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2296 Accepted Submission(s): 901
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.
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
Author
BUPT
Source
2012 Multi-University Training Contest 1
Recommend
zhuyuanchen520
---------------------
_(:3」∠)_ 其实我不会扩展KMP
---------------------
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MM=111111;
int next[MM],extand[MM];
char P[MM],T[MM];
void GetNext(const char *T)
{
int len=strlen(T),a=0;
next[0]=len;
while(a<len-1 && T[a]==T[a+1]) a++;
next[1]=a;
a=1;
for(int k=2; k<len; k++)
{
int p=a+next[a]-1,L=next[k-a];
if( (k-1)+L >= p)
{
int j = (p-k+1)>0 ? (p-k+1) : 0;
while(k+j<len && T[k+j]==T[j]) j++;
next[k]=j;
a=k;
}
else
next[k]=L;
}
}
void GetExtand(const char *S,const char *T)
{
GetNext(T);
int slen=strlen(S),tlen=strlen(T),a=0;
int MinLen = slen < tlen ? slen : tlen;
while(a<MinLen && S[a]==T[a]) a++;
extand[0]=a;
a=0;
for(int k=1; k<slen; k++)
{
int p=a+extand[a]-1, L=next[k-a];
if( (k-1)+L >= p)
{
int j= (p-k+1) > 0 ? (p-k+1) : 0;
while(k+j<slen && j<tlen && S[k+j]==T[j]) j++;
extand[k]=j;
a=k;
}
else
extand[k]=L;
}
}
int main()
{
int lex;
char cg[30];
char eng[30];
scanf("%d",&lex);
while (lex--)
{
scanf("%s",cg);
scanf("%s",T);
for (int i=0; i<26; i++)
{
eng[cg[i]-'a']=i+'a';
}
int len=strlen(T);
for (int i=0; i<len; i++)
{
P[i]=eng[T[i]-'a'];
}
//puts(T);
//puts(P);
GetExtand(T,P);
int ret=len;
for (int i=(len+1)/2;i<len;i++)
{
if (extand[i]+i>=len)
{
ret=i;
break;
}
}
for (int i=0;i<ret;i++) printf("%c",T[i]);
for (int i=0;i<ret;i++) printf("%c",P[i]);
puts("");
}
return 0;
}