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.
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;
2 abcdefghijklmnopqrstuvwxyz abcdab qwertyuiopasdfghjklzxcvbnm qwertabcde
abcdabcd qwertabcde
【题解】
前一段是转换表,第二行是密文和明文序列,各占多少不知道,要求你输出最短的铭+密文表。
分析: 因为串2由密文与明文构成,而密文是完整的,所以密文长度最小为整个串2长度的一半,所以从串2的中间位置开始枚举,直到找到第一个明文,这个过程就是:因为明文都是由密文转换过来的,所以把枚举的每个字符按照转换表转换成密文,把转换结果与密文第一个字符比较,如果不相等,说明此字符还是密文部分,继续枚举,直到某个字符转换为密文后与密文首字符相等,则这个字符就是明文的第一个字符,然后以此字符为后缀起点,向字符结尾枚举,如果这个过程中有以第k个字符为起点的序列最后到串尾都还是与密文相对应,则一串字符就是密文对应的明文。秩序补齐即可。
【AC代码】
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e5+10;
int m;
char s1[N],s2[30];
int num[N];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s%s",s2,s1);
int len1=strlen(s1);
int i;
if(len1&1) i=(len1>>1)+1; //注意写法加括号
else i=len1>>1;
for(int i=0;i<26;++i) //记录
{
num[s2[i]-'a']=i;
}
int k=0;
for(;i<len1;++i)
{
if(s2[s1[i]-'a']!=s1[k]) //现在假设s1串的后一半是明文,那么从后一半的第一个元素开始,把它转换成密文
//如果和串首元素不一样,,就说明此元素是密文,继续向后寻找,直到出现转化成密文等于串首元素的下标i,
//则表示从串首到i都是密文段
continue;
else//密文段完了就是明文
{
k++;
bool tag=0;
for(int j=i+1;j<len1;++j)//从下一个元素开始找明文(明文序列转换成密文序列应该是和密文一样的)
{
if(s2[s1[j]-'a']!=s1[k++]){//出现不一样的 就跳出 说明此段不是满足最短序列的明文
tag=1;break;
}
}
if(!tag) break; //直到找到符合条件的明文
}
}
for(int j=0;j<i;++j) //先输出那一段密文
printf("%c",s1[j]);
for(int j=0;j<i;++j) //接着是明文段 明文段就是前面的密文段转换成明文段输出的结果
printf("%c",num[s1[j]-'a']+'a');
printf("\n");
}
return 0;
}