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);elseprintf("Impossible\n");}return 0;
}

Codeforces Gym 100015H Hidden Code(暴力)相关推荐

  1. Codeforces gym 100685 A. Ariel 暴力

    A. Ariel Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/A Des ...

  2. Codeforces Gym 101173 CERC 16 D BZOJ 4790 Dancing Disks

    Codeforces Gym 101173 CERC 16 D & BZOJ 4790 Dancing Disks 强烈安利这道构造题目,非常有意思. 这里用到的思想是归并排序! 多路归并排序 ...

  3. Codeforces Gym 101086 M ACPC Headquarters : AASTMT (Stairway to Heaven)

    Codeforces Gym 101086 M ACPC Headquarters : AASTMT (Stairway to Heaven) 题目来源: Codeforces 题意: 给出一些比赛, ...

  4. [Codeforces Gym 101651/100725B] Banal Tickets

    Codeforces Gym 100725 题解: 先分两种情况, 积为000与积非0" role="presentation" style="position ...

  5. Codeforces Gym 100513G G. FacePalm Accounting 暴力

    G. FacePalm Accounting Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513 ...

  6. Codeforces Gym 101473D Folding Machine (暴力搜索)

    题目连接: http://codeforces.com/gym/101473/attachments 原先以为按照这个复杂度还要剪一下支,没想到暴力dfs完全可以通过. 代码: #include &l ...

  7. Codeforces Gym 100286J Javanese Cryptoanalysis 傻逼暴力

    原题地址:http://codeforces.com/gym/100286/attachments/download/2013/20082009-acmicpc-northeastern-europe ...

  8. Codeforces Gym 100418K Cards 暴力打表

    Cards Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  9. code Gym 100500D T-shirts(暴力)

    因为只能买一次,暴力枚举一下买的衣服的大小. #include<cstdio> #include<map> #include<algorithm>using nam ...

  10. Codeforces Gym 100203G G - Good elements 暴力

    G - Good elements Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest ...

最新文章

  1. windows扫描域内端口
  2. 三角形中惊现叛徒!自己胖的像个球,却能成就世界上最快的赛车引擎......
  3. 【CF - 699C】 Vacations (日程安排 dp)
  4. Android环境配置
  5. [机器学习-回归算法]Sklearn之线性回归实战
  6. php电子邮箱表单,带邮件发送功能的表单(form.php)
  7. Spark createDirectStream 维护 Kafka offset(Scala)
  8. d语言 c++ 混合编程,C++,D语言,Python语言一次模拟合作开发
  9. Java加密算法库BouncyCastle
  10. 2022短视频去水印小程序源码+支持批量解析
  11. 再谈过时且脆弱的TCP长肥管道三宗罪!
  12. 一维码识别技术与二维码识别技术
  13. win10:打印机无故脱机、打印机缺纸故障处理
  14. mcnpf5输出结果_MCNP学习笔记-计数卡F6
  15. python excel转csv两列互换,python excel转换csv代码实例
  16. 榜样访谈| 黄思怡:高校俱乐部提供了更大的平台
  17. Arduino使用SK6812(WS2812) 全彩RGB模块/ArduinoC、Mixly/Scartch
  18. 原生js + 后端nodejs实现邮箱信封表白程序
  19. 基于jQuery的QQ表情编辑器
  20. 新东方手机摄影大赛自动投票脚本

热门文章

  1. 华科计算机学院专业课,华中科技大学计算机专业课程表.xls
  2. linux下cat导出日志,Linux命令:cat
  3. npm安装依赖报错——npm ERR gyp verb cli的解决方法 Node Sass version 7.0.1 is incompatible with ^4.0.0. 因为在此系统上禁止运
  4. ACM779-兰州烧饼
  5. go 学习笔记之10 分钟简要理解 go 语言闭包技术
  6. vue项目中页面打印插件(去除页眉页脚)
  7. 用Python做的小游戏合集来咯~自行开发一个星际争霸小游戏~
  8. Prometheus最佳实践 Summary和Histogram
  9. 云上PDF怎么删除页眉页脚_有办法了!批量删除多个Word页眉页脚
  10. 【C++】C++中头文件使用双引号与书名号的区别