UVa-156 Ananagrams

来源:互联网 发布:js获取时间控件的值 编辑:程序博客网 时间:2024/06/03 21:07
#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用法

0 0
原创粉丝点击