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. Entity Framework(实体框架 EF)

    什么是Entity Framework呢(下面简称EF)? EF(实体框架)是ADO.NET中的一组支持开发面向数据的软件应用程序的技术,是微软的一个ORM框架.ORM(对象关系映射框架):指的是面向 ...

  2. 两个函数彻底理解Lua中的闭包

    本文通过两个函数彻底搞懂Lua中的闭包,相信看完这两个函数,应该能理解什么是Lua闭包.废话不多说,上 code: --[[************************************** ...

  3. combotree的总结(这个好)

    1:最近有这个需求,就是ext下的combo下拉是树,网上的例子很多,封装的也很好,基本都可以满足下拉框下出现想要的树,但是我的使用情况是比如在用户编辑的时候,要把用户已经选择过的数值自动再赋值到下拉 ...

  4. Java解读内存,优化编程

    1.别用new Boolean 在很多场景中Boolean类型是必须的,比如JDBC中boolean类型的set与get都是通过Boolean封装传递的,大部分ORM也是用Boolean来封装bool ...

  5. Use a microcontroller to design a boost converter

    Boost converters, like other switchers, have traditionally received their control signals from a ded ...

  6. shell 编译和执行java文件

    编译java程序 javac test.java 执行java程序 java test 附件test.java class test{         public static void main( ...

  7. Windows下Linux 环境 Cygwin安装及配置 基本工具使用

    用久了Ubuntu开发环境,切换到windows多少有些不爽,想在windows下享用linux便捷的工具可以安装强大的Cygwin,本人也是初次使用中学习.本文主要目的让你可以通过cygwin使用基 ...

  8. Unity技术面试题

    一:什么是协同程序?答:在主线程运行时同时开启另一段逻辑处理,来协助当前程序的执行.换句话说,开启协程就是开启一个可以与程序并行的逻辑.可以用来控制运动.序列以及对象的行为. 二:Unity3d中的碰 ...

  9. Nginx集群

    转自:http://hi.baidu.com/xingyuanju/blog/item/779a2a23b7ebb749935807f1.html http://hi.baidu.com/dianhu ...

  10. HTTP 错误 404.2 - Not Found 由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面。

    IIS的根节点->右侧"ISAPI和CGI限制"->把禁止的DotNet版本项设置为允许,即可.