UVA1339 UVALive3213 POJ2159 ZOJ2658 Ancient Cipher【密码】

509 篇文章 8 订阅
281 篇文章 4 订阅

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 35753 Accepted: 11639

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. The most popular ciphers in those times were so called substitution cipher and permutation cipher.  
Substitution cipher changes all occurrences of each letter to some other letter. Substitutes for all letters must be different. For some letters substitute letter may coincide with the original letter. For example, applying substitution cipher that changes all letters from 'A' to 'Y' to the next ones in the alphabet, and changes 'Z' to 'A', to the message "VICTORIOUS" one gets the message "WJDUPSJPVT".  
Permutation cipher applies some permutation to the letters of the message. For example, applying the permutation <2, 1, 5, 4, 3, 7, 6, 10, 9, 8> to the message "VICTORIOUS" one gets the message "IVOTCIRSUO".  
It was quickly noticed that being applied separately, both substitution cipher and permutation cipher were rather weak. But when being combined, they were strong enough for those times. Thus, the most important messages were first encrypted using substitution cipher, and then the result was encrypted using permutation cipher. Encrypting the message "VICTORIOUS" with the combination of the ciphers described above one gets the message "JWPUDJSTVP".  
Archeologists have recently found the message engraved on a stone plate. At the first glance it seemed completely meaningless, so it was suggested that the message was encrypted with some substitution and permutation ciphers. They have conjectured the possible text of the original message that was encrypted, and now they want to check their conjecture. They need a computer program to do it, so you have to write one.

Input

Input contains two lines. The first line contains the message engraved on the plate. Before encrypting, all spaces and punctuation marks were removed, so the encrypted message contains only capital letters of the English alphabet. The second line contains the original message that is conjectured to be encrypted in the message on the first line. It also contains only capital letters of the English alphabet.  
The lengths of both lines of the input are equal and do not exceed 100.

Output

Output "YES" if the message on the first line of the input file could be the result of encrypting the message on the second line, or "NO" in the other case.

Sample Input

JWPUDJSTVP
VICTORIOUS

Sample Output

YES

Source


Regionals 2004 >> Europe - Northeastern


问题链接UVA1339 UVALive3213 POJ2159 ZOJ2658 Ancient Cipher

问题简述:(略)

问题分析:对两组字符串分别进行字母统计,接着对统计结果进行排序,然后对排序后的结果进行比较。如果相同,说明可以找到一种一一映射,使得两个字符串相同。

程序说明:比较两组非字符值是否相等,还可以使用函数memcmp()来实现,参考相关链接。


AC的C语言程序如下:

/* UVA1339 UVALive3213 POJ2159 ZOJ2658 Ancient Cipher */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>

#define LETTERNUM 26
#define MAXN     100

int cmp(const void * a, const void * b)
{
    return *(int *)a - *(int *)b;
}

int main(void)
{
    char s[MAXN+1], t[MAXN+1];
    int counts[LETTERNUM], countt[LETTERNUM], len, flag, i;

    while(scanf("%s", s) != EOF) {
        scanf("%s", t);

        memset(counts, 0, sizeof(counts));
        memset(countt, 0, sizeof(countt));

        len = strlen(s);
        for(i=0; i<len; i++) {
            counts[s[i]-'A']++;
            countt[t[i]-'A']++;
        }

        qsort(counts, LETTERNUM, sizeof(counts[0]), cmp);
        qsort(countt, LETTERNUM, sizeof(countt[0]), cmp);

        flag = 1;
        for(i=0; i<LETTERNUM; i++)
            if(counts[i] != countt[i]) {
                flag = 0;
                break;
            }

        printf("%s\n", flag ? "YES" : "NO");
    }

    return 0;
}




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,我们可以得知这是一道POJ(Peking University Online Judge)的题目,题号为3213。这道题目的主要任务是计算给定的一组数据中,每个数出现的次数,并输出出现次数最多的数的出现次数。同时,由于测试数据较大,使用C++中的iostream对象或Java中的Scanner对象可能会导致效率问题。 下面是Java语言的解题思路和代码实现: 1.首先,我们需要使用Java中的HashMap来存储每个数出现的次数。HashMap是一种基于哈希表实现的Map接口,可以用来存储键值对,其中键是唯一的,值可以重复。 2.接下来,我们需要读入数据,并将每个数出现的次数存储到HashMap中。由于输入数据的格式比较特殊,我们需要使用Java中的Scanner类来读取数据。具体来说,我们可以使用Scanner类的nextInt()方法来读取整数,并使用while循环来读取所有的数据。 3.最后,我们需要遍历HashMap,找到出现次数最多的数的出现次数,并输出结果。 下面是Java语言的代码实现: ```java import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); Map<Integer, Integer> map = new HashMap<Integer, Integer>(); int n = in.nextInt(); int m = in.nextInt(); for (int i = 0; i < n; i++) { int x = in.nextInt(); if (x != -1) { if (map.containsKey(x)) { map.put(x, map.get(x) + 1); } else { map.put(x, 1); } } } int max = 0; for (int key : map.keySet()) { max = Math.max(max, map.get(key)); } System.out.println(max * m); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值