前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >hdu------(4300)Clairewd’s message(kmp)

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

作者头像
Gxjun
发布2018-03-26 14:53:09
6080
发布2018-03-26 14:53:09
举报
文章被收录于专栏:ml

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

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

代码:

代码语言:javascript
复制
 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 }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014-08-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Clairewd’s message
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档