分析:映射map的使用
思路:
1.关键点:将单词“标准化“,就是将单词转化成小写字母,按字典序排序;
2.把所有的输入都存到不定数组words中,同时在集合cnt中把每个string与int
形成映射关系;
3.挑选出words中在cnt中对应为1的string,即不能转化成其他单词的string,把它们存入不定数组ans,排序后输出即可。
知识点:
1.关于map–头文件为map; 提供了[ ]运算符,可以像数组一样被使用,也称”关联数组“。
2. map的基本操作函数:
*C++Maps 是一种关联式容器,包含“关键字/值”对
begin() 返回指向map头部的迭代器
clear() 删除所有元素
count() 返回指定元素出现的次数
empty() 如果map为空则返回true
end() 返回指向map末尾的迭代器
equal_range() 返回特殊条目的迭代器对
erase() 删除一个元素
find() 查找一个元素
get_allocator() 返回map的配置器
insert() 插入元素
key_comp() 返回比较元素key的函数
lower_bound() 返回键值>=给定元素的第一个位置
max_size() 返回可以容纳的最大元素个数
rbegin() 返回一个指向map尾部的逆向迭代器
rend() 返回一个指向map头部的逆向迭代器
size() 返回map中元素的个数
swap() 交换两个map
upper_bound() 返回键值>给定元素的第一个位置
value_comp() 返回比较元素value的函数*
代码:
#include<cstdio>
#include<iostream>
#include<string>
#include<cctype>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
map<string, int> cnt ; //声明映射 cnt
vector <string> words; //声明不定长数组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); //在数组尾部添加元素s
string r = repr(s); //把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;
}