hdu------(4300)Clairewd’s message(kmp)

时间:2022-05-05
本文章向大家介绍hdu------(4300)Clairewd’s message(kmp),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Clairewd’s message

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

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

Author

BUPT

Source

2012 Multi-University Training Contest 1

大意:  首先给你一个暗纹表,然后第二行给你一个包含暗纹和名文的的字符串,要你解析出明文... (很屌炸天的英文描述,完全不知道要表达什么,从去年就想做这道题,一看,不明白题意思就放下了...)

代码:

 1 #include<cstring>
 2 #include<cstdio>
 3 #include<map>
 4 using namespace std;
 5 const int maxn=100005;
 6 char stdch[28];  //给出的暗文标准版
 7 map<char,int>str;
 8 char text[maxn];
 9 char tes[maxn];
10 int next[maxn];
11   void getnext(int len){
12         int i=0,j=-1;
13         //memset(next,0,sizeof(next));
14         next[0]=-1;
15         while(i<len){
16         if(j==-1||tes[i]==tes[j]){
17             i++;
18             j++;
19             //  next[i]=j;   优化一下
20           if(tes[i]!=tes[j]) next[i]=j;
21           else next[i]=next[j];
22         }
23         else j=next[j];
24       }
25   }
26 void kmp(int len){
27 
28      //前半部是暗纹,后半部是明文,所以起点:i
29       //tes是经过翻转之后的暗纹,也就是明文
30        getnext(len);
31        int len1=len;
32        if(len1&1)len1++;  //这里需要特别注意因为暗纹要不明文长,明文可以又缺失
33       int i=len1>>1,j=0;
34       while(i<len&&j<len){
35           if(j==-1||text[i]==tes[j]){
36               i++;
37               j++;
38           }
39           else j=next[j];
40       }
41       printf("%s",text);
42      if(j*2!=len) {
43         for(int i=j;i+j<len;i++)
44            printf("%c",tes[i]);
45      }
46      puts("");
47 }
48 int main(){
49     int test;
50     //freopen("test.in","r",stdin);
51     scanf("%d",&test);
52     while(test--)    {
53 
54       if(!str.empty()) str.clear();
55       scanf("%s",stdch);
56       for(int i=0;i<26;i++)
57           str[stdch[i]]=i;
58        scanf("%s",text);
59        int tslen=strlen(text);
60       for(int i=0;i<tslen;i++){
61              tes[i]=str[text[i]]+'a';    //经过两次旋转
62       }
63          kmp(tslen);
64     }
65  return 0;
66 }