您的位置:首页 > 其它

UVa-156 Ananagrams

2015-08-18 15:21 411 查看
#include <iostream>
#include <cstdio>
#include <map>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;

map<string, int> cnt;               //储存标准化后的单词出现次数
vector<string> word;                //用来保存原始单词(原顺序 原大小写)
string str;

inline string norm(string temp)     //将单词temp标准化
{
for(int i = 0; i < (int)temp.length(); i ++)
temp[i] = tolower(temp[i]);
sort(temp.begin(), temp.end());
return temp;
}

int main()
{
while(cin >> str)
{
if(str == "#")
break;
word.push_back(str);                //保存原始单词到word
string substitute = norm(str);      //substitute接收函数返回值
if(cnt.count(substitute) == 0)      //如果map中之前没有substitute 则初始化0
cnt[substitute] = 0;
cnt[substitute] ++;                 //记录标准化后出现的次数(出现多次即是不符合题意的)
}
set<string> ans;                        //存入满足题意的原单词 并自动从小到大排序
for(int i = 0; i < (int)word.size();i ++)
if(cnt[norm(word[i])] == 1)         //从里往外看 word[i]是原单词 norm函数将此单词标准化 cnt判断标准化后的出现几次(1次即是满足题意的)
ans.insert(word[i]);            //存入set
for(set<string>::iterator it = ans.begin(); it != ans.end(); it ++)
cout << *it << endl;                //输出
return 0;
}

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

题解:如上。主要是掌握map用法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: