HDU4300 Clairewd’s message(扩展KMP)

Clairewd’s message

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

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

题意:给出一个转换表和一个串,串的前面是密文,后面是明文,不知道密文和明文从何分隔,明文可能没给全,求原串

分析:对于给出的串,前密文后明文,那么通过转换表转换一下,就变成了前明文后密文,此时只要求S串的明文和T串的明文的最大前缀匹配长度,此时就想到扩展KMP,然后找出S串中以S[i]为起点可以一直匹配到s[n-1]的最小i点,即为密文和明文的分割点,因为i越小求出的答案串长度越小,补全i点之后的串即可。
注意还有个坑点,就是 按上述方法时 密文长度<明文,所以寻找i时还要加上一个条件:i>=(n+1)/2

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
using namespace std;
const int N=1e5+10;
int nxt[N],n,m,extend[N];
char str[N],a[N],b[N];
int book[200];
void getnxt()
{
    nxt[0]=m;
    int a,p;
    for(int i=1,j=-1;i<m;i++,j--)
    {
        if(j==-1||i+nxt[i-a]>=p)
        {
            if(j==-1)
            {
                p=i;
                j=0;
            }
            while(p<m&&str[p]==str[j])
                p++,j++;
            nxt[i]=j;
            a=i;
        }
        else
            nxt[i]=nxt[i-a];
    }
}
void getextend()
{
    getnxt();
    int a,p;
    for(int i=0,j=-1;i<n;i++,j--)
    {
        if(j==-1||i+nxt[i-a]>=p)
        {
            if(j==-1)
            {
                p=i;
                j=0;
            }
            while(p<n&&j<m&&b[p]==str[j])
            p++,j++;
                extend[i]=j;
            a=i;
        }
        else
            extend[i]=nxt[i-a];
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s %s",a,b);
        for(int i=0;a[i];i++)
           book[a[i]]=i+'a';
         memset(str,0,sizeof(str));
           for(int i=0;b[i];i++)
               str[i]=book[b[i]];
               n=strlen(b);
               m=n;
        getextend();
        int maxx=n;
        for(int i=0;i<n;i++)
        {
            if(i+extend[i]>=n&&i>=(n+1)/2)
            {
                maxx=i;
                break;
            }
        }
        memset(str,0,sizeof(str));
        for(int i=0;i<maxx;i++)
        {
            str[i]=b[i];
            str[maxx+i]=book[b[i]];
        }
        puts(str);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值