UVA850 Crypt Kicker II密码译码

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA850 Crypt Kicker II密码译码相关的知识,希望对你有一定的参考价值。

A common but insecure method of encrypting text is to permute the letters of the alphabet. That is, in the text, each letter of the alphabet is consistently replaced by some other letter. So as to ensure that the encryption is reversible, no two letters are replaced by the same letter.
    A common method of cryptanalysis is the known plaintext attack. In a known plaintext attack, the cryptanalist manages to have a known phrase or sentence encrypted by the enemy, and by observing the encrypted text then deduces the method of encoding.
    Your task is to decrypt several encrypted lines of text, assuming that each line uses the same set of replacements, and that one of the lines of input is the encrypted form of the plaintext

the quick brown fox jumps over the lazy dog
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
    The input consists of several lines of input. Each line is encrypted as described above. The encrypted lines contain only lower case letters and spaces and do not exceed 80 characters in length. There are at most 100 input lines.
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
    Decrypt each line and print it to standard output. If there is more than one possible decryption (several lines can be decoded to the key sentence), use the first line found for decoding.
    If decryption is impossible, output a single line:
No solution.
Sample Input
1

vtz ud xnm xugm itr pyy jttk gmv xt otgm xt xnm puk ti xnm fprxq
xnm ceuob lrtzv ita hegfd tsmr xnm ypwq ktj
frtjrpgguvj otvxmdxd prm iev prmvx xnmq
Sample Output
now is the time for all good men to come to the aid of the party
the quick brown fox jumps over the lazy dog
programming contests are fun arent they

问题链接UVA850 Crypt Kicker II
问题简述:(略)
问题分析:密码的译码问题,不解释。
程序说明:输入处理需要点技巧。
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA850 Crypt Kicker II */

#include <bits/stdc++.h>

using namespace std;

const char key[] = "the quick brown fox jumps over the lazy dog";
const int KEYLEN = strlen(key);
const int N = 100;
const int L = 80;
char s[N][L + 1], mp[128];

int decrypt(char s[])
{
    if (strlen(s) != KEYLEN) return 0;
    for (int i = 0; s[i]; i++)
        if (s[i] != ' ' && key[i] == ' ') return 0;
    memset(mp, 0, sizeof mp);
    for (int i = 0; s[i]; i++)
        if (mp[s[i]] && mp[s[i]] != key[i]) return 0;
        else mp[s[i]] = key[i];
    return 1;
}

int main()
{
    int t;
    scanf("%d", &t);
    getchar();
    getchar();
    while (t--) {
        int n = 0;
        while (gets(s[n]) && s[n][0]) n++;

        int flag = 0;
        for (int i = 0; i < n; i++)
            if ((flag = decrypt(s[i]))) break;

        if (flag == 0) puts("No solution.");
        else {
            for (int i = 0; i < n; i++) {
                for (int j = 0; s[i][j]; j++)
                    printf("%c", mp[s[i][j]]);
                putchar('\\n');
            }
        }
        if (t) putchar('\\n');
    }

    return 0;
}

以上是关于UVA850 Crypt Kicker II密码译码的主要内容,如果未能解决你的问题,请参考以下文章

850. Rectangle Area II

850. Dijkstra求最短路 II

LeetCode 850. Rectangle Area II

[LeetCode] 850. Rectangle Area II 矩形面积之二

acwing 850. Dijkstra求最短路 II 模板

AcWing 850. Dijkstra求最短路 II(Dijkstra稠密图堆优化模板)