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. BZOJ4556 HEOI2016 字符串

    后缀数组. 复习了后缀数组后发现这题真的很好写. 我们只需要将c依次向前向后扩展,找落在[a,b]区间内的最大值,遍历过程中不断用height数组更新. 复杂度就是后缀数组,比主席树的快多了. By: ...

  2. [POJ1205]Water Treatment Plants

    题目大意: 有一排n个格子,要在它们上面装管道. 每个格子上的管道都是T形的,但是可以有三种流动的方向. 每种都是把两个方向的水往另一个方向排出. 如果方向是向左或向右,就是排到相邻的格子里. 特别地 ...

  3. bzoj 1009: [HNOI2008]GT考试 -- KMP+矩阵

    1009: [HNOI2008]GT考试 Time Limit: 1 Sec  Memory Limit: 162 MB Description 阿申准备报名参加GT考试,准考证号为N位数X1X2.. ...

  4. iOS Core Animation 动画 入门学习(一)基础

    reference:https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreAnimation_guide ...

  5. VK Cup 2016 - Qualification Round 2 A. Home Numbers 水题

    A. Home Numbers 题目连接: http://www.codeforces.com/contest/638/problem/A Description The main street of ...

  6. Redis 3.0 Windows 安装步骤

    Redis 3.0 Windows 安装步骤 ----来自 https://www.aliyun.com/jiaocheng/872572.html 发布时间:2018-04-10 来源:网络 上传者 ...

  7. MyEclipse2015创建配置Web+Maven项目

    首先我的MyEclipse版本是2015 stable 2.0,在MyEclipse中创建Maven项目通常有两种常见的方式,它们分别是: New Maven Project  New Web Pro ...

  8. Java过滤任意(script,html,style)标签符,返回纯文本--封装类

     import java.util.regex.Pattern;   /**  * 过滤标签字符串,返回纯文本  *  */ public class ChangePlainText {        ...

  9. CSDN 四川大学线下编程比赛第一题:数字填充

    题目意思: http://student.csdn.net/mcs/programming_challenges peter喜欢玩数字游戏,但数独这种游戏对他来说太简单了,于是他准备玩一个难的游戏. ...

  10. 执行计划解读 简朝阳 (Sky Jian) and 那蓝蓝海

    http://greemranqq.iteye.com/blog/2072878 http://www.mysqlab.net/ http://www.mysqlpub.com/ http://blo ...