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[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
int nextt[+];
int ex[+];
void getnext(string s){
int len=s.length(); int po;
nextt[]=len;
int pos=;
while(s[pos+]==s[pos]&&pos<len-) pos++;
nextt[]=pos;
po=;
for(int i=;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<) j=;
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=;
while(t[pos]==p[pos]&&pos<len1&&pos<len2) pos++;
ex[]=pos;
po=;
for(int i=;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<) j=;
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=;i<tab.length();i++){
mm[tab[i]]=i+'a'; //解码
}
int len=tx.length();
for(int i=;i<len;i++)
ctx+=mm[tx[i]];
getex(ctx,tx);
string ans="";
int k;
for(k=(len+)/;k<len;k++)
if(k+ex[k]==len) //k+ex值如果等于len 说明这个就是后缀
break;
for(int i=;i<k;i++)
ans+=tx[i];
for(int i=;i<k;i++)
ans+=mm[tx[i]];
cout<<ans<<endl;
}
return ;
}

hdu 4300 Clairewd’s message(扩展kmp)的更多相关文章

  1. hdu 4300 Clairewd’s message(kmp/扩展kmp)

    题意:真难懂.. 给出26个英文字母的加密表,明文中的'a'会转为加密表中的第一个字母,'b'转为第二个,...依次类推. 然后第二行是一个字符串(str1),形式是密文+明文,其中密文一定完整,而明 ...

  2. HDU 4300 Clairewd's message ( 拓展KMP )

    题意 : 给你一个包含26个小写字母的明文密文转换信息字符串str,第一个表示'a'对应的密文是str[0].'b'对应str[1]……以此类推.接下来一行给你一个另一个字符串,这个字符串由密文+明文 ...

  3. hdu 4300 Clairewd’s message KMP应用

    Clairewd’s message 题意:先一个转换表S,表示第i个拉丁字母转换为s[i],即a -> s[1];(a为明文,s[i]为密文).之后给你一串长度为n<= 100000的前 ...

  4. HDU - 4300 Clairewd’s message (拓展kmp)

    HDU - 4300 题意:这个题目好难读懂,,先给你一个字母的转换表,然后给你一个字符串密文+明文,密文一定是全的,但明文不一定是全的,求最短的密文和解密后的明文: 题解:由于密文一定是全的,所以他 ...

  5. hdu4300 Clairewd’s message 扩展KMP

    Clairewd is a member of FBI. After several years concealing in BUPT, she intercepted some important ...

  6. hdu 4300 Clairewd’s message 字符串哈希

    Clairewd’s message Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  7. hdu 4300 Clairewd’s message(具体解释,扩展KMP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4300 Problem Description Clairewd is a member of FBI. ...

  8. HDU 4300 Clairewd’s message(扩展KMP)

    思路:extend[i]表示原串以第i開始与模式串的前缀的最长匹配.经过O(n)的枚举,我们能够得到,若extend[i]+i=len且i>=extend[i]时,表示t即为该点之前的串,c即为 ...

  9. HDU 4300 Clairewd’s message(扩展KMP)题解

    题意:先给你一个密码本,再给你一串字符串,字符串前面是密文,后面是明文(明文可能不完成整),也就是说这个字符串由一个完整的密文和可能不完整的该密文的明文组成,要你找出最短的密文+明文. 思路:我们把字 ...

随机推荐

  1. oracle服务端安装与配置

    从oracle官网下载oracle服务端的安装包. 下载下来是两个压缩文件,两个压缩文件都解压(缺一不可)到同一目录下,最后会得到一个database文件夹. 双击database文件夹下的setup ...

  2. MyEclipse10 复制之前的项目部署到tomcat时项目名称对不上,还是复制前的项目名称,哪里修改设置

    工程 -- 右键属性 -- Myeclispse -- web修改一下发布名字就可以了.

  3. Day 3-6 生成器&迭代器

    ---恢复内容开始--- 列表生成式: list = [i*i for i in range(20)] # 这就是一个列表生成式 print(list) # [0, 1, 4, 9, 16, 25, ...

  4. (三)类数组对象 NamedNodeMap简单介绍

    Ele.attrbutes将返回一个NamedNodeMap对象,即NamedNodeMap存储的是元素的“特性Attribute”集合.而集合中的每一个元素,都是Attr类型的对象. html: & ...

  5. JUnit测试提示Java.lang.Exception: No runnable methods

    网上一大堆都说,没写@Test,或者是,导包错误,= =然而我并没有发现我有这个毛病 = =最后终于找到罪魁祸首 Junit版本太低!!! Junit版本太低!!! Junit版本太低!!! = =因 ...

  6. Percona-xtrabackup 使用详解与原理

    现在有个需求需要对使用 innodb 的数据库进行热备.网上查了很多工具皆推荐 Percona-xtrabackup 于是就仔细了解调研一番. 我们可以前往 https://www.percona.c ...

  7. Codevs1541[USACO]围墙涂色

    离散加差分有点涨姿势啊 对我这种菜鸡而言还是第一次看到啊qwq 题面 大意 :n次,每次覆盖一个区间,求覆盖过m次的节点个数 sol:大概是差分的思想加上离散,就可以解决普通差分无法解决的问题了,比如 ...

  8. 解决Maven依赖报错问题

    问题现象:从svn上签下源代码,本地执行打包命令,提示如下错误: [ERROR] Failed to execute goal on project dddwriter: Could not reso ...

  9. linq之左连接 + group by

    var list = from item in (from s in _sysBll.GetList(s => s.ParamID == "TraSchType" & ...

  10. 解决面板里没有network manager图标的问题 ,也就是在桌面环境下,没有那个网络图标

    在安装好了桌面之后,竟然发现没有那个连接网络的图标,本来想连接无限网络.可是.......如果去手动命令行下去配置这个连接wifi有些麻烦,所以我还是去找解决办法了 我执行了一条命令就解决了 gcon ...