题目大意:
输入一些单词,找出所有满足如下条件的单词: 该单词不能通过字母重排,得到输入文本中的另外一些单词。在判断是否满足条件时,字母不分大小写,但在输出时应保留输入中的大小写,按字典序进行排序(所有大写字母应该在小写字母前面)
题目分析:
需要把输入中的每个单词标准化,从而判断是否出现过;
怎么标准化呢, 将单词大写变成小写,按照一个你规定的顺序排序,如果标准化的单词没有重复的,该单词自然符合条件;
代码如下:
#include <iostream>
#include <map>
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <vector>
using namespace std;
map<string, int> suffix; //保存合适的下标;
vector<string> words;
string repr(string &s){
string r = s;
for(auto &c: r){
c = tolower(c);
}
sort(r.begin(), r.end());
return r;
}
int main()
{
string s, stbuf;
while(cin >> s && s[0] != '#'){
words.push_back(s);
string r = repr(s); //标准化;
if(!suffix.count(r))
suffix[r] = words.size()-1;
else suffix[r] = -1;
}
vector<string> ans;
for(auto c: suffix){
if(c.second != -1)
ans.push_back(words[c.second]);
}
sort(ans.begin(), ans.end());
for(auto c: ans){
cout << c << endl;
}
}