B: Known Plaintext Attack
One of the earliest encrypting systems is attributed to Julius Caesar: if the letter to be encrypted is the
B: Known Plaintext Attack |






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,

Output
The output has

Sample Input
2 drsc sc k docd test dl ruvd doha hp pz we
Sample Output
k hl
#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 100;
vector<string> vstr;
string s;
void input()
{
char buf[N], tmp[N];
fgets(buf, N, stdin);
buf[strlen(buf) - 1] = 0;
for (int i = 0, len = strlen(buf); i < len; i++) {
if (i == 0) {
sscanf(buf, "%s", tmp);
vstr.push_back((string)tmp);
} else if (buf[i] == ' ') {
sscanf(&buf[i + 1], "%s", tmp);
vstr.push_back((string)tmp);
}
}
fgets(buf, N, stdin);
buf[strlen(buf) - 1] = 0;
s = buf;
}
bool check(string &s1, string &s2)
{
int diff = (s1[0] - s2[0] + 26) % 26;
for (size_t i = 1, size = s1.length(); i < size; i++) {
if (diff != (s1[i] - s2[i] + 26) % 26) return false;
}
return true;
}
void solve()
{
string ans;
for (size_t i = 0, size = vstr.size(); i < size; i++) {
if (vstr[i].length() == s.length()) {
if (check(vstr[i], s)) {
int diff = (vstr[i][0] - s[0] + 26) % 26;
ans += ('a' + diff);
}
}
}
sort(ans.begin(), ans.end());
printf("%s\n", ans.c_str());
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("d:\\OJ\\uva_in.txt", "r", stdin);
#endif
char buf[N];
int t;
fgets(buf, N, stdin);
sscanf(buf, "%d", &t);
while (t--) {
vstr.clear();
input();
solve();
}
return 0;
}