kuangbin专题十六 KMP&&扩展KMP HDU4300 Clairewd’s message

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.

InputThe 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;
OutputFor each test case, output one line contains the shorest possible complete text.Sample Input
2
abcdefghijklmnopqrstuvwxyz
abcdab
qwertyuiopasdfghjklzxcvbnm
qwertabcde
Sample Output
abcdabcd
qwertabcde



题目意思非常绕。。。其实就是给你一个加密表,然后给你一个字符串S, 是由密文+明文构成,但是明文可能不完整。所以可以知道密文>=明文

所以就可以把S翻译一下得到T串。 S的后缀是明文(可能不完整) T的前缀是完整明文。
然后S和T匹配。存在这么一个位置 i+extend[i]>=len&&i>extend[i] 说明满足题意



 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 const int maxn=100010;
 6 int _,Next[maxn],extend[maxn],len;
 7 char table[26],S[maxn],T[maxn],temp[125];
 8 
 9 void getnext() {
10     Next[0]=len;
11     int j=0;
12     while(j+1<len&&T[j]==T[j+1]) j++;
13     Next[1]=j;
14     int k=1;
15     for(int i=2;i<len;i++) {
16         int L=Next[i-k],p=k+Next[k]-1;
17         if(i+L<p+1) Next[i]=L;
18         else {
19             j=max(0,p-i+1);
20             while(i+j<len&&T[i+j]==T[j]) j++;
21             Next[i]=j;
22             k=i;
23         }
24     }
25 }
26 
27 void getextend() {
28     getnext();
29     int j=0;
30     while(j<len&&T[j]==S[j]) j++;
31     extend[0]=j;
32     int k=0;
33     for(int i=1;i<len;i++) {
34         int L=Next[i-k],p=k+extend[k]-1;
35         if(i+L<p+1) extend[i]=L;
36         else {
37             j=max(0,p-i+1);
38             while(i+j<len&&S[i+j]==T[j]) j++;
39             extend[i]=j;
40             k=i;
41         }
42     }
43 }
44 
45 int main() {
46     //freopen("in","r",stdin);
47     for(scanf("%d",&_);_;_--) {
48         scanf("%s%s",table,S);
49         len=strlen(S);
50         for(int i=0;i<26;i++)
51             temp[table[i]]='a'+i;
52         for(int i=0;i<len;i++)
53             T[i]=temp[S[i]];
54         T[len]='\0';
55         getextend();
56         int i;
57         for(i=0;i<len;i++) {
58             if(i+extend[i]>=len&&i>=extend[i]) break;
59         }
60         for(int j=0;j<i;j++) printf("%c",S[j]);
61         for(int j=0;j<i;j++) printf("%c",T[j]);
62         printf("\n");
63     }
64 }

 



posted @ 2019-01-21 16:10  Frontierone  阅读(129)  评论(0编辑  收藏  举报