HDU 4300 Clairewd’s message (KMP的nex数组应用)

http://acm.hdu.edu.cn/showproblem.php?pid=4300

Clairewd’s message

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


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
重点是题意:我个菜鸡看了半天也没看懂0.0,现在第一行给你26个字母,这是字母的加密规则,

例如:qwertyuiopasdfghjklzxcvbnm

abcdefghijklmnopqrstuvwxyz这是字母的加密规则(将明文中所有的q都变成a,所有的w都变成b,以此类推),

明文(初始的串),密文(加密后的串)

第二行给你一个串,这个串是由密文(是由明文加密后得来的)+明文组成的(密文长度一定大于等于明文),

密文一定是完整的,但是明文不一定是完整的。你并不知道明文有多长,所以题意让你输出补全后的密文和明文

若有多个都满足题意,则输出最短的

思路:你先将第二个串全部解密成明文(不论它是密文还是明文,因为明文的长度不确定),然后将第二个串添加到解密后的串后面

新得到的串是原来串的两倍,这个串一定存在一个后缀等于前缀,后缀的长度一定是不完整的明文的长度。

然后明文,密文的长度都确定了,输出密文和明文就行了。

假设输入的第二个串的长度为la1;新形成的串长度为la2.

求新串的nex数组时,只需要求(0到la1)和 (la1+la1/2到la2;)因为密文一定大于等于明文的长度。

最后nex[la2]就是未补全的明文的长度。代码写的很清楚。

上AC代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <map>
using namespace std;
const int maxx = 200100;
const int mod = 10007;
char a[maxx],c[100];
int nex[maxx],n,la1,la2;
map<char,char>ma;
void prekmp()
{
    int x=-1,y=0;
    nex[0]=-1;
    while(y<la1)
    {
        while(x!=-1&&a[x]!=a[y])
            x=nex[x];
        nex[++y]=++x;
    }
    y=la2-(la1>>1);
    x=0;
    while(y<la2)
    {
        while(x!=-1&&a[x]!=a[y])
            x=nex[x];
        nex[++y]=++x;
    }
    for(int i=0;i<la1-nex[la2];i++)
        printf("%c",a[la1+i]);
    for(int i=0;i<la1-nex[la2];i++)
        printf("%c",a[i]);
    printf("\n");
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",c);
        for(int i=0;i<26;i++)
          ma[c[i]]='a'+i;
          scanf("%s",a);
          la1=strlen(a);
          la2=la1<<1;
          for(int i=la1;i<la2;i++)
            a[i]=a[i-la1];
          for(int i=0;i<la1;i++)
              a[i]=ma[a[i]];
          prekmp();
    }
}







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值