STL--映射:map

map的介绍

       map就是从键到值的映射。因为重载了[]运算符,map像是数组的“高级版”。例如可以用map<string,int>month_name来表示“月份名字到月份编号”的映射,然后用month_name["July"]=7这样的方式来赋值。支持insert,find,count,remove等操作。

map的应用

题目

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

样例输入
ladder came tape soon leader acme RIDE lone Dreis peatScAlE orb eye Rides dealer NotE derail LaCeS drIednoel dire Disk mace Rob dries

样例输出
Disk
NotE
derail
drIed
eye
ladder
soon

代码

#include<iostream>
#include<string>
#include<cctype>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
map<string,int>cnt;
vector<string>words;
string repr(const string &s){//将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);
		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]<<"\n";
	}
	return 0;
}

相关推荐

  1. STL--映射map

    2024-01-01 00:50:02       39 阅读
  2. c++ STL映射—— map 详解

    2024-01-01 00:50:02       20 阅读
  3. es修改mapping映射

    2024-01-01 00:50:02       80 阅读
  4. STL——map/unordered_map

    2024-01-01 00:50:02       10 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-01 00:50:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-01 00:50:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-01 00:50:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-01 00:50:02       20 阅读

热门阅读

  1. 第二篇 创建型设计模式 - 灵活、解耦的创建机制

    2024-01-01 00:50:02       35 阅读
  2. 第二百三十八回

    2024-01-01 00:50:02       36 阅读
  3. C# 时间修改器,修改系统本地时间

    2024-01-01 00:50:02       34 阅读
  4. GBASE南大通用-CodeFirst 模式

    2024-01-01 00:50:02       39 阅读
  5. 关于解决el-select组件自动清除数据空格的问题

    2024-01-01 00:50:02       35 阅读
  6. Linux: eBPF: bcc-tools:tcpdrop使用需要注意的问题

    2024-01-01 00:50:02       39 阅读
  7. 探索抖音详情API:塑造未来的媒体交互

    2024-01-01 00:50:02       41 阅读