UVa-156 Ananagrams 反片语【map】【vector】

时间:2023-03-09 03:12:13
UVa-156 Ananagrams 反片语【map】【vector】

题目链接:https://vjudge.net/contest/211547#problem/D

题目大意:

输入一些单词,找出所有满足以下条件的单词:该单词不能通过字母重排,得到输入文本中的另外一些单词。在判断是否满足条件时,字母不区分大小写。但在输出时应保留输入时的大小写,按字典序进行排列(所有大写字母在所有小写字母的前面)

样例输入:

ladder came tape soon leader acme RIDE lone Dreis peat ScAlE orb eye Rides dealer NotE derail LaCes dried noel dire Disk mace Rob dries #

样例输出:

Disk

NotE

derail

drIed

eye

ladder

soon

#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<algorithm>
using namespace std; map<string, int> cnt;
vector<string> words; // 将单词s进行“标准化”
string repr(string s) {
string ans = s;
for (int i = ; i < ans.length(); i++)
ans[i] = tolower(ans[i]);
sort(ans.begin(), ans.end());
return ans;
} int main() {
int n = ;
string s;
while (cin >> s) {
if (s[] == '#') break;
words.push_back(s);
string r = repr(s);
if (!cnt.count(r)) cnt[r] = ; //使用count,返回的是被查找元素的个数。如果有,返回1;否则,返回0。注意,map中不存在相同元素,所以返回值只能是1或0。
cnt[r]++;
}
vector<string> ans;
for (int i = ; i < words.size(); i++)
if (cnt[repr(words[i])] == ) ans.push_back(words[i]);
sort(ans.begin(), ans.end());
for (int i = ; i < ans.size(); i++)
cout << ans[i] << "\n";
return ;
}