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

【翻译】给两个串第一个串是翻译表(密文可以通过翻译表翻译成明文),第二个串是由密文+明文组成,前面是密文(完整的),后面是明文(未必完整),问能不能把第二个串补全,输出最短的一种可能。

题解:

①如果将整个串转成明文拼到前面,那么其实就是找到最长前后缀,并且要合法。

     ②KMP 的 next

#include<stdio.h>
#include<cstring>
#define go(i,a,b) for(int i=a;i<=b;i++)
const int N=1000003;char a[N],P[N<<1],Map[N];int T,m,f[N],n,j;
int main()
{
scanf("%d",&T);f[1]=f[0]=0;
while(T--&&scanf("%s%s",a,P))
{
n=strlen(P);
go(i,0,25)Map[a[i]-'a']='a'+i;
go(i,0,n-1)P[i+n]=P[i],P[i]=Map[P[i]-'a'];m=n<<1;
go(i,1,m-1){j=f[i];while(j&&P[i]!=P[j])j=f[j];f[i+1]=P[i]==P[j]?j+1:0;} j=m;while(j&&j>n/2)j=f[j];
go(i,n,m-1-j)putchar(P[i]);
go(i,0,n-1-j)putchar(P[i]);puts("");
}
return 0;
}//Paul_Guderian

最新文章

  1. 第二天 ci执行流程
  2. Entity Framework Code First实体对象变动跟踪
  3. Alpha阶段第二次Scrum Meeting
  4. Effective Java 26 Favor generic types
  5. Effective Java 69 Prefer concurrency utilities to wait and notify
  6. linux安装apache的纠结过程
  7. UIButton 长按点击 背景改变效果
  8. 沈逸老师PHP魔鬼特训笔记(7)--我叫什么名字
  9. 你听说过PHP 的面向方面编程吗?
  10. C++ ASCII 码的一些问题
  11. js+画曲线和圆 并限制圆的渲染范围
  12. web 文件下载(.shp)
  13. java基础-04泛型
  14. java 移动开发获取多级下拉框json数据的类和mobile-select-area插件
  15. 关于linux中的上下文切换
  16. 08_Flume_Selector实践
  17. MacOS Docker安装
  18. ORM 关键
  19. ssm框架中的乱码问题的解决
  20. 优化你的java代码性能

热门文章

  1. PHP入门笔记--基础语法一
  2. 文件 I/O字节流
  3. R语言学习笔记(十):零碎知识点(21-25)
  4. 洛谷(P1006 传纸条)
  5. 初步学习pg_control文件之九
  6. JavaScript---设计模式简介
  7. Android ANR 分析
  8. 清除远程桌面连接记录和SQLSERVER 连接记录的办法
  9. html5学得好不好,看掌握多少标签
  10. Qt Qpushbutton美化问题