uva 1339 Ancient Cipher

点击打开链接uva 1399

题意:给定两个长度分别为n的字符串,判断他们能否一一对应
思路:暴力
分析:
1 首先我们知道这两个字符串的长度最大为100并且相等
2 刚开始我的想法是对两个字符串排序,然后从头开始枚举判断。这个想法妥妥的过了样例,然而等到的确实一顿怒wa。下面举例证明我的思路是错误的
3 假设有两个字符串为ABBCFEA 和 CCGGHJB。排序后为AABBCEF和 BCCGGHJ如果按照我的思路那么我们从头开始枚举得到的是NO,但是实际上这个例子是YES。我们写出两个字符串中字母出现的次数的序列分别为22111和12211,那么我们对这个数字序列进行排序分别为11122和11122可以知道这个比较就可以得到YES

代码:

#include<map>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int maxn = 110;

bool ok(char *s1 , char *s2){
    int len = strlen(s1);
    int num1[maxn] , num2[maxn];
    memset(num1 , 0 , sizeof(num1));
    memset(num2 , 0 , sizeof(num2));
    for(int i = 0 ; i < len ; i++){
       num1[s1[i]-'A']++;
       num2[s2[i]-'A']++;
    }
    sort(num1 , num1+maxn);
    sort(num2 , num2+maxn);
    return !memcmp(num1 , num2 , sizeof(num1)) ? true : false;
}

int main(){
    char s1[maxn] , s2[maxn];
    while(scanf("%s" , s1) != EOF){
        scanf("%s" , s2);
        printf("%s\n" , ok(s1 , s2) ? "YES" : "NO");
    }
    return 0;
}
时间: 2025-01-31 01:16:47

uva 1339 Ancient Cipher的相关文章

poj 2159 Ancient Cipher

这题主要应该就是理解题意的问题,我的确是把题目理解错了,WA了三次,每次都以为一定是正确的,后来看了别人的解释才知道是题目理解错了(竟然还看到了别人的结题报告也有错的...),另外就是学会了用STL的sort快排,很方便,在<algorithm>头文件里面,这说明了一定要自己会写排序,但是不一定每次都是自己写函数,有N多好的函数别人已经写好了,你只用知道什么时候该用就可以了...但是自己也要会写(万一比赛不允许用库函数) 开始错误的想法: #include <iostream> #

uvalive 3213 Ancient Cipher

题目:给定两个只含大写字母的等长字符串,问两者之间是否存在一一映射 分析:考察一一映射的概念,将两个字符串分别作字母统计,再按字母出现个递增的顺序排序(排列的是每个字母出现的个数),如果排序后结果一样那么两者是一一映射 1 #include <stdio.h> 2 #include <iostream> 3 #include <string> 4 #include <algorithm> 5 #define zz 6 using namespace std;

UVa 306 / POJ 1026 Cipher (置换群)

306 - Cipher Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=242 http://poj.org/problem?id=1026 Bob and Alice started to use a brand-new encoding scheme.

UVa 1362 Exploring Pyramids:多叉树遍历&amp;amp;DP

1362 - Exploring Pyramids Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=469&page=show_problem&problem=4108 Archaeologists have discovered a new set of hidden caves in one of the Egy

UVa 10254 The Priest Mathematician:组合数学&amp;amp;规律发现&amp;amp;高精度

10254 - The Priest Mathematician Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=34&page=show_problem&problem=1195 The ancient folklore behind the "Towers of Hanoi" puzzle i

UVa 10313 Pay the Price:DP&amp;amp;整数拆分

10313 - Pay the Price Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=1254 In ancient days there was a country whose people had very interesting habits.

UVa 10361 Automatic Poetry:字符串处理&amp;amp;两种读入方式

10361 - Automatic Poetry Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=96&page=show_problem&problem=1302 "Oh God", Lara Croft exclaims, "it's one of these dumb riddle

UVa 10112 Myacm Triangles:枚举&amp;amp;计算几何

10112 - Myacm Triangles Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=101&page=show_problem&problem=1053 计算几何-puzzle acm uva227">There has been considerable archeological wo

UVa 11609 Teams (组合数学)

11609 - Teams Time limit: 1.000 seconds http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=26 56 In a galaxy far far away there is an ancient game played among the planets. The spec