POJ 2159

又水了一题,题目不太好懂。

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.
 
其实就是输入两个字符串,一个为明文,一个为密文,判断这两个是不是可能匹配的。明文转化为密文分为两步走,一是substitution,用另外一个字符代替原字符,比如用B代替A,这里要注意的是每个字符的对应规则可以不一致(如不用都取后一个来代替前一个),只要关系是一一对应的即可,二是permutation,简单而言就是用某种排序规则从新排序。
解决这道题的关键在于,不要纠结于这两步,其实只要比对明文和密文,对应字符的个数是否相等,若相等即有可能排序匹配,而此处不需要算出排序规则。
http://blog.csdn.net/lyy289065406/article/details/6642586 思路讲得挺清楚的,帮了我大忙!
 
我还是用JAVA写的,如下:
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext()){
            char[] str1 = cin.next().toCharArray();
            char[] str2 = cin.next().toCharArray();
            int[] flag1 = new int[26];
            int[] flag2 = new int[26];
            for(int i = 0; i < str1.length; i++){
                flag1[str1[i] - 'A']++;
                flag2[str2[i] - 'A']++;
            }
            Arrays.sort(flag1);
            Arrays.sort(flag2);
            if(Arrays.equals(flag1, flag2))
                System.out.println("YES");
            else
                System.out.println("NO");
        }
    }

}

若改成C++也很简单,一是记得每次初始化数组memset(flag1, 0, sizeof(flag1)),二是排序时用#include<algorithm>里的sort()函数,然后就按顺序把算组里的每一位对比,若全部相等则输出YES

若不想把两个数组都排序也可以,那就要把后面改为

for(i=0;i<26;i++){
        for(j=0;j<26;j++)
            if(flag1[i] == flag2[j]) break;
        flag2[j] = 0;
        if( j == 26 ){
            printf("NO\n");
            return 0;
        }
    }
    if( i == 26 )
        printf("YES\n");

一遍煮粥,一遍水题,现在刚吃完。。。

 

转载于:https://www.cnblogs.com/sillypudding/archive/2013/04/22/3036384.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值