UVa156

题意:

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

分析:

       将输入的单词进行“标准化”,即将单词中的每个字母化为小写并按字典序重排单词,用一个字典来统计一个标准化的单词出现过多少次,输出的时候只输出在标准字典中出现一次的那些单词即可。

 1 #include <iostream>
 2 #include <string>
 3 #include <map>
 4 #include <vector>
 5 #include <algorithm>
 6 using namespace std;
 7 // 将单词进行标准化
 8 string repr(const string& s){
 9     string ans = s;
10     for(int i = 0 ; i < ans.length() ; i++)
11         ans[i] = tolower(ans[i]);
12     sort(ans.begin(),ans.end());
13     return ans;
14 }
15 vector<string> words;
16 map<string,int> cnt;
17 int main(){
18     int n = 0;
19     string s;
20     while(cin >> s){
21         if(s[0] == '#') break;
22         words.push_back(s);
23         string r = repr(s);
24         if(!cnt.count(r)) cnt[r] = 0;
25         cnt[r]++;
26     }
27     vector<string> ans;
28     for(int i = 0 ; i < words.size() ; i++)
29         if(cnt[repr(words[i])] == 1) ans.push_back(words[i]);
30     sort(ans.begin(),ans.end());
31     for(int i = 0 ; i < ans.size() ; i++)
32         cout << ans[i] << endl;
33     return 0;
34 }
View Code

 

posted @ 2016-09-02 17:59  Yan_Bin  阅读(441)  评论(0编辑  收藏  举报