hdu4300 Clairewd’s message
地址:http://acm.hdu.edu.cn/showproblem.php?pid=4300
题目:
Clairewd’s message
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6347 Accepted Submission(s): 2381
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.
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.
T<= 100 ;
n<= 100000;
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
Author
BUPT
Source
思路:扩展kmp+枚举
1 #include<iostream>
2 #include<string>
3 #include<cstring>
4 #include<cstdio>
5
6 using namespace std;
7 const int K=1e6+7;
8 int nt[K],extand[K];
9 char S[K],T[K],sa[K],tr[200];
10 void Getnext(char *T,int *next)
11 {
12 int len=strlen(T),a=0;
13 next[0]=len;
14 while(a<len-1 && T[a]==T[a+1]) a++;
15 next[1]=a;
16 a=1;
17 for(int k=2; k<len; k++)
18 {
19 int p=a+next[a]-1,L=next[k-a];
20 if( (k-1)+L >= p)
21 {
22 int j = (p-k+1)>0 ? (p-k+1) : 0;
23 while(k+j<len && T[k+j]==T[j]) j++;
24 next[k]=j;
25 a=k;
26 }
27 else
28 next[k]=L;
29 }
30 }
31 void GetExtand(char *S,char *T,int *next)
32 {
33 Getnext(T,next);
34 int slen=strlen(S),tlen=strlen(T),a=0;
35 int MinLen = slen < tlen ? slen : tlen;
36 while(a<MinLen && S[a]==T[a]) a++;
37 extand[0]=a;
38 a=0;
39 for(int k=1; k<slen; k++)
40 {
41 int p=a+extand[a]-1, L=next[k-a];
42 if( (k-1)+L >= p)
43 {
44 int j= (p-k+1) > 0 ? (p-k+1) : 0;
45 while(k+j<slen && j<tlen && S[k+j]==T[j]) j++;
46 extand[k]=j;
47 a=k;
48 }
49 else
50 extand[k]=L;
51 }
52 }
53 int main(void)
54 {
55 int t;cin>>t;
56 while(t--)
57 {
58 scanf("%s%s",S,T);
59 int len=strlen(T),st=0;
60 for(int i=0;i<26;i++)
61 tr[S[i]]='a'+i;
62 for(int i=0;i<len;i++)
63 sa[i]=tr[T[i]];
64 sa[len]=T[len];
65 GetExtand(T,sa,nt);
66 for(int i=(len+1)/2;i<len&&!st;i++)
67 if(extand[i]==len-i)
68 st=i-1;
69 if(st==0)st=len-1;
70 for(int i=0;i<=st;i++)
71 printf("%c",T[i]);
72 for(int i=0;i<=st;i++)
73 printf("%c",tr[T[i]]);
74 printf("\n");
75
76 }
77 return 0;
78 }
作者:weeping
出处:www.cnblogs.com/weeping/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。