HDU - 4300 Clairewd’s message

Clairewd’s message

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7630    Accepted Submission(s): 2838


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
 

 

Author
BUPT
 

 

Source
 

 

Recommend
zhuyuanchen520

 

题目大意:

给一个明文密文的对应串,再给第二个串,至少前一半是密文,之后是明文(可能只有一部分,也可能全部都是)

最少补充几个字符可以补全字符串使之成为完整的密文-明文串。

做法:

字符串HASH

 

代码:

#include<iostream>
using namespace std;
#include<cstring>
#include<cstdio>
#include<cstdlib>
const int MAXN=210000; 
const unsigned long long seed=163;
typedef unsigned long long ull;
int n;
ull hashlist1[MAXN],hashlist2[MAXN];
ull p[MAXN];
char c[MAXN],s[MAXN],trans[MAXN],trans2[MAXN];
ull get(int l,int r,ull hashlist[]){
	return hashlist[r]-hashlist[l-1]*p[r-l+1];
}
char pp[MAXN];
void deal(){
	scanf("%s",pp);
	int lentrans=strlen(pp);
	for (int i=0;i<lentrans;i++) trans['a'+i]=pp[i],trans2[pp[i]]='a'+i;
	scanf("%s",c);
	int len=strlen(c);
	int beg=len/2+len%2;
	hashlist1[0]=0;
	hashlist2[0]=0;
	for (int i=1;i<=len;i++){
		hashlist1[i]=hashlist1[i-1]*seed+c[i-1]-'a'+1;
		hashlist2[i]=hashlist2[i-1]*seed+trans[c[i-1]]-'a'+1;
	}
	int ans=0;
	for (int i=beg;i<=len;i++){
		int l=len-i;
		ull s1=get(1,l,hashlist1);
		ull s2=get(len-l+1,len,hashlist2);
		if (s1==s2&&ans<l) ans=l; 
	}
	for (int i=0;i<len-ans;i++){
		printf("%c",c[i]);
	}
	for (int i=0;i<len-ans;i++){
		printf("%c",trans2[c[i]]);
	}
	putchar('\n');
}
int main(){
	int t;
	p[0]=1;
	for (int i=1;i<MAXN;i++){
		p[i]=p[i-1]*seed;
	}
	scanf("%d",&t);
	while(t--)	
		deal();
	return 0;
}

  

posted @ 2017-10-18 14:39  晓风微微  阅读(257)  评论(0编辑  收藏  举报