• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
dwtfukgv
博客园    首页    新随笔    联系   管理    订阅  订阅
LA 3213 Ancient Cipher (水题,转化)

题意:给定两个长度相同的字符串,判断它们之间是否存在一一对应关系,顺序不定。

析:刚开始没看到顺序不定,然后写完没胡把样例看完就交了,结果WA了一次。。。其实这是一个水题,既然顺序不定,那么更简单,我们只要统计两个串中每个字母出现的次数,然后再排序(从大到小还是从小到大无所谓),只要它们的次数对应相等,那么它就可以一一对应。

代码如下:

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

using namespace std;
const int maxn = 100 + 10;
char s1[maxn], s2[maxn];
int num1[26], num2[26];

int main(){
    while(~scanf("%s %s", s1, s2)){
        int n = strlen(s1);
        memset(num1, 0, sizeof(num1));
        memset(num2, 0, sizeof(num2));
        for(int i = 0; i < n; ++i)  ++num1[s1[i]-'A'];
        for(int i = 0; i < n; ++i)  ++num2[s2[i]-'A'];
        sort(num1, num1+26);
        sort(num2, num2+26);

        bool ok = true;
        for(int i = 0; i < 26; ++i)
            if(num1[i] != num2[i]) { ok = false;  break; }
        if(ok)  puts("YES");
        else  puts("NO");
    }
    return 0;
}

 

posted on 2016-05-26 23:44  dwtfukgv  阅读(404)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3