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. k8s-资源指标API及自定义指标API-二十三

    一. 原先版本是用heapster来收集资源指标才能看,但是现在heapster要废弃了. 从k8s v1.8开始后,引入了新的功能,即把资源指标引入api: 在使用heapster时,获取资源指标是 ...

  2. HDU 2077 汉诺塔IV (递推)

    题意:... 析:由于能最后一个是特殊的,所以前n-1个都是不变的,只是减少了最后一个盘子的次数,所以根据上一个题的结论 答案就是dp[n-1] + 2. 上一题链接:http://www.cnblo ...

  3. HDOJ1584蜘蛛牌【DFS】

    10张牌,大的只能跟小的跑,可以针对每一个状态进行搜索,求一个最小的移动距离. 但是不会怎么遍历整个状态是硬伤? 因为只能大的跟着小的. 先把小的标记,去寻找大的点,最终一定是满足的吧. 比如先标记1 ...

  4. P5112 FZOUTSY

    传送门 没想到这题还这能用莫队--本来看看以为复杂度会挂的-- 预处理出每个字母开头往后\(k\)个的字符串的哈希值,然后大概就是那道小z的袜子了 而且据说这题的哈希得用自然溢出 //minamoto ...

  5. vue父组件调用子组件方法

    父组件: 代码 <sampleapplylinemodel ref="sampleapplylinemodel" @reLoad="_fetchRecords&qu ...

  6. 日志系统:一条sql更新语句是如何执行的?--Mysql45讲笔记记录 打卡day2

    下面是一个表的创建语句,这个表有一个主键id和一个整型字段c: create table t(id int primary key,c int); 如果要将 id = 2 这一行的值加 1,sql语句 ...

  7. Luogu P2114[NOI2014]起床困难综合症 【贪心/位运算】By cellur925

    题目传送门 所以NOI的题现在简单惹? 30分做法:枚举开始的权值,n²过掉. 100分做法:竟然是贪心qwq.因为我们的计算背景是二进制下,所以我们贪心地想让每一位都是1.我们现在需要解决的问题,就 ...

  8. 关于使用IQKeyBoardManager键盘还是被遮挡的问题解决方案

    今天在做一个登录界面的时候发现使用了IQKeyBoardManager键盘还是被遮挡,解决方案如下 解决方案一:在所有视图的最外层添加一个UIView作为容器即可,但在有导航栏的情况下导航栏会跟着向上 ...

  9. C# 中使用Image.FromFile(string path)后,提示该文件正在被另一进程使用XXX的问题

    C# 中使用Image.FromFile(string path)后,提示该文件正在被另一进程使用XXX的问题 C# 中使用Image.FromFile(string path)后,提示该文件正在被另 ...

  10. $ybt\ 【信息学奥赛一本通】题解目录$

    [信息学奥赛一本通]题解目录 $ \large -> OJ$ $ problem1000 $ \(Answer\) - > $ \large 1000$ $ problem1001 $ \ ...