Ananagrams
Ananagrams
SCUACM2022集训前训练-数据结构 - Virtual Judge (vjudge.net)
字符串比较
若比较两个字符串能否经过重新排列构成(不区分大小写),不必统计每个字符出现的次数看是否相等,可以将字符全部变成小写再 sort 一下,看两个字符串是否相等
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <map>
using namespace std;
typedef long long ll;
const int N = 1e3;
string s[N];
map<string, int> mp;
int main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
string str;
int cnt = 0;
while(cin >> str, str != "#")
{
s[++cnt] = str;
for (int i = 0; i < str.size(); i++)
str[i] = tolower(str[i]);
sort(str.begin(), str.end());
mp[str]++;
}
sort(s + 1, s + cnt + 1);
for (int i = 1; i <= cnt; i++)
{
string now = s[i];
for (int j = 0; j < now.size(); j++)
now[j] = tolower(now[j]);
sort(now.begin(), now.end());
if (mp[now] == 1)
cout << s[i] << endl;
}
return 0;
}