zoukankan      html  css  js  c++  java
  • POJ 2159 Ancient Cipher

    解题思路:给出第一串字母,比如ABHGYU,以及第二串字母BHIUIJJ,问第二串字母能不能够通过题目中的规则变化得到,

    先说一下自己的错误思路----,变化的规则我的理解当时是:(就是题目中给的样例那种,A可以变成B,B可以变成C,-----Z可以变成A,然后排列顺序是任意的,并且两串字母等长就可以了)---然后果断地WA---- 

    然后是http://poj.org/showmessage?message_id=112259 这是discuss里面的

    下面是自己的一点体会, Substitution cipher changes all occurrences of each letter to some other letter. 这是题目中关于代替密码的第一句描述:代替密码将所有的字母转换成另外的字母(没有说A只能变成B,B只能变成C,这只是符合题意的一个样例--) Substitutes for all letters must be different 所有的代替密码都是不同的, 即为符合一一映射的关系 比如 AABBBBCCCCC   2 4 5 MMNNNNYYYYY   2 4 5

    A映射到M,B映射到N,C映射到Y,即可以这样理解出现的频率是一致的

    所以我们可以用一个数组分别储存一个字符串里面相应字母出现的频率,排序

    然后比较这两个字符串的频率数组是否一致即可。

    Ancient Cipher
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 29131   Accepted: 9539

    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
    #include<iostream>  
    #include<cstdio>  
    #include<cstring>  
    #include<algorithm>  
    using namespace std;
    char a[105],b[105];
    int f[30],g[30],s1[30],s2[30];
    int main()
    {
    	int i,j,len1,len2,k=1,flag=1,t=1;
    	scanf("%s",&a);
    	scanf("%s",&b);
    	len1=strlen(a);
    	len2=strlen(b);
    	memset(f,0,sizeof(f));
    	memset(g,0,sizeof(g));
    	
    	if(len1!=len2)
    	flag=0;
    	else
    	{
    			for(i=0;i<len1;i++) f[a[i]-64]++;
    	        for(j=0;j<len2;j++) g[b[j]-64]++;
    	
    	for(i=1;i<=26;i++)
    	if(f[i]!=0) s1[k++]=f[i];
    	sort(s1+1,s1+k);
    	
    	for(i=1;i<=26;i++)
    	if(g[i]!=0) s2[t++]=g[i];
    	sort(s2+1,s2+t);
    	
    	for(i=1;i<=26;i++)
    	{
    		if(s1[i]!=s2[i])
    		{
    			flag=0;
    			break;
    		}
    	}	
    	}
    	
    	if(flag)
    	printf("YES
    ");
    	else
    	printf("NO
    ");
    
    }
    

      

  • 相关阅读:
    突破极验验证的验证码
    c# 自定义多选下拉列表2
    c#多选下拉框(ComboBox)
    图片缩放+拖动(html)
    使用天天模拟器开发android应用
    FineUI开源版之TreeGrid(修改)
    FineUI开源版之TreeGrid实现
    c# 窗体最小化后截图实现
    c#一个简单的实例告诉你,多继承还可以这么来
    设置控件Enable=false,控件颜色不变
  • 原文地址:https://www.cnblogs.com/wuyuewoniu/p/4259833.html
Copyright © 2011-2022 走看看