(kuangbin带你飞--kmp) Clairewd’s message hdu4300

原题目:

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

中文概要:

本题题意是给你一个字符对应表,再给一个密文和明文相连接的串(明文后缀可能缺失),求补全后的串。

(妈的半个多小时才读懂题啥意思)

用的是扩展kmp

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
using namespace std;
const int MAXN=100000+10;
const int MAXM=100+10;
int nextra[MAXN],extend[MAXN];
char s[MAXN],t[MAXN],s1[MAXN];
int flag[300];
int n,m,maxn;
char k[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
void get_next()
{
    nextra[0]=m;
    int st,p;
    for(int i=1,j=-1; i<m; i++,j--)
    {
        if(j==-1||i+nextra[i-st]>=p)
        {
            if(j==-1)
            {
                p=i;
                j=0;
            }
            while(p<m&&t[p]==t[j])
                p++,j++;
            nextra[i]=j;
            st=i;
        }
        else
            nextra[i]=nextra[i-st];
    }
}
void get_extend()//用s的后缀匹配t的前缀
{
    memset(nextra,0,sizeof(nextra));
    memset(extend,0,sizeof(extend));
    get_next();
    int st,p;
    for(int i=0,j=-1; i<n; i++,j--)
    {
        if(j==-1||i+nextra[i-st]>=p)
        {
            if(j==-1)
            {
                p=i;
                j=0;
            }
            while(p<n&&j<m&&s[p]==t[j])
                p++,j++;
            extend[i]=j;
            st=i;
        }
        else
            extend[i]=nextra[i-st];
        maxn=max(maxn,extend[i]);
    }
}
int main()
{
     int T;
     scanf("%d",&T);
     while(T--)
     {
         maxn=0;
         memset(flag,0,sizeof(flag));
         scanf("%s%s",s1,s);
         for(int i=0;i<26;i++)
            flag[s1[i]]=i;
         n=strlen(s);
         m=n;
         for(int i=0;i<n;i++)
            t[i]=k[flag[s[i]]];
        get_extend();
        int kk=n;
        for(int i=(n+1)/2;i<n;i++)
        {
            if((extend[i]+i)==n)
            {
                kk=i;
                break;
            }
        }
        for(int i=0;i<kk;i++)
            printf("%c",s[i]);
        for(int i=0;i<kk;i++)
            printf("%c",t[i]);
        printf("\n");
     }
     return 0;
}

名人不说暗话,我没看懂,太尼玛难了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

deebcjrb

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值