hdu 4300 kmp算法扩展

Clairewd’s message

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2929    Accepted Submission(s): 1132


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
abcdabcd
qwertabcde
 
题目大意:给两个字符串,第一个为字母转化表(第一个字母a对应字母s[0]),第二个为密文+明文的字符串(对应转化的)。不过其中明文部分可能丢失一部分(包括所有),把他补全再输出结果。
分析:把第一个字符串的后半部分映射(明文——密文),KMP算法求他的失配函数,求出他的最大后缀(小于等于len/2)。
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 using namespace std;
 5 
 6 int mp[27],f[200010];
 7 char str[27],s1[200010],s2[200010];
 8 
 9 void HalfChange()
10 {
11     int i,len=strlen(s1);
12     for(i=len/2;i<len;i++)
13         s1[i]=str[s1[i]-'a'];
14 }
15 
16 void getFail()
17 {
18     int i,j,len=strlen(s1);
19     f[0]=f[1]=0;
20     for(i=1;i<len;i++)
21     {
22         j=f[i];
23         while(j && s1[i]!=s1[j]) j=f[j];
24         f[i+1]=(s1[i]==s1[j]?j+1:0);
25     }
26 }
27 
28 int main()
29 {
30     int t,i,len,k;
31     scanf("%d",&t);
32     while(t--)
33     {
34         scanf("%s %s",str,s1);
35         for(i=0;i<26;i++) mp[str[i]-'a']=i;
36         strcpy(s2,s1);
37         len=strlen(s1);
38         HalfChange();//把s1后半部分由明文转成密文
39         getFail();//s1求失配函数
40         k=f[len];
41         while(k > len/2) k=f[k];
42         for(i=0;i<len-k;i++) printf("%c",s2[i]);
43         for(i=0;i<len-k;i++) printf("%c",mp[s2[i]-'a']+'a');
44         printf("\n");
45     }
46     return 0;
47 }

 

 

 

posted on 2014-03-28 21:31  雄..  阅读(152)  评论(0编辑  收藏  举报

导航