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

最新文章

  1. leetcode一些常用函数
  2. 使用count结合nvl函数时碰到的问题
  3. C++嵌入Python,以及两者混用
  4. JavaIO(03)字节流--OutputStream and InputStream
  5. Jquery实现图片轮播源码
  6. 多线程、多任务管理 简单demo
  7. SQL语句 计算某段时间工作日的天数(除了周六日)
  8. memcached的安装和linux下memcached服务自启动的配置
  9. 总线接口与计算机通信(一)I2C总线
  10. css字体设置
  11. SpringBoot之SOAP WebService
  12. 目标检测----ImageAI使用
  13. 神奇的Redis延迟
  14. 使用spring提供的ReflectionUtils简化项目中反射代码的复杂性
  15. Longest Repeating Character Replacement
  16. Git6:Git简单远程仓库部署
  17. 用ADO操作数据库的方法步骤
  18. log4j相关配置
  19. jzoj3865[JSOI2014]士兵部署
  20. mac 添加环境变量

热门文章

  1. WINDOWS-基础:LPTSTR
  2. Java获取yml里面的配置
  3. Mybatis学习记录(3)
  4. HTML5&lt;article&gt;元素
  5. vmware虚拟机安装Windows 7后虚拟机自动挂起
  6. Unity基础-发布
  7. linux 上安装配置l2tp的客户端
  8. 思维水题:UVa512-Spreadsheet Tracking
  9. Ubuntu添加环境变量
  10. github FATAL:unable to access &#39;https://github.com/...: Failed to connect to github.com:443; No error