HDU4300 Clairewd’s message(拓展kmp)
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.
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
题意:破译密码。给一个加密表,分别对应a到z加密后的字母。再给一个字符串,为全部的密文+原文的一部分(也可能是全部)。也就是说这个字符串把密文和原文连在一起了,还不全,要求补齐串使满足全部密文+全部原文,并且这个串尽量短。
思路:由于密文一定全给出了,所以暴力的做法就是从中间开始向后检测,当后面的串加密后与前面串的对应前缀吻合,破译就成功了。
想到这就有点熟悉.....主串后缀和模式串前缀.......可以用拓展kmp了。感觉其实把原串再加密或者解密都行,下面的代码是解密,然后得到一个前面原文后面无所谓是什么的串,作为模式串和原串跑一次拓展kmp,得到extend数组,从前往后找满足条件的ex,找到就跳出。条件为前面串长度大于等于ex(原串密文一定比原文长),并且ex和这个长度相加等于主串长(这样才是后缀)。
代码:
#include<bits/stdc++.h>
#define FastIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define repp(i,a,b) for(ll i=a;i<=b;i++)
#define rep1(i,a,b) for(ll i=a;i>=b;i--)
#define mem(gv) memset(gv,0,sizeof(gv))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define QAQ 0
#define miaojie1
#ifdef miaojie
#define dbg(args...) do {cout << #args << " : "; err(args);} while (0)
#else
#define dbg(...)
#endif
void err() {std::cout << std::endl;}
template<typename T, typename...Args>
void err(T a, Args...args){std::cout << a << ' '; err(args...);} using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pLL;
const int mod=1e9+;
const int maxn=1e6+; int nxt[maxn],ex[maxn];
void GETNEXT(char *str)
{
int i=,j,po,len=strlen(str);
nxt[]=len;
while(str[i]==str[i+]&&i+<len)
i++;
nxt[]=i;
po=;
for(i=;i<len;i++)
{
if(nxt[i-po]+i<nxt[po]+po)
nxt[i]=nxt[i-po];
else
{
j=nxt[po]+po-i;
if(j<)j=;
while(i+j<len&&str[j]==str[j+i])
j++;
nxt[i]=j;
po=i;
}
}
} void EXKMP(char *s1,char *s2)
{
int i=,j,po,len=strlen(s1),l2=strlen(s2);
GETNEXT(s2);
while(s1[i]==s2[i]&&i<l2&&i<len)
i++;
ex[]=i;
po=;
for(i=;i<len;i++)
{
if(nxt[i-po]+i<ex[po]+po)
ex[i]=nxt[i-po];
else
{
j=ex[po]+po-i;
if(j<)j=;
while(i+j<len&&j<l2&&s1[j+i]==s2[j])
j++;
ex[i]=j;
po=i;
}
}
} int T;
char s1[maxn],s2[maxn],h[],ha[]; int main(){
scanf("%d",&T);
while(T--){
mem(s2); mem(h); mem(ha);
scanf("%s%s",h,s1);
int l1=strlen(s1);
rep(i,,){
ha[h[i]]=i+;
}
rep(i,,l1){
s2[i]=ha[s1[i]];
}
EXKMP(s1,s2);
int st=l1;
rep(i,,l1){
if(i+ex[i]==l1 && i>=ex[i]){
st=i;
break;
}
}
rep(i,,st){
s2[i]=s1[i];
s2[i+st]=ha[s1[i]];
}
printf("%s\n",s2);
}
return QAQ;
}
HDU4300 Clairewd’s message(拓展kmp)的更多相关文章
- HDU 4300 Clairewd's message ( 拓展KMP )
题意 : 给你一个包含26个小写字母的明文密文转换信息字符串str,第一个表示'a'对应的密文是str[0].'b'对应str[1]……以此类推.接下来一行给你一个另一个字符串,这个字符串由密文+明文 ...
- hdu4300 Clairewd’s message 扩展KMP
Clairewd is a member of FBI. After several years concealing in BUPT, she intercepted some important ...
- hdu------(4300)Clairewd’s message(kmp)
Clairewd’s message Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU-4300 Clairewd’s message
http://acm.hdu.edu.cn/showproblem.php?pid=4300 很难懂题意.... Clairewd’s message Time Limit: 2000/1000 MS ...
- hdu4300 Clairewd’s message【next数组应用】
Clairewd’s message Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu4300 Clairewd’s message
地址:http://acm.hdu.edu.cn/showproblem.php?pid=4300 题目: Clairewd’s message Time Limit: 2000/1000 MS (J ...
- kuangbin专题十六 KMP&&扩展KMP HDU4300 Clairewd’s message
Clairewd is a member of FBI. After several years concealing in BUPT, she intercepted some important ...
- hdu 4300 Clairewd’s message(扩展kmp)
Problem Description Clairewd is a member of FBI. After several years concealing in BUPT, she interce ...
- hdu 4300 Clairewd’s message(kmp/扩展kmp)
题意:真难懂.. 给出26个英文字母的加密表,明文中的'a'会转为加密表中的第一个字母,'b'转为第二个,...依次类推. 然后第二行是一个字符串(str1),形式是密文+明文,其中密文一定完整,而明 ...
随机推荐
- hdoj5328【尺取】
现在在队内赛(灰常艾斯比的队内赛),还是来写篇题解开心一下,23333. 题意: 就是问你找出一个最长的等比数列或者等差数列 思路: 一个等差的尺取,一个等比的尺取.2333,就这么过了,具体自己写吧 ...
- Python 爬虫面试题 170 道:2019 版
引言 最近在刷面试题,所以需要看大量的 Python 相关的面试题,从大量的题目中总结了很多的知识,同时也对一些题目进行拓展了,但是在看了网上的大部分面试题不是很满意,一个是有些部分还是 Python ...
- 黑客攻防技术宝典web实战篇:测试后端组件习题
猫宁!!! 参考链接:http://www.ituring.com.cn/book/885 随书答案. 1. 某网络设备提供用于执行设备配置的 Web 界面.为什么这种功能通常易于受到操作系统命令注入 ...
- Luogu P1552 [APIO2012]派遣【左偏树】By cellur925
题目传送门 $Chat$ 哈哈哈我xj用dfs序乱搞竟然炸出了66分....(其实还是数据水,逃) $Sol$ 首先我们应该知道,一个人他自己的满意度与他子树所有节点的领导力是无关的,一个人的满意度受 ...
- elasticsearch 查询 query
对于 类型是 text的字段,并且分析器指明是ik_max_word的会建立倒排索引 查询的分类: match查询: 会自动转换大小写,会分词, term查询: 不会转换和分词,只能值匹配 term ...
- c++继承汇总(单继承、多继承、虚继承、菱形继承)
多重继承中,一个基类可以在派生层次中出现多次,如果一个派生类有多个直接基类,而这些直接基类又有一个共同的基类,则在最终的派生类中会保留该间接共同基类数据成员的多分同名成员.C++提供虚基类的方法使得在 ...
- nginx使用autoindex
有时候一个nginx服务就是为了用来下载文件的,网上很多下载服务都是这样的 这个很简单 在http段加上以下参数,重启nginx就行. autoindex on; autoindex_exact_si ...
- 1-9方法的重写(override)
什么是重写? 重写,也叫做覆盖,当父类中的方法无法满足子类需求时,子类可以将父类的方法进行重写编写来满足需求.比如孩子继承了父亲的房子,可以将房子重新装修. 方法重写的条件: 两个类必须是继承关系 必 ...
- 1-10super和this关键字
什么是super? super代表的是当前子类对象中的父类型特征. 什么时候使用super? 子类和父类中都有某个数据,例如,子类和父类中都有name这个属性.如果要再子类中访问父类中的name属性, ...
- 微信小程序生成分享图片,保存到本地
1.页面 <canvas canvas-id="shareCanvas" style="width:600px;height:900px">< ...