Sicily 4959. Hidden Code
来源:互联网 发布:金域名都南昌 编辑:程序博客网 时间:2024/06/04 18:22
4959. Hidden Code
Constraints
Time Limit: 1 secs, Memory Limit: 256 MB
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, then the first |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) mod 26 for 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|) mod 26 for 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.
Output
For each test case, print a single line that contains the shortest possible key or “Impossible” (quotes added for clarity) if no possible key could have produced all of the encryptions.
Sample Input
1A B3STANFORD SVMFAAWDAVOWIENR AXAWFEJWVAMRI VCYMK3ABCDEFGHIJKLMNOPQRSTUVWXYZ AAAAAAAAAAAAAAAAAAAAAAAAAAY YZ Z2A BB A01
Sample Output
BACMAZYXWVUTSRQPONMLKJIHGFEDCBImpossible
Problem Source
Stanford Programming Contest 2011
// Problem#: 4959// Submission#: 3372676// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <stdio.h>#include <iostream>#include <vector>#include <string>#include <stack>#include <iomanip>#include <algorithm>#include <queue>#include <functional>#include <map>#include <string.h>#include <math.h>using namespace std;int N;vector<pair<string, string> > S;string K;void getK(int L) { K.push_back('A' + S[0].second[L - 1] - S[0].first[L - 1] + (S[0].second[L - 1] < S[0].first[L - 1] ? 26 : 0));}bool test(int n) { string P = S[n].first, C = S[n].second, k = K, c; c.resize(C.size()); for (int i = 0, j = 0; i < P.size(); i++) { c[i] = (P[i] - 'A' + k[j] - 'A') % 26 + 'A'; k[j] = c[i]; if (c[i] != C[i]) return false; j = (j + 1) % K.size(); } return true;}bool testK() { for (int i = 0; i < N; i++) { if (!test(i)) return false; } return true;}bool cmp(const pair<string, string> & p1, const pair<string, string> & p2) { return p1.first.size() > p2.first.size();}int main() { std::ios::sync_with_stdio(false); while (1) { K.clear(); cin >> N; if (!N) break; S.resize(N); for (int i = 0; i < N; i++) cin >> S[i].first >> S[i].second; sort(S.begin(), S.end(), cmp); int L = 1, maxL = S[0].first.size(); bool success = false; while (L <= maxL) { getK(L); if (testK()) { success = true; break; } else L++; } cout << (success ? K : "Impossible") << endl; } return 0;}
- Sicily 4959. Hidden Code
- Sicily 1721. Gray code
- Sicily 1721. Gray code
- hunnu 11120 hidden code 暴力
- Gym 100015H Hidden Code
- Sicily 2611. Da Vinci Code
- Sicily 1721 Gray code[Special judge]
- Sicily|1721. Gray code[Special judge]
- Code: The Hidden Language of Computer Hardware and Software 总结
- Codeforces Gym 100015H Hidden Code(暴力)
- 贪心算法之Huffman Code——来自Sicily
- Delete a file in use - Run an EXE Hidden - Run code in other process' memory)(vb)
- PyCon 2011 - Hidden Treasures of the Python Standard Library - Source code
- overflow:hidden
- overflow:hidden
- overflow:hidden
- overflow:hidden
- hidden this
- Django代码注意
- Sicily 2370. Just The Simple Fax
- listview中添加Button后item不能点击的解决办法
- mac 下更新sdk
- PLSQL collection 方法 和 三种collection对比
- Sicily 4959. Hidden Code
- 数据库、数据库管理系统和数据库系统的区别
- session维持登录状态的简单例子
- Android动态修改图片颜色的实现方式分析
- js转到jsp参数传递
- 防止疲劳和忧虑的第一原则
- linux下cat命令简略
- Linux-vim
- python中if __name__ == '__main__': 的解析