HDU 4300Clairewd’s message(题目不好懂 KMP)

Clairewd’s message

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


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
 


                      题目大意:这个题目真是要费点脑筋才能看懂。
先给你一个明文对应位置的密文,这个串的长度为26,比如第二组数据qwe实际上代表的是abc。

然后再给你一个串,第二个串就是密文+明文。像第一组数据abc可能是密文,但是abc翻译了之后并不是对应的dab,所以密文会更长一点,变成abcd,这个时候前两个对应的便是ab之后再输出cd。就是要求输出的时候前面的密文必须跟后面的明文相对应。要求输出来的长度最短。

            解题思路:密文的长度至少为一半,我假设前面的一半(向上取整)为密文(也不用假设),假设后面剩下的全是明文,然后KMP看能够有多少匹配的。不过需要用map处理将前面的密文转换成明文。剩下的就是输出了。

            题目地址:Clairewd’s message

AC代码:
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<map>
using namespace std;
map <char,char> cod;  //表示密文所对应的原文
char s2[30];  //译表
char s1[100005],p[50005],s[50005];
int len,len1,len2,next[50005];
//s1是原串,s1代表截获串,p1表示自己假设的最短密文,s表示假设最长的明文

void getnext()
{
     int i,j;
     next[0]=0,next[1]=0;
     for(i=1;i<len1;i++)
     {
          j=next[i];
          while(j&&p[i]==p[j])
               j=next[j];
          if(p[i]==p[j])
               next[i+1]=j+1;
           else
               next[i+1]=0;
     }
}

int KMP()
{
     int i,j=0;
     for(i=0;i<len2;i++)
     {
          while(j&&s[i]!=p[j])
               j=next[j];
          if(s[i]==p[j])
               j++;
     }
     return j;
}

int main()
{
     int T,i;
     scanf("%d",&T);
     while(T--)
     {
          scanf("%s",s2);
          for(i=0;i<26;i++)
             cod[s2[i]]='a'+i;
          scanf("%s",s1);
          len=strlen(s1);
          len1=len/2;
          if(len&1) len1++;
          len2=len-len1;
          for(i=0;i<len1;i++)
               p[i]=cod[s1[i]];  //把密文转化为明文方便匹配
          p[len1]='\0';     //把p搞定
          //cout<<p<<endl;
          for(i=0;i<len2;i++)
               s[i]=s1[len1+i];
          s[len2]='\0';
          getnext();
          int s=KMP();
          int flag=len-s*2;  //标记还有多少木有匹配的
          printf("%s",s1); //原串必须会输出的

          for(i=s;i<s+flag;i++)
               printf("%c",cod[s1[i]]);
          puts("");
     }
     return 0;
}
/*
34
qwertyuiopasdfghjklzxcvbnm
qwert
qwertyuiopasdfghjklzxcvbnm
qwerta
qwertyuiopasdfghjklzxcvbnm
q
abcdefghijklmnopqrstuvwxyz
aaaa
abcdefghijklmnopqrstuvwxyz
aaa
abcdefghijklmnopqrstuvwxyz
aa
abcdefghijklmnopqrstuvwxyz
a
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值