HDU4300 Clairewd’s message(e chou ti mian)拓展KMP

传送门
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

别的就不多说什么,就是题意恶心人;
题意:给你一串字符表,表示明文对应的密文,比如样例2第一行qwe…对应明文abc…; 然后现在有一串信息由(完整的密文+部分明文)组成(有可能一整串都是密文),然后我们就知道密文肯定至少占这串字符串的一半,因为密文是完整的,明文是缺失的。
然后我们结合样例:
样例一: 信息:abcdab , 假设前半部分abc是密文,那剩下的就是明 文dad, 这时明文也是完整的,
Pi : abc
Si : dab 但由字符表发现每一位Pi与Si显然不对应; 假设不成立。
假设前半部分abcd是密文,那剩下的就是明文ad, 这时明文也是完整的,
Pi : abcd
Si : ab 由字符表发现每一位Pi与Si是对应的,此时我们只需要补全剩下的明文cd即可,所以输出 abcdabcd

接下来
先把S全部都当作是密文的,然后把S全转换成明文,保存为T
这时,S中的密文部分就全部都变成了明文,而明文部分都变成了xxx(不用管是什么)。
然后可以发现,原来的S中的明文部分是S的后缀,而T中的明文部分是T的前缀。
所以,演变成了求S【i…n】中的与T的最长公共前缀,就是赤裸的拓展KMP问题了。

我们要输出密文最短就是要 S【i…n】中的与T的最长公共前缀 最大,但 密文的长度一定是大于或等于S长度的一半;

#include <iostream>
#include <stdio.h>
#include <string>
#include<cstring>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn=1e5+5;
char p[maxn],s[maxn],mp[30],vis[maxn];
int Ext[maxn],Nxt[maxn],len;

void get_Nxt()
{
    Nxt[0]=len;
    int now=0;
    while (s[now]==s[now+1]&&now+1<len) now++;
    Nxt[1]=now;
    int pre=1;
    for(int i=2;i<len;i++)
    {
        if(i+Nxt[i-pre]<Nxt[pre]+pre) Nxt[i]=Nxt[i-pre];
        else{
            now=Nxt[pre]+pre-i,now=max(now,0);
            while (s[now]==s[i+now]&&i+now<len) now++;
            Nxt[i]=now;
            pre=i;
        }
    }
}
void ex_KMP()
{
    get_Nxt();int now=0;
    while (s[now]==p[now]&&now<len) now++;
    Ext[0]=now;
    int pre=0;
    for(int i=1;i<len;i++)
    {
        if(i+Nxt[i-pre]<Ext[pre]+pre) Ext[i]=Nxt[i-pre];

        else{
            now=Ext[pre]+pre-i,now=max(0,now);
            while (s[now]==p[i+now]&&now<len&&now+i<len) now++;
            Ext[i]=now;
            pre=i;
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while (t--){

        memset(s, 0, sizeof(s));
        memset(Nxt, 0, sizeof(Nxt));
        memset(Ext, 0, sizeof(Ext));
        memset(vis,0,sizeof(vis));

        scanf("%s", mp);
        scanf("%s",p);
        len = strlen(p);
        for(int i=0;i<26;i++)
            vis[mp[i]]=i+'a';

        for(int i=0; i<len; i++)
            s[i]=vis[p[i]];

        ex_KMP();
        int k;
        for(k=0;k<len;k++){
            if(k+Ext[k]>=len&&k>=Ext[k]) break;
        }

        for(int i=0;i<k;i++)
            printf("%c",p[i]);
        for(int i=0;i<k;i++)
            printf("%c",s[i]);

        printf("\n");

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值