Codeforces Gym 100015H Hidden Code(暴力)

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


【思路】

明文和密码长度都相同,而要求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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您提供的链接是Codeforces的一个问题,问题编号为104377。Codeforces是一个知名的在线编程竞赛平台,经常举办各种编程比赛和训练。GymCodeforces的一个扩展包,用于组织私人比赛和训练。您提供的链接指向了一个问题的页面,但具体的问题内容和描述无法通过链接获取。如果您有具体的问题或需要了解关于Codeforces Gym的更多信息,请提供更详细的信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [http://codeforces.com/gym/100623/attachments E题](https://blog.csdn.net/weixin_30820077/article/details/99723867)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [http://codeforces.com/gym/100623/attachments H题](https://blog.csdn.net/weixin_38166726/article/details/99723856)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [CodeforcesPP:Codeforces扩展包](https://download.csdn.net/download/weixin_42101164/18409501)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值