HDU 4300 Clairewd’s message(扩展KMP)

Clairewd’s message

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

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  

 

Author BUPT  

 

Source
2012 Multi-University Training Contest 1  

 

Recommend zhuyuanchen520       这题作为是多校赛第一场的第一题,我已经盯上它很久了。但是一直苦于题目意思太难懂了。。。。 反复看了n遍都不知道题目在说什么。。。。终于在今天看懂了题意,秒过了,1A 这题的意思就是首先有一个字母的转换表,就是输入的第一行的字符串,就是’a’转成第一个字母,’b’转成转换表的第二个字母······· 然后下面一个字符串是密文+明文的形式的字符串。 就是说前后两段是重复的,只不过通过转换表转换了下。 而且后面一段可能不完整。 让我们补完整。 明显的扩展KMP。 就是最长的前缀。     附上解题报告:

这道题问的就是将1个串如何变为stringA+stringB的形式,使得stringA是stringB经过映射得到相同的串。映射那步其实没有什么价值,假设str为原串s经过映射后得到的串,我们可以以str为模式串,以s为原串做一次扩展KMP,得到extend数组,extend[i]表示原串以第i开始与模式串的前缀的最长匹配。经过O(n)的枚举,我们可以得到,若extend[i]+i=len且i>=extend[i]时,表示stringB即为该点之前的串,stringA即为该点之前的str串,最后输出即可。

 

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<map>
#include<algorithm>
using namespace std;
const int MAXN=200000;

int next[MAXN];
int extend[MAXN];
void EKMP(char s[],char t[])//s[]为主串,t[]为模式串
{
    int i,j,p,l;
    int len=strlen(t);
    int len1=strlen(s);
    memset(next,0,sizeof(next));
    memset(extend,0,sizeof(extend));

    next[0]=len;
    j=0;
    while(j+1<len&&t[j]==t[1+j])j++;
    next[1]=j;

    int a=1;
    for(int i=2;i<len;i++)
    {
        p=next[a]+a-1;
        l=next[i-a];
        if(i+l<p+1)next[i]=l;
        else
        {
            j=max(0,p-i+1);
            while(i+j<len&&t[i+j]==t[0+j])j++;
            next[i]=j;
            a=i;
        }
    }


    j=0;
    while(j<len1&&j<len&&s[j]==t[j])j++;

    extend[0]=j;
    a=0;
    for(i=1;i<len1;i++)
    {
        p=extend[a]+a-1;
        l=next[i-a];
        if(l+i<p+1)next[i]=l;
        else
        {
            j=max(0,p-i+1);
            while(i+j<len1&&j<len&&s[i+j]==t[j])j++;
            extend[i]=j;
            a=i;
        }
    }
}

char s[MAXN],tab[MAXN],c[MAXN];
map<char,char>map1;

int main()
{
   // freopen("in.txt","r",stdin);
  //  freopen("out.txt","w",stdout);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s%s",&tab,&s);
        int len=strlen(tab);
        int len1=strlen(s);
        for(int i=0;i<len;i++)
            map1[tab[i]]='a'+i;
        for(int i=0;i<len1;i++)
           c[i]=map1[s[i]];
        c[len1]=0;
        EKMP(s,c);

        int k;
        for(k=0;k<len1;k++)
        {
            if(k+extend[k]>=len1&&k>=extend[k])
              break;
        }
        for(int i=0;i<k;i++)printf("%c",s[i]);
        for(int i=0;i<k;i++)printf("%c",map1[s[i]]);
        printf("\n");
    }
    return 0;
}

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/kuangbin/archive/2012/08/27/2659246.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞