Clairewd’s message
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10376 Accepted Submission(s): 3709
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.
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.
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
题意:有一份文件,原文由密文和明文组成,前面一部分是密文,后面一部分是明文,密文可以通过转换规则(26个字母,每个字母变成它所在的下标的字母qwerty->abcdef)表示成明文,但那个人接到这个文件后不知道从哪里开始是明文,所以你要帮忙还原一下,
如果后面明文比密文少,你就将明文补全
所以这题的关键就是找到密文和明文交界的位置,
因为密文是完整的给出的,明文可能不完整,所以明文开始的位置一定是 >= len/2+len%2 的(数据是从0开始存,所以可以取等),因为如果长度为奇数个要从后一位开始。
对原文用哈希处理两次,第一次都按密文转换规则处理(p1),第二次按明文处理(p2),之后从中间位置开始,不断比较两者哈希值,当相等是就找到交界位置,若找不到,就说明只有密文
#include<iostream>
#include<string.h>
#include<string>
#include<math.h>
#define ull unsigned long long
using namespace std;
string str,s;
ull num[];
ull p1[],p2[],pow1[];//存储哈希值
ull n;
void init()//计算131的i次方
{
pow1[]=;
for(int i=;i<=;i++)
pow1[i]=pow1[i-]*;
}
void hs()
{
p1[]=num[str[]-'a'], p2[]=str[]-'a';
for(int i=;i<str.length();i++)
{
p1[i]=p1[i-]*+num[str[i]-'a'];//计算原文str的哈希值
p2[i]=p2[i-]*+(str[i]-'a');//计算明文的哈希值
}
}
ull get(ull pp[],ull l,ull r)//pow()函数会溢出
{
return pp[r]-pp[l-]*pow1[r-l+];
}
int main()
{
cin>>n;
init();
while(n--)
{
cin>>s;
cin>>str;
for(int i=;i<;i++)//先把转换规则表示成[0,25]个数字,输出结果的时候在转换回去
{
num[s[i]-'a']=i;
}
hs();
ull x1,x2,len,flag=;
len=str.length();
x1=len/+len%;//原文中第一个明文的下标
for(int i=x1;i<len;i++)//通过比较哈希值,找到原文的第一个明文下标
{
ull y1,y2;
x2=len--i;
y1=get(p1,,x2);
y2=get(p2,i,len-);
if(y1==y2)//密文与明文的哈希值相等
{
flag=;
x1=i;
break;
}
}
if(flag==)//原文都是密文
x1=len;
for(int i=;i<x1;i++)//输出密文
cout<<str[i];
for(int i=;i<x1;i++)//把密文翻译成明文输出
printf("%c",num[str[i]-'a']+'a');
cout<<endl;
}
}