码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-08-24 10:16:32      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   java   os   io   strong   

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

 

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

标签:des   style   blog   http   color   java   os   io   strong   

原文地址:http://www.cnblogs.com/gongxijun/p/3932498.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!