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;
}


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

  1. hdu------(4300)Clairewd’s message(kmp)

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

  2. hdu4300 Clairewd’s message【next数组应用】

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

  3. hdu 4300 Clairewd’s message KMP应用

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

  4. POJ 2752 Seek the Name,Seek the Fame(KMP,前缀与后缀相等)

    Seek the Name,Seek the Fame 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...

  5. HDU-4300 Clairewd’s message

    http://acm.hdu.edu.cn/showproblem.php?pid=4300 很难懂题意.... Clairewd’s message Time Limit: 2000/1000 MS ...

  6. hdu4300 Clairewd’s message

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=4300 题目: Clairewd’s message Time Limit: 2000/1000 MS (J ...

  7. hdu4300 Clairewd’s message 扩展KMP

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

  8. kuangbin专题十六 KMP&&扩展KMP HDU4300 Clairewd’s message

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

  9. HDU4300 Clairewd’s message(拓展kmp)

    Problem Description Clairewd is a member of FBI. After several years concealing in BUPT, she interce ...

随机推荐

  1. HDU5904 LCIS 水题

    http://acm.hdu.edu.cn/showproblem.php?pid=5904:// 说是LCIS其实和LCIS没有一点儿关系的水题. 代码 #include<cstdio> ...

  2. 利用dll加载漏洞实现远程代码执行

    微软的“不安全dll加载”漏洞涉及Windows XP至Windows 7等多个版本的操作系统.由于Windows存在加载未指明完整路径的dll文件的机制,可能导致用户在使用第三方软件.玩游戏.听音乐 ...

  3. SpringBoot返回结果如果为null或空值不显示处理方法

    第一种方法:自定义消息转换器 @Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter{ // /** // * ...

  4. 实用小工具 -- 国家地区IP段范围查询工具

    如果想限制某个国家地区IP段访问,这几个查询工具就很有用了. 可以查询各个国家IP段范围,并且是持续更新的,使用方便. 当然,除此之外,你还可以通过APNIC.ARIN.RIPE这些官方IP分配机构查 ...

  5. SQL的in的参数化查询

    SqlCommand cmd=con.CreateCommand(); cmd.CommandText="exec('select * from novel where novelid in ...

  6. CodeM资格赛1

    题目描述 美团外卖的品牌代言人袋鼠先生最近正在进行音乐研究.他有两段音频,每段音频是一个表示音高的序列.现在袋鼠先生想要在第二段音频中找出与第一段音频最相近的部分. 具体地说,就是在第二段音频中找到一 ...

  7. centos安装pcre

    安装pcre前需要已安装gcc工具 1.跳转下载目录 cd install-file 2.下载pcre wget ftp://ftp.csx.cam.ac.uk/pub/software/progra ...

  8. Lnmmp

    简介 LNMMP=Linux+Nginx+MySQL+Memcache+PHP: 利用Nginx的高性能特点做前端反向代理服务器,分发用户请求,并在能够利用缓存的地方使用Memcache缓存服务,以加 ...

  9. ruby 状态转移

    0. 引言       昨天遇到一个问题,就是关于对象状态转移的问题,我姑且这样命名吧.简要描述一下就是:对于一个人,他有进食,帮助他人,恋爱等功能,但是这些功能是有先后顺序的,对于刚出生的人,他要先 ...

  10. Autocomplete TEdit

    http://forum.codecall.net/topic/75946-autocomplete-tedit/ Overview Autocomplete feature really helpf ...