题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4300

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
 
Author
BUPT
 
Source

题意:

比較难理解,给定两组字符串,第一组仅仅有26个字符表相应明文中a,b,c,d....z能够转换第1个,第2个...第26个字符变成密文,

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

PS:读了好几遍都不理解题意。用了翻译工具还是不能理解。最后还是百度了才知道题意!(毕竟英语太渣)。

思路:

把给出的不完整的明文加密文字符串所有转换一次。将转换后的做模式串,与原串进行kmp,记录返回的j值(j值代表的就是前缀和后缀的最长相等长度)。然后再与给出的明文加密文字符串比較一下。

代码例如以下:

//#pragma warning (disable:4786)
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <climits>
#include <ctype.h>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <deque>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
const double eps=1e-9;
const double pi=3.1415926535897932384626;
#define INF 1e18
//typedef long long LL;
//typedef __int64 LL;
const int MAXN = 100017; char strm[MAXN];//password表
char strz[MAXN];//不完整的密文和密文
char str[MAXN]; //password表转换后
char s[MAXN]; //密文和明文转换后
int next[MAXN]; void getnext( char T[], int len)
{
int i = 0, j = -1;
next[0] = -1;
while(i < len)
{
if(j == -1 || T[i] == T[j])
{
i++, j++;
next[i] = j;
}
else
j = next[j];
}
}
int KMP(int len1, int len2)
{
int i, j = 0;
if(len1%2 == 1)
{
i = len1/2+1;
}
else
i = len1/2;
while(i < len1 && j < len2)
{
if(j == -1 || strz[i] == s[j])
{
i++, j++;
}
else
j = next[j];
}
return j;//j值代表的就是前缀和后缀的最长相等长度
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s",strm);
for(int i = 0; i < 26; i++)
{
char ss = strm[i];
str[ss] = i;
}
scanf("%s",strz);
int lenz = strlen(strz);
for(int i = 0; i < lenz; i++)//将密文和明文依照password表转换
{
int tt = str[strz[i]];
s[i] = 'a' + tt;
}
int lens = strlen(s);
getnext(s, lens);
int j = KMP(lenz, lens);//得到密文的个数
if(j*2 == lenz)//前缀和后缀恰各占一半
{
printf("%s\n",strz);
}
else
{
int tt = lenz - j;
printf("%s",strz);
for(int i = j; i < tt; i++)//须要加入的明文
{
printf("%c",s[i]);
}
printf("\n");
}
}
return 0;
}

最新文章

  1. 【iOS】在Swift中使用JSONModel
  2. 在 node.js 的 express web 框架中自动注册路由
  3. Linux下使用Eclipse开发Hadoop应用程序
  4. TcxComboBox控件说明
  5. [纯小白学习OpenCV系列]官方例程00:世界观与方法论
  6. 浅谈PopupWindow弹出菜单
  7. Linux下通过JDBC连接Oracle,SqlServer和PostgreSQL
  8. 僵尸进程 图解 分布式 LINUX内核
  9. HDOJ 2081 手机短号
  10. 数据挖掘经典算法之KNN
  11. OpenRisc-48-or1200的SPRS模块分析
  12. centos ldap
  13. Oracle odi 数据表导出到文件
  14. ThinkPhp框架的数据库操作(查询)
  15. params修饰符
  16. cache 订单队列 - TP5
  17. java.lang.ClassCastException: oracle.sql.CLOB cannot be cast to oracle.sql.CLOB
  18. 【翻译】Ext JS最新技巧——2016-3-4
  19. [ZZ] [精彩盘点] TesterHome 社区 2018年 度精华帖
  20. 未能加载或程序集“XXXX,Version=0.0.0.0,Culter=neutral,PublicKeyToken=null”或它的某一个依赖项。试图加载格式不正确的程序。

热门文章

  1. ubuntu 设置Path 开机启动脚本
  2. C++基础——1.变量和基本类型(基于c++11)
  3. MyCAT+MySQL 搭建高可用企业级数据库集群——第3章 MyCat核心配置讲解
  4. match_parent, wrap_content, 和 fill_parent 区别联系
  5. 【bzoj4031】[HEOI2015]小Z的房间 矩阵树定理
  6. ubuntu服务器与本地文件传输
  7. php-超全局变量
  8. HDU——1233还是畅通工程(克鲁斯卡尔+优先队列)
  9. HDU——1395 2^x mod n = 1(取模运算法则)
  10. BZOJ 4816 [Sdoi2017]数字表格 ——莫比乌斯反演