您的位置:首页 > 其它

Codeforces Gym 100015H Hidden Code(暴力)

2017-09-08 15:48 411 查看


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.

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.

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.

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

【思路】

明文和密码长度都相同,而要求key的长度其实也没必要比最长的明文和密码长,我们可以将所有的明文和密码按照长度排一下序,由长到短,然后从1到最长枚举key的长度,直接暴力检验就好了。

【代码】

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int MAXN=105;

struct text{
char plain[MAXN],cipher[MAXN];
int len;

bool operator<(const text &another)const
{
return len>another.len;
}

};

int n;
text mes[MAXN];

int main()
{
while(scanf("%d",&n)==1&&n!=0){
int maxlen=0;
for(int i=1;i<=n;i++){
scanf("%s %s",mes[i].plain,mes[i].cipher);
mes[i].len=(int)strlen(mes[i].plain);
maxlen=max(maxlen,mes[i].len);
}
sort(mes+1,mes+1+n);
char key[MAXN];
int keylen;
bool flag;
for(int k=1;k<=maxlen;k++){
for(int i=0;i<k;i++)
key[i]='A'+((26+mes[1].cipher[i]-mes[1].plain[i])%26);
key[k]='\0';
flag=true;
for(int i=1;i<=n;i++){
for(int index=0;index<mes[i].len;index++){
if(index>=k){
if((mes[i].plain[index]-'A'+mes[i].cipher[index-k]-'A')%26+'A'!=mes[i].cipher[index]){
flag=false;
break;
}
}
else{
if((mes[i].plain[index]-'A'+key[index]-'A')%26+'A'!=mes[i].cipher[index]){
flag=false;
break;
}
}
}
}
if(flag)break;
}
if(flag)
printf("%s\n",key);
else
printf("Impossible\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: