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.
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
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");
}
}
Codeforces Gym 100015H Hidden Code 暴力的更多相关文章
- Codeforces gym 100685 A. Ariel 暴力
A. ArielTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/A Desc ...
- Codeforces Gym 100637G G. #TheDress 暴力
G. #TheDress Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100637/problem/G ...
- Codeforces Gym 100203G Good elements 暴力乱搞
原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 考虑暴力的复杂度是O(n^3),所以 ...
- Codeforces Gym 101190M Mole Tunnels - 费用流
题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...
- Codeforces Gym 101252D&&floyd判圈算法学习笔记
一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...
- Codeforces Gym 101623A - 动态规划
题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...
- 【Codeforces Gym 100725K】Key Insertion
Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...
- Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】
2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...
- codeforces gym 100553I
codeforces gym 100553I solution 令a[i]表示位置i的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ...
随机推荐
- ANT 发布项目中 build.xml 文件的详细配置
xml 代码 <?xml version="1.0" encoding="UTF-8"?> <!-- name:对应工程名字 default: ...
- IoC Service Provier
本文节选自<Spring 揭秘>. 虽然业务对象可以通过IoC方式声明相应的依赖,但是最终仍然需要通过某种角色或者服务将这些相互依赖的对象绑定到一起,而IoC Service Provid ...
- 【Java多线程】两种基本实现框架
Java多线程学习1——两种基本实现框架 一.前言 当一个Java程序启动的时候,一个线程就立刻启动,改程序通常也被我们称作程序的主线程.其他所有的子线程都是由主线程产生的.主线程是程序开始就执行的, ...
- “内部类” 大总结(Java)
(本文整理自很久以前收集的资料(我只是做了排版修改),作者小明,链接地址没有找到,总之感谢,小明) (后面也对"静态内部类"专门做了补充) 内部类的位置: 内部类可以作用在方法里以 ...
- bzoj 2190 [SDOI2008]仪仗队(欧拉函数)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2190 [题意] n*n的正方形,在(0,0)格点可以看到的格子数目. [思路] 预处理 ...
- 原型模式--prototype
C++设计模式——原型模式 什么是原型模式? 在GOF的<设计模式:可复用面向对象软件的基础>中是这样说的:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象.这这个定义中,最 ...
- VS2010安装EntityFramework5.0
EntityFramework 当前最新版本是6.0,2012年10月份发布,不过是alpha1版本,稳定版本是8月份发布的5.0版本,对于初学者来说,还是别在测试版上折腾了,先学习稳定的5.0版本, ...
- remoting blazeds 实施步骤
remoting 实施步骤 1.创建 --web project 和 -- Flex project 2.在web project 下创建 -- com.HelloRemoting: package ...
- hadoop 伪分布模式的配置
转自 http://blog.csdn.net/zhaogezhuoyuezhao/article/details/7328313 centos系统自带ssh,版本为openssh4.3 免密码ssh ...
- Having the Result Set of a Stored Proc Sent to You by RSS Feed.
Having the Result Set of a Stored Proc Sent to You by RSS Feed. by JBrooks 14. 十二月 2010 12:44 I wa ...