Clairewd’s message

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

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
 
题目大意:给两个字符串,第一个为字母转化表(第一个字母a对应字母s[0]),第二个为密文+明文的字符串(对应转化的)。不过其中明文部分可能丢失一部分(包括所有),把他补全再输出结果。
分析:把第一个字符串的后半部分映射(明文——密文),KMP算法求他的失配函数,求出他的最大后缀(小于等于len/2)。
 #include<iostream>
#include<cstring>
#include<cstdio>
using namespace std; int mp[],f[];
char str[],s1[],s2[]; void HalfChange()
{
int i,len=strlen(s1);
for(i=len/;i<len;i++)
s1[i]=str[s1[i]-'a'];
} void getFail()
{
int i,j,len=strlen(s1);
f[]=f[]=;
for(i=;i<len;i++)
{
j=f[i];
while(j && s1[i]!=s1[j]) j=f[j];
f[i+]=(s1[i]==s1[j]?j+:);
}
} int main()
{
int t,i,len,k;
scanf("%d",&t);
while(t--)
{
scanf("%s %s",str,s1);
for(i=;i<;i++) mp[str[i]-'a']=i;
strcpy(s2,s1);
len=strlen(s1);
HalfChange();//把s1后半部分由明文转成密文
getFail();//s1求失配函数
k=f[len];
while(k > len/) k=f[k];
for(i=;i<len-k;i++) printf("%c",s2[i]);
for(i=;i<len-k;i++) printf("%c",mp[s2[i]-'a']+'a');
printf("\n");
}
return ;
}
 

hdu 4300 kmp算法扩展的更多相关文章

  1. hdu 3613 KMP算法扩展

    Best Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  2. hdu 1711 KMP算法模板题

    题意:给你两个串,问你第二个串是从第一个串的什么位置開始全然匹配的? kmp裸题,复杂度O(n+m). 当一个字符串以0为起始下标时.next[i]能够描写叙述为"不为自身的最大首尾反复子串 ...

  3. hdu 1686 KMP算法

    题意: 求子串w在T中出现的次数. kmp算法详解:http://www.cnblogs.com/XDJjy/p/3871045.html #include <iostream> #inc ...

  4. hdu 4300(kmp)

    题意:说实话这个题的题意还真的挺难懂的,我开始看了好久都没看懂,后来百度了下题意才弄懂了,这题的意思就是首先有一个字母的转换表,就是输入的第一行的字符串,就是'a'转成第一个字母,'b'转成转换表的第 ...

  5. HDU 2594 kmp算法变形

    Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  6. KMP算法模板&&扩展

    很不错的学习链接:https://blog.csdn.net/v_july_v/article/details/7041827 具体思路就看上面的链接就行了,这里只放几个常用的模板 问题描述: 给出字 ...

  7. HDU 4333 Revolving Digits 扩展KMP

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4333 题意:给以数字字符串,移动最后若干位到最前边,统计得到的数字有多少比原来大,有多少和原来同样,有多少 ...

  8. 扩展KMP算法

    一 问题定义 给定母串S和子串T,定义n为母串S的长度,m为子串T的长度,suffix[i]为第i个字符开始的母串S的后缀子串,extend[i]为suffix[i]与字串T的最长公共前缀长度.求出所 ...

  9. 扩展KMP算法小记

    参考来自<拓展kmp算法总结>:http://blog.csdn.net/dyx404514/article/details/41831947 扩展KMP解决的问题: 定义母串S和子串T, ...

随机推荐

  1. opensue "Have a lot of fun..."的出处

    每次登陆opensuse都会出现“Have a lot of fun...”,觉得奇怪. 通过搜索发现在这是/etc/motd文件中配置的. MOTD(5)                       ...

  2. Python基础篇 -- 集合

    set集合 set 中的元素是不重复的,无序的 里面的元素必须是可hash的,(int str tuple bool) set 就是dict 类型的数据,但是不保存value 只保存 key set集 ...

  3. 03_5_static关键字

    03_5_static关键字 1. static关键字 在类中,用static声明的成员变量为静态成员变量,它为该类的公用 变量,在第一次使用时被初始化,对于该类的所有对象来说,static成员变量只 ...

  4. rest_framework之status HTTP状态码

    Django Rest Framework有一个status.py的文件 通常在我们Django视图(views)中,HTTP状态码使用的是纯数字,像400,404,200,304等,并不是那么很好理 ...

  5. PHP数组函数 array_multisort() ----对多个数组或多维数组进行排序

    PHP中array_multisort可以用来一次对多个数组进行排序,或者根据某一维或多维对多维数组进行排序. 关联(string)键名保持不变,但数字键名会被重新索引. 输入数组被当成一个表的列并以 ...

  6. 【linux】【指令集】查看是否打开selinux

    > getenforce selinux相关原理资料参考 <鸟哥的linux私房菜>  http://cn.linux.vbird.org/linux_server/0210netw ...

  7. Thinkphp5 获取执行的sql语句

    获取最后执行的sql语句 $str_order_action = db('order_action')->getLastSql(); //获取最后执行的sql语句 获取执行的sql语句 $ord ...

  8. C# NotifyIcon 托盘控件

    右下角以图标形式显示,双击图标显示界面,最小化界面时不显示在任务栏. 第一步:在界面上添加NotifyIcon控件. 第二步:设置notifyIcon1的属性,添加图标,将Visible设为true. ...

  9. 使用TensorFlow的卷积神经网络识别手写数字(1)-预处理篇

    功能: 将文件夹下的20*20像素黑白图片,根据重心位置绘制到28*28图片上,然后保存.经过预处理的图片有利于数字的准确识别.参见MNIST对图片的要求. 此处可下载已处理好的图片: https:/ ...

  10. Linux学习-进程管理

    为什么进程管理这么重要呢? 这是因为: 首先,我们在操作系统时的各项工作其实都是经过某个 PID 来达成的 (包括你的 bash 环境), 因此,能不能进行某项工作,就与该进程的权限有关了. 再来,如 ...