One of the earliest encrypting systems is attributed to Julius Caesar: if the letter to be encrypted is the m-th letter in the alphabet, replace it with the (m + k)-th where k is some fixed integer. Caesar used k = 3, or a → d, as the key to the encryption. That is, “a” would be encrypted to “d”, “b” would be “e”, and so on until “z” would be “c”. For example, for a → k, the sentence “this is a test” would become “drsc sc k docd”.
Now you have an encrypted sentence, and you know a decrypted word in that sentence. Your task is to find all the possible keys to the encryption.
Input
The input starts with a line containing a number, n, followed by n sets of encrypted sentence/decrypted word pairs. Each set has two lines, the first line is the encrypted sentence which contains only words separated by a space with no punctuation, the second line contains a word which is one of the decrypted word of the sentence above it. All words are in lowercase letters. You may assume a word is at most 16 characters long, and a line has no more than 70 characters.
Output
The output has n lines of character(s), corresponding to the n sets of encrypted sentence/decrypted word pairs.
The line consists of lowercase letter(s) of possible key(s). That is, if the plaintext “a” is encrypted to “x”, then ‘x’ would be in the line.
Each line of characters must be sorted in alphabetical order.
Sample Input
2
drsc sc k docd
test
dl ruvd doha hp pz
we
Sample Output
k
hl
问题链接:UVA10896 Known Plaintext Attack
问题简述:(略)
问题分析:
简单的文本处理题,不解释。给出的程序代码是比较简洁的。
程序说明:(略)
参考链接:(略)
题记:(略)
AC的C++语言程序如下:
/* UVA10896 Known Plaintext Attack */
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
getchar();
while(t--) {
string s, word;
getline(cin, s);
getline(cin, word);
set<char> ans;
set<int> st;
int n = s.size(), m = word.size(), j = 0;
for(int i = 0; i < n; i++) {
if(islower(s[i])) {
if(j < m) {
st.insert((26 + s[i] - word[j]) % 26);
j++;
}
} else if(s[i] == ' ') {
if(j == m) {
if(st.size() == 1)
ans.insert('a' + (26 + s[i - j] - word[0]) % 26);
}
j = 0;
st.clear();
}
}
if(j == m)
if(st.size() == 1)
ans.insert('a' + (26 + s[n - j] - word[0]) % 26);
for(set<char>::iterator iter = ans.begin(); iter != ans.end(); iter++)
cout << *iter;
cout << endl;
}
return 0;
}