Clairewd’s message HDU - 4300 (扩展KMP)

Clairewd’s message

HDU - 4300

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的题目,扩展KMP的讲解博客下面这个讲的很好很清楚

扩展KMP

而这道题的意思是,给你一个对应的转换表,这个转化表是明文对应的暗文表,也就是‘a’对应第一字母,‘b’对应第二个字母,然后给一个字符串,这个字符串是前一部分是暗文后一部分是明文(即这两部分字符串其实是一样的),而且后面一部分的明文可能不完整了,但不知道分界线从哪里,要我们打印出完整的暗文+明文

思路:先把所给的字符串根据转换表全部转换,这样得到的新字符串前半部分就是明文了(原来是暗文的部分转换成了明文),这样通过扩展kmp我们就可以找到原字符串后半部分和转化后的字符串匹配也就是下图

原串 ———————— ——————(中间空格表示前面是暗文后面是明文)

转化                               ————————— ————(空格前是明文,空格后什么也不是)

这是最终我们找到了分界位置,此时有什么特点,设分界点位置为k,发现其k+extend[k]恰好就到达了串的长度len,即k+entend[k]>=len,同时说明k前面都是暗文,那么k>=extend[k],因为extend[k]最长也就是等于前面暗文的长度嘛,所以k一定在这之后。

code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
const int MAXN = 200000;
int Next[MAXN];
int extend[MAXN];
void EKMP(char s[],char 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[j+1])j++;
    Next[1] = j;
    int a = 1;
    for(i = 2; i < len; i++){
        p = Next[a]+a;
        l = Next[i-a];
        if(i+l<p)Next[i] = l;
        else{
            j = max(0,p-i);
            while(i+j<len&&t[i+j]==t[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;
        l = Next[i-a];
        if(l+i<p)Next[i] = l;
        else{
            j = max(0,p-i);
            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(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%s%s",tab,s);
        int len = strlen(tab);
        int len1 = strlen(s);
        int i;
        for(i = 0; i < len; i++){
            map1[tab[i]] = 'a'+i;
        }
        for(i = 0; i < len1; i++){
            c[i] = map1[s[i]];
        }
        EKMP(s,c);
        int k;
        for(k = 0; k < len1; k++)
            if(k+extend[k]>=len1&&k>=extend[k])
               break;
        for(i = 0; i < k; i++)printf("%c",s[i]);
        for(i = 0; i < k; i++)printf("%c",map1[s[i]]);
        printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值