HDU4300-Clairewd’s message(KMP前缀匹配后缀)

Clairewd’s message

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3228    Accepted Submission(s): 1248

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

题意:给你一个加密协议,即26个英文字母加密相应表。然后给你一个前一段是加密串,后一段为加密串相应的原串的字符串(原串可能小于加密串)输出最短的原串和加密串

思路:用二分之中的一个前缀去匹配二分之中的一个后缀,由于加密串大于等于原串,利用KMP得到匹配的位置,就可以输出答案。

这道题弹了非常久的TLE,发现不能一个字符一个字符地输出。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 100000+10;
const int esize = 28;
int next[maxn],mid;
char mp[esize],str[maxn];
string str1,str2;

void getNext(){
    next[0] = next[1] = 0;
    int n = str1.size();
    for(int i = 1,j; i < n; i++){
        j = next[i];
        while(j && str1[i] != str1[j]) j = next[j];
        if(str1[i] == str1[j]) next[i+1] = j+1;
        else next[i+1] = 0;
    }
}

void KMP(int sta){
    int n = strlen(str) , j = 0;
    for(int i = sta; i < n; i++){
        while(j && str1[j] != str[i]) j = next[j];
        if(str1[j] == str[i]) j++;
        if(j==str1.size()) break;
    }
    int  k;
    if(n%2==0) k = sta-j;
    else k = sta-j-1;
    for(int i = 0; i < k; i++){
        str1 += mp[str[sta+i]-‘a‘];
        str2 += str[sta+i];
    }
    cout<<str2<<str1<<endl;

}
int main(){
    int ncase;
    cin >> ncase;
    char tmp[esize];
    getchar();
    while(ncase--){
        scanf("%s%s",tmp,str);
        int n = strlen(str);
        if(n==0){
            puts("");
            continue;
        }
        str1.clear();
        str2.clear();
        for(int i = 0; i < 26; i++)
            mp[tmp[i]-‘a‘] = char(‘a‘+i);
        if(n%2==0) mid = n/2-1;
        else mid = n/2;
        for(int i = 0; i <= mid; i++){
            str1 += mp[str[i]-‘a‘];
            str2 += str[i];
        }
        getNext();
        KMP(mid+1);
    }
    return 0;
}
时间: 2024-11-07 19:12:32

HDU4300-Clairewd’s message(KMP前缀匹配后缀)的相关文章

hdu 4300 Clairewd’s message (KMP)

给定一个翻译表,即第i个字母用哪个字母表示 再给一个串,里面前面为密文,后面为明文,密文一定是完整的,但明文不完整或可能没有 求这个完整的前面密文后面明文的串 # include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int len; int next[100010]; char a1[100010],a2[100010],a3[100010]; void Ge

HDU 4300 Clairewd&#39;s message ( 拓展KMP )

题意 : 给你一个包含26个小写字母的明文密文转换信息字符串str,第一个表示'a'对应的密文是str[0].'b'对应str[1]--以此类推.接下来一行给你一个另一个字符串,这个字符串由密文+明文组成,但是现在后面部分的明问可能有不完整的情况(也有可能缺失只包含密文),问你现在最少需要补充多多少个字符串才能使得字符串变成完整的满足==>密文+密文对应的明文 组成的字符串,将这个完整的字符串输出出来. 分析 : 冷静分析一下可以发现,在给出的残缺字符串中,前面的一半肯定是属于密文的!如果不是这

hdu 4300 Clairewd’s message(详解,扩展KMP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4300 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 ea

POJ 2752+KMP+利用next数组性质求出所有相同的前缀和后缀

题目链接:点击进入 这个题目要求所有相同的前缀和后缀的长度.我们可以利用KMP算法中next数组的性质,在next[len]这个点不断的失配下去,这样就可以将所有相同的前后缀的长度求出来.还要注意这个中整个串的长度也可以看成是一个合法的解. 代码如下: #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn=400000+100; char str

kmp(最长前缀与后缀)

http://acm.hdu.edu.cn/showproblem.php?pid=1358 Period Problem Description For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether the prefix is a periodic string.

HDU 4300 Clairewd‘s message 拓展KMP入门

HDU 4300 Clairewd's message 拓展KMP入门 题意 原题链接 这个题关键是要读懂题意,我做的时候就没有读懂,泪.题意是说给你的一个两个字符串,一个是26个字母密码表,依次对应替换的字母.然后给你一个字符串,这个字符串是不完整的(完整的应该是前半部分是加密的,后半部分是解密了的),然而,给你的字符串一定是加密的部分+一部分解密的部分(可以是全部,也可以是没有),让你求出最短的完整字符串,包括密文和明文: 解题思路 考虑给出的字符串S加密部分一定全部给出,所以给出的字符串的

kmp(所有长度的前缀与后缀)

http://poj.org/problem?id=2752 Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27512   Accepted: 14244 Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and aske

字符串前缀,真前缀,后缀,真后缀,及前缀函数

举个例子,如字符串 ababc 首先,不考虑空字符,所有的前缀有a, ab, aba, abab, ababc,其中真前缀有a, ab, aba, abab 同理可以理解后缀,真前(后)缀就是指不包含自身的前(后)缀 前缀函数next[j]是指某个字符串的最长真后缀同时也是它的前缀的子串长度.不太理解可以看下面的例子 a -> 0 ab -> 0 aba -> 1 abab -> 2 ababc -> 0 前缀函数在字符串的匹配中用的较多,如KMP等.它主要是表明在一次匹配失

139. 回文子串的最大长度(回文树/二分,前缀,后缀和,Hash)

题目链接 : https://www.acwing.com/problem/content/141/ #include <bits/stdc++.h> using namespace std; const int MAXN = 1000005 ; const int N = 26 ; struct Palindromic_Tree { //cnt最后count一下之后是那个节点代表的回文串出现的次数 int next[MAXN][N] ;//next指针,next指针和字典树类似,指向的串为当