這道題目的題意太難理解了。
第一行給你26個字母的一段密文,對應明文是從a-z。
第二行給你前面是密文後面是明文的字符串,密文一定是完整的,但是明文可能沒有也可能都有。
讓你求最短的密文+明文。
例一:abcdab
最短密文:abcd,它對應的明文是abcd
所以最短密文+明文為abcdabcd
例二:qwertabcde
最短密文:qwert,它對應的明文是abcde
所以最短密文+明文為qwertabcde
有點難理解。
思路:明文的長度一定小於等於len/2,然後用後面的一半與對應的明文匹配
比如第一個例子:用dab與abcdab最大匹配為t=2,說明有兩個已經是明文,從t到len-t輸出後面的即為未顯示的明文,
AC代碼:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std;
typedef __int64 LL;
const int N=100090;
const LL II=1000000007;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
char word[N],yi[26],mi[26],xh[N];
int wlen,next[N];
void getnext(char *p)
{
int j=0,k=-1;
next[0]=-1;
while(j<wlen)
{
if(k==-1||p[j]==p[k])
{
j++; k++;
next[j]=k;
}
else
k=next[k];
}
}
int kmp(char *text,char *word)
{
int i=0,j=0,tlen=strlen(text);
while(i<tlen)
{
if(j==-1||text[i]==word[j])
j++,i++;
else
j=next[j];
}
return j;//返回母串後綴與模式串前綴最多有多少個重合
}
int main()
{
int i,j,T;
scanf("%d",&T);
while(T--)
{
scanf("%s%s",yi,word);
printf("%s",word);
for(i=0;i<26;i++)
mi[yi[i]-'a']=i+'a';//密文對應的明文
wlen=strlen(word);
strcpy(xh,word+(wlen+1)/2);
for(i=0;i<wlen;i++)
word[i]=mi[word[i]-'a'];
getnext(word);
int t=kmp(xh,word);//明文重疊的部分
int p=wlen-t;//還要輸出的
word[p]='\0';
printf("%s\n",word+t);//從t到p
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std;
typedef __int64 LL;
const int N=100090;
const LL II=1000000007;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
char word[N],yi[26],mi[26],xh[N];
int wlen,next[N];
void getnext(char *p)
{
int j=0,k=-1;
next[0]=-1;
while(j<wlen)
{
if(k==-1||p[j]==p[k])
{
j++; k++;
next[j]=k;
}
else
k=next[k];
}
}
int kmp(char *text,char *word)
{
int i=0,j=0,tlen=strlen(text);
while(i<tlen)
{
if(j==-1||text[i]==word[j])
j++,i++;
else
j=next[j];
}
return j;//返回母串後綴與模式串前綴最多有多少個重合
}
int main()
{
int i,j,T;
scanf("%d",&T);
while(T--)
{
scanf("%s%s",yi,word);
printf("%s",word);
for(i=0;i<26;i++)
mi[yi[i]-'a']=i+'a';//密文對應的明文
wlen=strlen(word);
strcpy(xh,word+(wlen+1)/2);
for(i=0;i<wlen;i++)
word[i]=mi[word[i]-'a'];
getnext(word);
int t=kmp(xh,word);//明文重疊的部分
int p=wlen-t;//還要輸出的
word[p]='\0';
printf("%s\n",word+t);//從t到p
}
return 0;
}