L - Clairewd’s message
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
思路:
扩展KMP的应用。
题意真是比较难以理解。
一开始给一个转换表,即s[0]是’a’,s[1]是’b’
之后给一个密文加明文。后面的明文可能不完整。
明显的扩展KMP,把密文加明文全部转化为密文。
求其最长公共前缀即可。参考了kuangbin大神的思路。
若extend[i]+i=len且i>=extend[i]
时。这个i即为所求的长度。
代码:
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<map>
#include<algorithm>
using namespace std;
const int MAXN=200000;
int next[MAXN];
int extend[MAXN];
void EKMP(char s[],char t[])//s[]为主串,t[]为模式串
{
int i,j,p,l;
int len=strlen(t);
int len1=strlen(s);
memset(next,0,sizeof(next));
memset(extend,0,sizeof(extend));
next[0]=len;
j=0;
while(j+1<len&&t[j]==t[1+j])j++;
next[1]=j;
int a=1;
for(int i=2;i<len;i++)
{
p=next[a]+a-1;
l=next[i-a];
if(i+l<p+1)next[i]=l;
else
{
j=max(0,p-i+1);
while(i+j<len&&t[i+j]==t[0+j])j++;
next[i]=j;
a=i;
}
}
j=0;
while(j<len1&&j<len&&s[j]==t[j])j++;
extend[0]=j;
a=0;
for(i=1;i<len1;i++)
{
p=extend[a]+a-1;
l=next[i-a];
if(l+i<p+1)next[i]=l;
else
{
j=max(0,p-i+1);
while(i+j<len1&&j<len&&s[i+j]==t[j])j++;
extend[i]=j;
a=i;
}
}
}
char s[MAXN],tab[MAXN],c[MAXN];
map<char,char>map1;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s%s",&tab,&s);
int len=strlen(tab);
int len1=strlen(s);
for(int i=0;i<len;i++)
map1[tab[i]]='a'+i;
for(int i=0;i<len1;i++)
c[i]=map1[s[i]];
c[len1]=0;
EKMP(s,c);
int k;
for(k=0;k<len1;k++)
{
if(k+extend[k]>=len1&&k>=extend[k])
break;
}
for(int i=0;i<k;i++) printf("%c",s[i]);
for(int i=0;i<k;i++) printf("%c",map1[s[i]]);
printf("\n");
}
return 0;
}