Codeforces Gym 100015H Hidden Code 暴力

时间:2024-10-24 08:34:08

Hidden Code

题目连接:

http://codeforces.com/gym/100015/attachments

Description

It’s time to put your hacking skills to the test! You’ve been called upon to help crack enemy codes in the

current war on... something or another. Anyway, the point is that you have discovered the encryption

technique used by the enemy; it is quite simple, and proceeds as follows. Note that all strings contain only

uppercase letters of the alphabet.

  1. We are given a key K and a plaintext P which is encrypted character-by-character to produce a

    ciphertext C of the same length.

  2. If |K| is the length of the key K,thenthefirst |K| characters of C are obtained by adding the first

    |K| characters of P to the characters of K, where adding two letters means interpreting them as

    numbers (A =0, B = 1, and so on) and taking the sum modulo 26. That is, Ci =(Pi +Ki)mod26for

    i =1,..., |K|.If |K| > |P|, then the extra characters in K are ignored.

  3. The remaining characters of P, i.e. Pi for i> |K|, are encrypted using the previous ciphertext

    characters by Ci =(Pi + Ci!|K|)mod26for i = |K| +1,..., |P|.

    +

    As an example, consider the encryption of the string “STANFORD” using the key “ACM”:

STA NFORD

+ACM SVMFA

SVM FAAWD

Knowing this, you are well on your way to being able to read the enemy’s communications. Luckily, you

also have several pairs of plaintexts and ciphertexts which your team recovered, all of which are known to

be encrypted with the same key. Help find the key that the enemy is using.

Input

The input consists of multiple test cases. Each test case begins with a line containing a single integer N,

1 ! N ! 100, the number of plaintext and ciphertext pairs you will receive. The next N lines each contain

two strings P and C, the plaintext and ciphertext, respectively. P and C will contain only uppercase letters

(A-Z) and have the same length (at most 100 characters). The input terminates with a line with N =0. For

example:

Output

For each test case, print a single line that contains the shortest possible key or “Impossible”(quotesadded

for clarity) if no possible key could have produced all of the encryptions. For example, the correct output

for the sample input above would be:

Sample Input

1

A B

3

STANFORD SVMFAAWD

AVOWIENR AXAWFEJW

VAMRI VCYMK

3

ABCDEFGHIJKLMNOPQRSTUVWXYZ AAAAAAAAAAAAAAAAAAAAAAAAAA

Y Y

Z Z

2

A B

B A

0

Sample Output

B

ACM

AZYXWVUTSRQPONMLKJIHGFEDCB

Impossible

Hint

题意

给你一个串A,然后给你一个key串,加密的B串就是A+key串得到的

如果key串太长的话,就直接把后面的无视掉就好了

如果key串太短的话,就让加密的串来填充后面的串就好了

现在给你串A和串B,你需要输出一个最短的key串

题解:

数据范围只有100

所以直接瞎暴力就好了

暴力枚举串的长度,然后再暴力check就好了

代码

#include<bits/stdc++.h>
using namespace std; int check(string s1,string s2,int len)
{
if(len>s1.size())return 1;
vector<int>A1,A2;
for(int i=0;i<s1.size();i++)
{
A1.push_back((int)(s1[i]-'A'));
A2.push_back((int)(s2[i]-'A'));
}
for(int i=0;i<s1.size()-len;i++)
if((A1[i+len]+A2[i])%26!=A2[i+len])
return 0;
return 1;
}
string code(string s1,string s2,int len)
{
vector<int>A1,A2;
for(int i=0;i<s1.size();i++)
{
A1.push_back((int)(s1[i]-'A'));
A2.push_back((int)(s2[i]-'A'));
}
string ans;
for(int i=0;i<min(len,(int)s1.size());i++)
{
int num = A2[i]-A1[i];
if(num<0)num+=26;
ans.push_back((char)(num+'A'));
}
return ans;
}
struct node
{
string pre,ne;
int len;
}now[200];
bool cmp(node A,node B)
{
return A.len>B.len;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
if(n==0)break;
int len = 0;
for(int i=0;i<n;i++)
{
cin>>now[i].pre>>now[i].ne;
now[i].len = now[i].pre.size();
len = max(now[i].len,len);
}
sort(now,now+n,cmp);
int flag = 0;
for(int i=1;i<=len;i++)
{
int flag2 = 1;
for(int j=0;j<n;j++)
{
if(!check(now[j].pre,now[j].ne,i))
{
flag2 = 0;
break;
}
} if(flag2==0)
continue;
string dp = code(now[0].pre,now[0].ne,i);
for(int j=1;j<n;j++)
{
string ttt = code(now[j].pre,now[j].ne,i);
for(int k=0;k<ttt.size();k++)
{
if(ttt[k]!=dp[k])
{
flag2 = 0;
break;
}
}
if(flag2==0)
break;
}
if(flag2==0)
continue;
flag = 1;
cout<<dp<<endl;
break;
}
if(flag==0)printf("Impossible\n");
}
}