UVA 1339 Ancient Cipher
題意
給出兩個字符串,可以重新排列可以自由映射,把其中的一個字符映射到另一個字符。詢問是否存在一種一一映射方式,使得原來的字符串可以映射到目標字符串
解決
- 由於可以重新排列,所以字符的位置不重要(這點以後會游泳,記住)
- 由於本題中的映射只能一對一,所以要求如果我們記錄下26個字符每個字符出現的次數,並對字符出現的次數進行排序,str1(原串)裏一個字符出現了n次,那麼對應str2(目標串)中也必須存在一個出現n次的字符(且沒被用過)。
- 所以我們對兩個串中字符出現次數進行排序,比較是否完全相同即可
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <complex>
using namespace std;
typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;
typedef vector<int> vi;
#define de(x) cout << #x << "=" << x << endl
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define E 1e-6
#define INF 0x3f3f3f3f
#pragma comment(linker, "/STACK:1024000000,1024000000")
void open(){freopen("data.txt","r",stdin);}
void out(){freopen("out.txt","w",stdout);}
const int maxn = 101010;
const int MOD = 1e9 + 7;
int main()
{
char str1[105],str2[105];
int cnt1[30],cnt2[30];
while(~scanf("%s",str1))
{
scanf("%s",str2);
memset(cnt1,0,sizeof(cnt1));
memset(cnt2,0,sizeof(cnt2));
int len1 = strlen(str1);
int len2 = strlen(str2);
rep(i,0,len1) cnt1[str1[i]-'A']++;
rep(i,0,len2) cnt2[str2[i]-'A']++;
sort(cnt1,cnt1+26);
sort(cnt2,cnt2+26);
bool ok = true;
rep(i,0,26) if(cnt1[i]!=cnt2[i]) ok = false;
if(ok) puts("YES");
else puts("NO");
}
}