您的位置:首页 > 其它

Problem D Ananagrams(map的使用)

2015-06-30 15:57 218 查看
题目链接:Problem D

题意:输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文本中的另一个单词。在判断是否满足条件时,字母不区分大小写。

但是输出时应保留原始大小写,按字典序进行排列。

思路:把单词统一处理一下,然后放入map中,用vector记录下满足要求的单词,最后排序一下即可。

code:

#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <cctype>
using namespace std;

vector<string> words;
map<string, int> cnt;

string repr(string str)
{
string ret = str;
int len = ret.size();
for (int i = 0; i < len; ++i)
ret[i] = tolower(ret[i]);
sort(ret.begin(), ret.end());
return ret;
}

int main()
{
string str;
while (cin >> str)
{
if ('#' == str[0]) break;
words.push_back(str);
string t = repr(str);
if (cnt.count(t) == 0) cnt[t] = 0;
++cnt[t];
}
int len = words.size();
vector<string> ans;
for (int i = 0; i < len; ++i)
{
if (1 == cnt[repr(words[i])])
ans.push_back(words[i]);
}
len = ans.size();
sort(ans.begin(), ans.end());
for (int i = 0; i < len; ++i)
cout << ans[i] << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: