首页 > 其他 > 详细

UVa156

时间:2016-09-02 18:46:07      阅读:194      评论:0      收藏:0      [点我收藏+]

技术分享

技术分享

题意:

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

分析:

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

技术分享
 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

 

UVa156

原文:http://www.cnblogs.com/cyb123456/p/5834387.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!