思路:extend[i]表示原串以第i開始与模式串的前缀的最长匹配。经过O(n)的枚举,我们能够得到,若extend[i]+i=len且i>=extend[i]时,表示t即为该点之前的串,c即为该点之前的str串,最后输出就可以。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
const int N=100010; char s[N],t[N];
char c[N];
int next[N],extand[N];
void getNext(int lt)
{
next[0]=lt;
int a=0;
while(a<lt-1&&t[a+1]==t[a]) a++;
next[1]=a;
a=1;
for(int k=2;k<lt;k++)
{
int p=a+next[a]-1;
int L=next[k-a];
if(k-1+L>=p)
{
int j=(p-k+1)>0? (p-k+1):0;
while(j+k<lt&&t[k+j]==t[0+j]) j++;
next[k]=j;
a=k;
}
else
next[k]=L;
}
} void getExtand(int ls,int lt)
{
getNext(lt);
int a=0;
int minLen=min(ls,lt);
while(a<minLen&&t[a]==c[a]) a++;
extand[0]=a;
a=0;
for(int k=1;k<ls;k++)
{
int p=a+extand[a]-1;
int L=next[k-a];
if(k-1+L>=p)
{
int j=(p-k+1)>0?(p-k+1):0;
while(j+k<ls&&j<lt&&t[j+k]==c[j]) j++;
extand[k]=j;
a=k;
}
else
extand[k]=L;
}
} int main()
{
int T;
cin>>T;
while(T--)
{
cin>>s>>t;
map<char,char> mmap;
int lt=strlen(t);
int ls=strlen(s);
for(int i=0;i<ls;i++)
mmap[s[i]]='a'+i;
for(int i=0;i<lt;i++)
c[i]=mmap[t[i]];
getExtand(lt,lt);
int i;
for(i=0;i<lt;i++)
{
if(i+extand[i]>=lt&&i>=extand[i])
break;
}
for(int j=0;j<i;j++) cout<<t[j];
for(int j=0;j<i;j++) cout<<c[j];
cout<<endl;
}
return 0; }

HDU 4300 Clairewd’s message(扩展KMP)的更多相关文章

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

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

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

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

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

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

  4. hdu 4300 Clairewd’s message KMP应用

    Clairewd’s message 题意:先一个转换表S,表示第i个拉丁字母转换为s[i],即a -> s[1];(a为明文,s[i]为密文).之后给你一串长度为n<= 100000的前 ...

  5. HDU - 4300 Clairewd’s message (拓展kmp)

    HDU - 4300 题意:这个题目好难读懂,,先给你一个字母的转换表,然后给你一个字符串密文+明文,密文一定是全的,但明文不一定是全的,求最短的密文和解密后的明文: 题解:由于密文一定是全的,所以他 ...

  6. hdu4300 Clairewd’s message 扩展KMP

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

  7. hdu 4300 Clairewd’s message 字符串哈希

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

  8. hdu 4300 Clairewd’s message(具体解释,扩展KMP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4300 Problem Description Clairewd is a member of FBI. ...

  9. HDU 4300 Clairewd’s message(扩展KMP)题解

    题意:先给你一个密码本,再给你一串字符串,字符串前面是密文,后面是明文(明文可能不完成整),也就是说这个字符串由一个完整的密文和可能不完整的该密文的明文组成,要你找出最短的密文+明文. 思路:我们把字 ...

随机推荐

  1. Extjs4.0.7 实现Grid的嵌套

    网上相关资料非常少,我看过的大多是Extjs 3.0 急以前版本的解决方案. 比如:http://mikhailstadnik.com/ext/examples/nested-grid.htm  (E ...

  2. apache +php +php curl 模块设置

    2.2 linux 下面 2.2.1 web服务器安装 1目前采用的web服务器是apache2,在ubuntu 下安装 apt-getupdate apt-get installapache2 测试 ...

  3. OUI-67076 : OracleHomeInventory was not able to create a lock file&quot; in Unix

    Symptoms The command "opatch lsinventory" reports the error: OUI-67076:OracleHomeInventory ...

  4. LAMBDA表达式常用 (全)

    这里主要是将数据库中的常用操作用LAMBDA表达式重新表示了下,用法不多,但相对较常用,等有时间了还会扩展,并将查询语句及LINQ到时也一并重新整理下: 1.select语句:books.Select ...

  5. Golang在Linux环境下的POSIX风格socket编程

    这里给出一个服务端和client,服务端能够接受多个连接,而且利用Go的杀手特性go和channel来替代select进行数据的接收. 服务端: package main import ( " ...

  6. Golang 1.3 发布时间。最终找到地方下载。

    golang 1.3 已发布 但golang.org官方网站被封锁不能下载. 最终找到一个镜像站点. http://golang.so/ http://tip.golang.so/ golang中国的 ...

  7. leetcode dfs Validate Binary Search Tree

    Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...

  8. C语言中的函数指针

    C语言中的函数指针 函数指针的概念:   函数指针是一个指向位于代码段的函数代码的指针. 函数指针的使用: #include<stdio.h> typedef struct (*fun_t ...

  9. 文章3说话 微信商城云server创建后台

    一个.   应用server资源              想要进行微信开发.少不了后台server端程序的开发,那么我们首先就要申请server资源.眼下有非常多云server可选,比方新浪的sae ...

  10. [ACM] HDU 3395 Special Fish (最大重量二分图匹配,KM算法)

    Special Fish Problem Description There is a kind of special fish in the East Lake where is closed to ...