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 = 0; i < ans.length(); i++)
        ans[i] = tolower(ans[i]);
    sort(ans.begin(), ans.end());
    return ans;
}

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


作者:is_ok
出处:http://www.cnblogs.com/00isok/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

原文地址:https://www.cnblogs.com/00isok/p/8650948.html