Clairewd’s message HDU - 4300
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
题意:给你一个两个字符串,第一个字符串长度为26,为a~z加密后的字母,例如第二个样例,a加密后为q;第二个字符串其前半部分为加密后的密文,后半部分为不加密的明文,其中前半部分解密后和后半部分一样,但现在这个字符串缺失了一部分没加密的明文,现在让你把该字符串找到,并让其长度最短。
思路:我们先把第二个字符串全部当成密文进行解密得到一个新串t,现在t串的前缀中包含了后半部分明文,原串s中的后缀则包含的明文的一部分,现在结果就显而易见的,从s串后半部分中的后缀中找到与t串中的前缀中相等的最大值maxx,然后s串的前len-maxx则为密文,t串中的前len-maxx则为明文,我们可以用扩展kmp去找,由于这个题的数据比较水也可以暴力去找。
#include <bits/stdc++.h>
using namespace std;
int nxt[100100],exnxt[100100];
void getnxt(char st[])
{
int l=strlen(st);
nxt[0]=l;
int a=0,p=0;
for(int i=1;i<l;i++)
{
if(i>=p||i+nxt[i-a]>=p)
{
if(i>=p)
p=i;
while(p<l&&st[p]==st[p-i])
p++;
nxt[i]=p-i;
a=i;
}
else
{
nxt[i]=nxt[i-a];
}
}
}
void exkmp(char s[],char t[])
{
int l1=strlen(s);
int l2=strlen(t);
getnxt(t);
int a=0,p=0;
for(int i=0;i<l1;i++)
{
if(i>=p||i+nxt[i-a]>=p)
{
if(i>=p)
p=i;
while(p<l1&&p-i<l2&&s[p]==t[p-i])
p++;
exnxt[i]=p-i;
a=i;
}
else
exnxt[i]=nxt[i-a];
}
}
char temp[100100];
char s[100100],t[100100];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
map<char,char>mp;
scanf("%s",temp);
char tt='a';
for(int i=0;i<26;i++)
{
mp[temp[i]]=tt+i;
}
scanf("%s",s);
int l1=strlen(s);
for(int i=0;i<l1;i++)
{
t[i]=mp[s[i]];
}
exkmp(s,t);
int k;
if(l1%2==0)
k=l1/2;
else
k=l1/2+1;
int maxx=0;
for(int i=k;i<l1;i++)
{
if(exnxt[i]==l1-i)
maxx=max(maxx,exnxt[i]);
}
maxx=l1-maxx;
for(int i=0;i<maxx;i++)
printf("%c",s[i]);
for(int i=0;i<maxx;i++)
printf("%c",t[i]);
printf("\n");
}
return 0;
}

浙公网安备 33010602011771号