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

解密暗纹与明文转换的秘密
本文探讨了FBI特工Clairewd在BUPT进行情报传递时,利用暗纹表将信息进行加密,并通过明文与暗文对应关系进行解密的过程。面对GFW的监测,Clairewd停止了消息的发送。作为挑战,读者被邀请协助GFW从截获的文本中分离出最短可能的明文。

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值