HDU - 4300(扩展kmp 求后缀的最大前缀)

版权声明:转载请注明出处哦。 https://blog.csdn.net/Dch19990825/article/details/83829749

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

题意:

扫描二维码关注公众号,回复: 4043708 查看本文章

首先t组输入

第一行给你26个字符,即字符串S1, 表示字符的加密方式,即a~z中的第i个字符加密后变为为S1[i]

第二行一个截取的字符串,这个字符串的前部分是情报的密文,后面是情报明文的一部分(因为发送情报的人发现自己被侦察了就不发了),

让你求这个最短的完整信息

重点:

密文的长度一定满足temp>=(len+1)/2  这样才符合题意 ,

将该信息解密后的前缀解密后的后缀最大匹配  表示最长的可能明文,注意这个最长的可能明文长度不能超过len-temp

问题就改成了两个字符串求前缀后缀最大匹配(扩展kmp)

#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=2e5+7;
typedef long long ll;
const int MOD=1e5+7;
int code[100];//将密文转化为明文
char convert(char c)
{
    return code[c-'a']+'a';
}
char infostr[maxn];
char codestr[maxn];
int net[maxn];//存放info的next数组
int maxpp[maxn];//最长后缀匹配前缀数组
void get_nextval(char *s,int* net)
{
    int ls=strlen(s);
    int j=0,k=-1;
    net[0]=-1;
    while(j<ls)
    {
        if(k==-1||s[j]==s[k])
            net[++j]=net[++k];
        else
            k=net[k];
    }
}
int exkmp(char *s,char *p,int *net)
{
    int ls=strlen(s);
    int temp=(ls+1)/2;//代表最大结尾的匹配不能超过
    get_nextval(p,net);
    int i=temp,k=0;
    /*
    重点:  文本串的第temp+1个字符与模式串的第1个字符开始匹配 因为前temp字符肯定是密文,不能让
    前缀与密文匹配到 ,这样匹配得到的前缀才肯定全都是密文的
    扩展kmp:  计算此串的最大匹配前缀时,利用上一个串的最大匹配前缀
    */
    while(i<ls)
    {
        if(k==-1||s[i]==p[k])//第i+1个字符与第k+1个字符是否相等
        {
             ++i;
             ++k;//代表第i+1个字符的最大匹配为k+1个
             maxpp[i]=k;
        }
        else
            k=net[k];
    }
    return maxpp[ls];
}
int main()
{
    int t,ans;
    char bm[30];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",bm);
        for(int i=0;i<26;++i)
            code[bm[i]-'a']=i;
        scanf("%s",infostr);
        int len=strlen(infostr);
        for(int i=0;i<len;++i)
            codestr[i]=convert(infostr[i]);
        ans=exkmp(infostr,codestr,net);
        printf("%s",infostr);
        for(int i=ans;i<len-ans;++i)
            printf("%c",convert(infostr[i]));
        printf("\n");
    }
}

猜你喜欢

转载自blog.csdn.net/Dch19990825/article/details/83829749