加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

hdu 4300 Clairewd’s message(扩展kmp)

发布时间:2020-12-14 04:38:09 所属栏目:大数据 来源:网络整理
导读: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 trans
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的问题,因为密文长度一定大于等于明文,所以我们再判断一下分割点的位置是不是在字符串的二分之一之后。
?
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long int
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[13]={0,31,28,30,31};
int dir[4][2]={1,0,1,-1,-1};
int dirs[8][2]={1,1};
const int inf=0x3f3f3f3f;
const ll mod=1e9+7;
int nextt[100000+7];
int ex[100000+7];
void getnext(string s){
    int len=s.length(); int po;
    nextt[0]=len;
    int pos=0;
    while(s[pos+1]==s[pos]&&pos<len-1) pos++;
    nextt[1]=pos;
    po=1;
    for(int i=2;i<len;i++){
        if(nextt[i-po]+i<nextt[po]+po)
            nextt[i]=nextt[i-po];
        else{
            int j=po+nextt[po]-i;
            if(j<0) j=0;
            while(i+j<len&&s[j]==s[i+j]) j++;
            nextt[i]=j;
        }
    }
}
void getex(string t,string p){
    int len1=p.length(); int len2=t.length();
    int po;
    getnext(t);
    int pos=0;
    while(t[pos]==p[pos]&&pos<len1&&pos<len2) pos++;
    ex[0]=pos;
    po=0;
    for(int i=1;i<len1;i++){
        if(nextt[i-po]+i<po+ex[po])
            ex[i]=nextt[i-po];
        else{
            int j=po+ex[po]-i;
            if(j<0) j=0;
            while(i+j<len1&&j<len2&&t[j]==p[i+j]) j++;
            ex[i]=j;
        }
    }
}
int main(){
    ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--){
        string tab;
        map<char,char> mm;
        string tx;
        cin>>tab>>tx;
        string ctx="";
        for(int i=0;i<tab.length();i++){
            mm[tab[i]]=i+a;    //解码 
        }
        int len=tx.length();
        for(int i=0;i<len;i++)
            ctx+=mm[tx[i]];
        getex(ctx,tx);
        string ans="";
        int k;
        for(k=(len+1)/2;k<len;k++)
            if(k+ex[k]==len) //k+ex值如果等于len 说明这个就是后缀 
                break;
        for(int i=0;i<k;i++)
            ans+=tx[i];
        for(int i=0;i<k;i++)
            ans+=mm[tx[i]];
        cout<<ans<<endl;
    }
    return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读