//.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxn 100 + 10
int cmp(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
}
int main()
{
char a[maxn];
char b[maxn];
while(scanf("%s%s", a,b) != EOF)
{
int len = strlen(a);
int cnt1[26],cnt2[26];
memset(cnt1,0,sizeof(cnt1));
memset(cnt2,0,sizeof(cnt2));
for(int i=0; i < len; i++)
{
cnt1[a[i] - 'A']++;
cnt2[b[i] - 'A']++;
}
//排序
qsort(cnt1,26,sizeof(cnt1[0]),cmp);
qsort(cnt2,26,sizeof(cnt2[0]),cmp);
for(int i=0; i < 26; i++)
{
if(cnt1[i] != cnt2[i])
{
printf("NO\n");
return 0;
}
}
printf("YES\n");
}
return 0;
}
//.cpp
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
#define maxn 100 + 10
bool cmp(const int a, const int b)
{
return a<b;
}
int main()
{
string a,b;
while(cin >> a >> b)
{
int len = a.length();
int cnt1[26],cnt2[26];
memset(cnt1,0,sizeof(cnt1));
memset(cnt2,0,sizeof(cnt2));
for(int i=0; i < len; i++)
{
cnt1[a[i] - 'A']++;
cnt2[b[i] - 'A']++;
}
//排序
sort(cnt1,cnt1+26,cmp);
sort(cnt2,cnt2+26,cmp);
for(int i=0; i < 26; i++)
{
if(cnt1[i] != cnt2[i])
{
cout<<"NO"<<endl;
return 0;
}
}
cout<<"YES"<<endl;
}
return 0;
}
例题4-1 古老的密码 (排序)
最新推荐文章于 2024-02-25 23:45:44 发布
本文介绍了一种使用C和C++实现的字符串比较算法,通过统计并比较两个字符串中每个字符出现的次数来判断它们是否相等。该算法首先初始化两个计数数组,然后遍历字符串更新计数,最后通过比较计数数组来确定字符串是否相同。
1195

被折叠的 条评论
为什么被折叠?



