UVa-156-反片语

这题比较精妙的是,我们对于单词重排,实际上是进行了标准化的处理,即按照字典序排序。
这样的话,就很方便地处理了单词的重排问题,我们不需要使用全排列函数进行排列尝试,我们直接化简为一,然后进行比较就可以了。
然后我们使用map映射,映射每个单词的出现次数,只有等于一的,我们才输出原版的单词。
最后输出的时候我们对于最初读入的单词库,进行遍历,看它的映射值为多少,先写入,再排序,最后输出即可。
注:sort和se的默认排序都是按照字典序排序的。

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

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

string repr(const 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()
{
    string s;
    while (cin>>s) {
        if (s[0]=='#')
            break;
        words.push_back(s);
        string r = repr(s);
        if (!cnt.count(r))
            cnt[r]=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] << endl;
    return 0;
}
posted @ 2019-02-03 10:44  xyee  阅读(353)  评论(0编辑  收藏  举报