题目链接:https://vjudge.net/problem/UVA-156
Sample Input
ladder came tape soon leader acme RIDE lone Dreis peatScAlE orb eye Rides dealer NotE derail LaCeS drIednoel dire Disk mace Rob dries #
Sample Output
Disk
NotE
derail
drIed
eye
ladder
soon
题意:输入一个文段,这个文段由若干单词构成,然后找出一些单词,这些单词不能重复(也就是说在这个文段中只出现一次),单词不区分大小写,但是在输出的时候需要保存大小写,输出的时候要按照字典序排序,并且输出的时候大写字母应该在所有的小写字母前面!
用map对文段中的每个单词进行计数,然后将满足的元素插入到vector里面,因为vector会自动排序,所以最后输出就可以了。
具体代码如下:
#include <map>
#include <vector>
#include <cctype>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
map <string, int> mat;
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() {
int n = 0;
string s;
while(cin >> s) {
if(s[0] =='#') break;
words.push_back(s);
string r = repr(s);
//count() 判断是否有元素
if(!mat.count(r)) mat[r] = 0;
mat[r]++;//单词计数
}
vector<string> ans;
for(int i = 0; i < words.size(); i++) {
if(mat[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;
}