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到z加密后的字母。再给一个字符串,为全部的密文+原文的一部分(也可能是全部)。也就是说这个字符串把密文和原文连在一起了,还不全,要求补齐串使满足全部密文+全部原文,并且这个串尽量短。

思路:由于密文一定全给出了,所以暴力的做法就是从中间开始向后检测,当后面的串加密后与前面串的对应前缀吻合,破译就成功了。

   想到这就有点熟悉.....主串后缀和模式串前缀.......可以用拓展kmp了。感觉其实把原串再加密或者解密都行,下面的代码是解密,然后得到一个前面原文后面无所谓是什么的串,作为模式串和原串跑一次拓展kmp,得到extend数组,从前往后找满足条件的ex,找到就跳出。条件为前面串长度大于等于ex(原串密文一定比原文长),并且ex和这个长度相加等于主串长(这样才是后缀)。

代码:

#include<bits/stdc++.h>
#define FastIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define repp(i,a,b) for(ll i=a;i<=b;i++)
#define rep1(i,a,b) for(ll i=a;i>=b;i--)
#define mem(gv) memset(gv,0,sizeof(gv))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define QAQ 0
#define miaojie1
#ifdef miaojie
#define dbg(args...) do {cout << #args << " : "; err(args);} while (0)
#else
#define dbg(...)
#endif
void err() {std::cout << std::endl;}
template<typename T, typename...Args>
void err(T a, Args...args){std::cout << a << ' '; err(args...);} using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pLL;
const int mod=1e9+;
const int maxn=1e6+; int nxt[maxn],ex[maxn];
void GETNEXT(char *str)
{
int i=,j,po,len=strlen(str);
nxt[]=len;
while(str[i]==str[i+]&&i+<len)
i++;
nxt[]=i;
po=;
for(i=;i<len;i++)
{
if(nxt[i-po]+i<nxt[po]+po)
nxt[i]=nxt[i-po];
else
{
j=nxt[po]+po-i;
if(j<)j=;
while(i+j<len&&str[j]==str[j+i])
j++;
nxt[i]=j;
po=i;
}
}
} void EXKMP(char *s1,char *s2)
{
int i=,j,po,len=strlen(s1),l2=strlen(s2);
GETNEXT(s2);
while(s1[i]==s2[i]&&i<l2&&i<len)
i++;
ex[]=i;
po=;
for(i=;i<len;i++)
{
if(nxt[i-po]+i<ex[po]+po)
ex[i]=nxt[i-po];
else
{
j=ex[po]+po-i;
if(j<)j=;
while(i+j<len&&j<l2&&s1[j+i]==s2[j])
j++;
ex[i]=j;
po=i;
}
}
} int T;
char s1[maxn],s2[maxn],h[],ha[]; int main(){
scanf("%d",&T);
while(T--){
mem(s2); mem(h); mem(ha);
scanf("%s%s",h,s1);
int l1=strlen(s1);
rep(i,,){
ha[h[i]]=i+;
}
rep(i,,l1){
s2[i]=ha[s1[i]];
}
EXKMP(s1,s2);
int st=l1;
rep(i,,l1){
if(i+ex[i]==l1 && i>=ex[i]){
st=i;
break;
}
}
rep(i,,st){
s2[i]=s1[i];
s2[i+st]=ha[s1[i]];
}
printf("%s\n",s2);
}
return QAQ;
}

HDU4300 Clairewd’s message(拓展kmp)的更多相关文章

  1. HDU 4300 Clairewd's message ( 拓展KMP )

    题意 : 给你一个包含26个小写字母的明文密文转换信息字符串str,第一个表示'a'对应的密文是str[0].'b'对应str[1]……以此类推.接下来一行给你一个另一个字符串,这个字符串由密文+明文 ...

  2. hdu4300 Clairewd’s message 扩展KMP

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

  3. hdu------(4300)Clairewd’s message(kmp)

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

  4. HDU-4300 Clairewd’s message

    http://acm.hdu.edu.cn/showproblem.php?pid=4300 很难懂题意.... Clairewd’s message Time Limit: 2000/1000 MS ...

  5. hdu4300 Clairewd’s message【next数组应用】

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

  6. hdu4300 Clairewd’s message

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=4300 题目: Clairewd’s message Time Limit: 2000/1000 MS (J ...

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

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

  8. hdu 4300 Clairewd’s message(扩展kmp)

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

  9. hdu 4300 Clairewd’s message(kmp/扩展kmp)

    题意:真难懂.. 给出26个英文字母的加密表,明文中的'a'会转为加密表中的第一个字母,'b'转为第二个,...依次类推. 然后第二行是一个字符串(str1),形式是密文+明文,其中密文一定完整,而明 ...

随机推荐

  1. Java中手动提交事务

    项目中遇到一个问题,就是在程序的执行过程中需要不断地更新某个信息,但是在springmvc中好像是默认不可以的,那么就需要手动提交 // 从spring容器对象中获取DataSourceTransac ...

  2. GCC在C语言中内嵌汇编 asm __volatile__

    2012-11-26 22:20 17958人阅读 评论(2) 收藏 举报  分类: linux(59)  架构管理(24)  C/C++(59)  目录(?)[+] 在内嵌汇编中,可以将C语言表达式 ...

  3. oppo手机怎么打开USB调试模式

    OPPO手机USB调试的设置方法:1.ColorOS 3.0版本,进入设置--关于手机,连续点击版本号直到出现“您已处于开发者选项”,再进入设置--其他设置--开发者选项--USB调试进行设置: 2. ...

  4. BestCoder Round #73 (div.2)1002/hdoj5631

    题意: 给出一张 nnn 个点 n+1n+1n+1 条边的无向图,你可以选择一些边(至少一条)删除. 分析: 一张n个点图,至少n-1条边才能保证联通 所以可以知道每次可以删去1条边或者两条边 一开始 ...

  5. maven构建web项目,cannot be cast to javax.servlet.Servlet

    今天开发web的时候,需要用到servlet-api,于是在pom.xml中添加依赖 <dependency> <groupId>javax.servlet</group ...

  6. robotframework自动化系列:登陆操作

    robotframework自动化系统:登录 robotframework对于编程能力比较弱的测试人员而言,真的是雪中送炭!我们可以使用robotframework根据之前完成的测试用例,一步步完善自 ...

  7. jQuery笔记之工具方法—Ajax 优化回调地狱

    在上一篇文我们说到了回调地狱不好的地方,今天我们看看怎么来优化它,让它可以运用到实际开发中. 什么是回调地狱?回调地狱就是一个函数里面嵌套了所有功能函数,然后缩略图形成一个三角形. 这样的代码可复用性 ...

  8. 因磁盘空间不足导致HDFS的NameNode进入安全模式问题记录

    因磁盘空间不足导致HDFS的NameNode进入安全模式问题记录,调用API上传及下载文件时报如下错误信息: org.apache.hadoop.ipc.RemoteException(org.apa ...

  9. mysql replication错误常见处理

    大部分的错误,都是日志错误 日志本身的错误 主日志和中继日志都可能出错,可以使用mysqlbinlog来读一下mysqlbinlog mysql-bin.000007>/dev/null ##只 ...

  10. qconshanghai2015

    http://2015.qconshanghai.com/schedule 大会日程 2015年10月15日 星期四 08:30 开场致辞   地点 光大宴会厅 专题 主题演讲 数据分析与移动开发工具 ...