Clairewd’s message

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
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 
 6 const int MS=100005;
 7 char str1[MS],str2[MS],str3[MS];
 8 int next[MS];
 9 int table[27];
10 void get_next(char *s,int *next)
11 {
12     int i=1,j=0;
13     next[1]=0;
14     int len=strlen(s);
15     while(i<len)
16     {
17         if(j==0||s[i-1]==s[j-1])
18         {
19             i++;
20             j++;
21             next[i]=j;  //求最大循环次数  或者前后公共缀的长度  就用这个
22             /*
23             if(s[i-1]==s[j-1])
24                 next[i]=next[j];   //优化了功能却减弱了
25             else
26                 next[i]=j;
27             */
28         }
29         else
30             j=next[j];
31     }
32 }
33 
34 int KMP(char *s,char *t,int pos)
35 {
36     int i=pos,j=1;
37     int len1=strlen(s);
38     int len2=strlen(t);
39     get_next(t,next);
40     while(i<=len1&&j<=len2)
41     {
42         if(j==0||s[i-1]==t[j-1])
43         {
44             i++;
45             j++;
46         }
47         else
48             j=next[j];
49     }
50     /*
51     if(j>len2)
52         return i-len2-1;
53     return -1;
54     */
55     return j-1;
56 }
57 
58 int main()
59 {
60    int T;
61    scanf("%d",&T);
62    while(T--)
63    {
64       scanf("%s",str1);
65       scanf("%s",str2);
66       int len1=strlen(str1);
67       int len2=strlen(str2);
68       for(int i=0;i<len1;i++)
69       {
70           table[str1[i]-'a']=i;
71       }
72       int j=0;
73       for(int i=0;i<len2;i++)
74       {
75           str3[j++]=table[str2[i]-'a']+'a';
76       }
77       str3[j]='\0';
78       j=KMP(str2,str3,(len2+1)/2+1);
79       if(j*2==len2)
80         printf("%s\n",str2);
81       else
82       {
83           printf("%s",str2);
84           int tmp=len2-j;
85           for(int i=j;i<tmp;i++)
86             printf("%c",str3[i]);
87           printf("\n");
88       }
89    }
90     return 0;
91 }

 

posted @ 2015-02-27 17:34  daydaycode  阅读(153)  评论(0编辑  收藏  举报