【HDU 4300 Clairewd’s message】

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

 

【翻译】给两个串第一个串是翻译表(密文可以通过翻译表翻译成明文),第二个串是由密文+明文组成,前面是密文(完整的),后面是明文(未必完整),问能不能把第二个串补全,输出最短的一种可能。

 

题解:

       ①如果将整个串转成明文拼到前面,那么其实就是找到最长前后缀,并且要合法。

     ②KMP 的 next    

 

#include<stdio.h>
#include<cstring>
#define go(i,a,b) for(int i=a;i<=b;i++)
const int N=1000003;char a[N],P[N<<1],Map[N];int T,m,f[N],n,j;
int main()
{	
	scanf("%d",&T);f[1]=f[0]=0;
	while(T--&&scanf("%s%s",a,P))
	{
		n=strlen(P);
		go(i,0,25)Map[a[i]-'a']='a'+i;		
		go(i,0,n-1)P[i+n]=P[i],P[i]=Map[P[i]-'a'];m=n<<1;
		go(i,1,m-1){j=f[i];while(j&&P[i]!=P[j])j=f[j];f[i+1]=P[i]==P[j]?j+1:0;}
		
		j=m;while(j&&j>n/2)j=f[j];	
		go(i,n,m-1-j)putchar(P[i]);
		go(i,0,n-1-j)putchar(P[i]);puts("");
	}
	return 0;
}//Paul_Guderian
posted @ 2017-10-23 07:56  小米狐  阅读(264)  评论(1编辑  收藏  举报
TOP