Clairewd’s message

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2398    Accepted Submission(s): 942

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个字符表对应明文中a,b,c,d....z可以转换第一个,第二个...第26个字符变成密文,

第二组字符串是给定的密文+明文,明文可能不完整(缺失或没有),叫你补完且整个密文+明文是最短的

如果有多种明文则取最大的明文

分析:因为可能有多种明文且密文>=明文,所以将字符串的前一半(一定是密文)转换成明文和后一部分进行匹配,如果从后面某个位置能匹配到末尾,则表示那一部分是明文,然后进行补充完整输出即可//这里根据key得到密文转换成明文的钥匙b,不用明文转换成密文匹配是因为明文可能有多种,后面一部分不一定是明文,根据b将密文转换成明文(一定是最大的明文,因为b里面转换成的明文字符一定是最大的,比如key:aa....,则b:b.....)

EKMP:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 999999999
using namespace std; const int MAX=100000+10;
char s1[MAX],s2[MAX],a[27],b[27];
int next[MAX]; void get_next(char *a,int len){
int k=0,i=1;
next[0]=len;//本题无作用
while(k+1<len && a[k] == a[k+1])++k;
next[1]=k;
k=1;
while(++i<len){
int maxr=k+next[k]-1;
next[i]=min(next[i-k],max(maxr-i+1,0));
while(i+next[i]<len && a[next[i]] == a[i+next[i]])++next[i];
if(i+next[i]>k+next[k])k=i;
}
} int main(){
int T,k;
cin>>T;
while(T--){
cin>>a>>s1;
for(int i=0;i<26;++i)b[a[i]-'a']=i+'a';
int len=strlen(s1);
for(int i=0;i<(len+1)/2;++i)s2[i]=b[s1[i]-'a'];//将密文转换为明文,密文长度>=明文长度
for(int i=(len+1)/2;i<=len;++i)s2[i]=s1[i];
get_next(s2,len);
for(k=(len+1)/2;k<len;++k){
if(next[k] == len-k)break;
}
cout<<s1;
for(int i=len-k;i<k;++i)cout<<b[s1[i]-'a'];
cout<<endl;
}
return 0;
}

KMP:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 999999999
using namespace std; const int MAX=100000+10;
char s1[MAX],s2[MAX],a[27],b[27];
int next[MAX]; void get_next(char *a,int len){
int i=-1,j=0;
next[0]=-1;
while(j<len){
if(i == -1 || a[i] == a[j])next[++j]=++i;
else i=next[i];
}
} int main(){
int T;
cin>>T;
while(T--){
cin>>a>>s1;
for(int i=0;i<26;++i)b[a[i]-'a']=i+'a';
int len=strlen(s1),k=len;
for(int i=0;i<(len+1)/2;++i)s2[i]=b[s1[i]-'a'];//将密文转换为明文,密文长度>=明文长度
for(int i=(len+1)/2;i<=len;++i)s2[i]=s1[i];
get_next(s2,len);
while(next[k]>len/2)k=next[k];
cout<<s1;
for(int i=next[k];i<len-next[k];++i)cout<<b[s1[i]-'a'];
cout<<endl;
}
return 0;
}

hdu4300之KMP&&EKMP的更多相关文章

  1. 复习下KMP&e-KMP

    KMP算法的核心思想是next数组. 接下来,我来谈谈我对KMP数组的理解. KMP算法是用来匹配有多少相同字串的一种算法. 1.next数组记录前缀与后缀相等的位置,然后跳到这. 2.数组即记录后缀 ...

  2. (25+4/25+4)复健-KMP/EKMP/manache/Trie

    (29/29) 3.23已完成  1.KMP int Next[maxn]; void prekmp(char* x,int len){ ,suf=; Next[]=-; while(suf<l ...

  3. hdu-4300(kmp或者拓展kmp)

    题意:乱七八糟说了一大堆,就是先给你一个长度26的字符串,对应了abcd....xyz,这是一个密码表.然后给你一个字符串,这个字符串是不完整的(完整的应该是前半部分是加密的,后半部分是解密了的),然 ...

  4. kmp基础 ekmp

    +]; int lenp,lens; +];//可以是char 也可以是string +]; void getnext() { nex[]=-; ,j=; ) { ||p[j]==p[k]) nex[ ...

  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(kmp)

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

  7. kuangbin专题十六 KMP&&扩展KMP HDU3613 Best Reward(前缀和+manacher or ekmp)

    After an uphill battle, General Li won a great victory. Now the head of state decide to reward him w ...

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

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

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

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

随机推荐

  1. 获取select赋值

    <select class="sel-ul-add" id="xuanzhe"> <option>A</option> &l ...

  2. Linux中的IO复用接口简介(文件监视?)

    I/O复用是Linux中的I/O模型之一.所谓I/O复用,指的是进程预先告诉内核,使得内核一旦发现进程指定的一个或多个I/O条件就绪,就通知进程进行处理,从而不会在单个I/O上导致阻塞. 在Linux ...

  3. HDU 5823 color II(FWT)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5823 [题目大意] 定义一张无向图的价值:给每个节点染色使得每条边连接的两个节点颜色不相同的最少颜 ...

  4. BZOJ 1635: [Usaco2007 Jan]Tallest Cow 最高的牛

    题目 1635: [Usaco2007 Jan]Tallest Cow 最高的牛 Time Limit: 5 Sec  Memory Limit: 64 MB Description FJ's N ( ...

  5. python中变量命名

    一 综述:  二 全局变量(包含函数和类): (1)正常变量x: *通过module.x能够使用. *通过from module import *能够使用. (2)以"_"开头变量 ...

  6. android解决内存溢出的问题(没有从根本上解决)

    Android游戏虚拟机算法JNI 尽量不要使用setImageBitmap或setImageResource或BitmapFactory.decodeResource来设置一张大图,因为这些函数在完 ...

  7. chrome扩展小试

    chorme 扩展 小试 官方文档 http://developer.chrome.com/extensions 非官方中文文档 http://docs.lmk123.com/getstarted.h ...

  8. asp.net 连接access数据库方法

    在 Web.Config 中配置 Access 数据库驱动和数据库文件名称. 请看代码 <appSettings> <add key="DBDriver" val ...

  9. ECharts的使用相关参考---转

    ECharts图表组件初级入门之(一):如何将ECharts引入至项目中的几种方式 http://www.stepday.com/topic/?801 ECharts图表初级入门篇:如何配置EChar ...

  10. [Jobdu] 题目1283:第一个只出现一次的字符

    题目描述: 在一个字符串(1<=字符串长度<=10000,全部由大写字母组成)中找到第一个只出现一次的字符. 输入: 输入有多组数据每一组输入一个字符串. 输出: 输出第一个只出现一次的字 ...