uva1339Ancient Cipher

【题目描述】:密码

古典密码学有两种密码,仿射和换位,给定一段明文,一段密文,看密文能否由这段明文,是否可能通过仿射和换位得到。

【算法分析】:这道题目的关键是读题。因为接触过密码学,所以容易理解。

仿射密码:M=(m+a)%26,注意:对于相同的字母,仿射到同一个字母

换位密码:就是把所有的字母重排。

所以这道提的关键是,无论怎么仿射和置换,不用的字母总数不会增加,相同的字母的个数不会增加。即使通过很多次这样的操作也是这样。所以统计两段密文,不同的字母出现的个数,排序后比较即可。题外话:可能变换成功。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

char str1[101], str2[101];
int  set1[26], set2[26];

int main()
{
    while (cin >> str1 >> str2) {
        memset(set1, 0, sizeof(set1));
        memset(set2, 0, sizeof(set2));
        for (int i = 0; str1[i]; ++i)
        {
            set1[str1[i] - ‘A‘] ++;
            set2[str2[i] - ‘A‘] ++;
        }
        sort(set1, set1 + 26);
        sort(set2, set2 + 26);

        int flag = 1;
        for (int i = 0; i < 26; ++i)
            if (set1[i] != set2[i]) {
                flag = 0;
                break;
            }
        if (flag)
            puts("YES");
        else puts("NO");
    }
    return 0;
}
时间: 2024-08-24 16:09:54

uva1339Ancient Cipher的相关文章

UVA1339- Ancient Cipher

题意:给定两个长度均为n的字符串,判断它们之间的26个字母能否一一对应,即做一个一一映射后使得两个字符串相同.输入两个字符串,输出YES或者NO. 思路:只要判断出现的字母的次数能否完全一一对应即可,比如s1存在出现3次的字符,那么s2中也必须存在这样的字符. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; c

uva-1339Ancient Cipher

Ancient Roman empire had a strong government system with various departments, including a secret service department. Important documents were sent between provinces and the capital in encrypted form to prevent eavesdropping. The most popular ciphers

紫书第4章 函数和递归

1  序 系统的整理下第四章的学习笔记.同上次一样,尽量在不依赖书本的情况下自己先把例题做出来.这次有许多道题代码量都比较大,在例题中我都用纯C语言编写,但由于习题的挑战性和复杂度,我最终还是决定在第五章开始前,就用C++来完成习题.不过所有的代码都是能在C++提交下AC的. 在习题中,我都习惯性的构造一个类来求解问题,从我个人角度讲,这会让我的思路清晰不少,希望自己的一些代码风格不会影响读者对解题思路的理解. 其实在第四章前,我就顾虑着是不是真的打算把题目全做了,这些题目代码量这么大,要耗费很

Cracking The Vigenere Cipher

In order to crack "Vigenere Cipher" under the circumstance that the key length can be only 3, 4 or 5, I used frequency analysis to find possible keys and compared the Euclidean distance of all candidate keys calculated with "Relative freque

poj 2159 D - Ancient Cipher 文件加密

Ancient Cipher Description Ancient Roman empire had a strong government system with various departments, including a secret service department. Important documents were sent between provinces and the capital in encrypted form to prevent eavesdropping

Caesars Cipher

让上帝的归上帝,凯撒的归凯撒. 下面我们来介绍风靡全球的凯撒密码Caesar cipher,又叫移位密码. 移位密码也就是密码中的字母会按照指定的数量来做移位. 一个常见的案例就是ROT13密码,字母会移位13个位置.由'A' ? 'N', 'B' ? 'O',以此类推. 写一个ROT13函数,实现输入加密字符串,输出解密字符串. 所有的字母都是大写,不要转化任何非字母形式的字符(例如:空格,标点符号),遇到这些特殊字符,跳过它们. 这是一些对你有帮助的资源: String.charCodeAt

BZOJ 1031: [JSOI2007]字符加密Cipher 后缀数组

1031: [JSOI2007]字符加密Cipher Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 6014  Solved: 2503[Submit][Status][Discuss] Description 喜欢钻研问题的JS同学,最近又迷上了对加密方法的思考.一天,他突然想出了一种他认为是终极的加密办法 :把需要加密的信息排成一圈,显然,它们有很多种不同的读法.例如下图,可以读作: JSOI07 SOI07J OI07JS I07JSO 0

【BZOJ】【1031】【JSOI2007】字符加密Cipher

后缀数组 当年感觉好神的题现在好像变水了…… 题意其实有点蛋疼……一开始没看懂<_< 将原串复制一遍接在后面,用后缀数组求一下SA,那么SA<n的就是所找到的那n个字符串,然后把它们的第n个字符抠出来就可以了…… 1 /************************************************************** 2 Problem: 1031 3 User: Tunix 4 Language: C++ 5 Result: Accepted 6 Time:7

POJ1026 Cipher 【polya】

This question is not so difficult. First,my thoughts were we should use a lot of code to find out the loop block,but there is no need to do that . you just need to get every new position of char in the string. Algorithm is also easy , just like how d