Let the Balloon Rise HDU
来源:互联网 发布:java读取身份证阅读器 编辑:程序博客网 时间:2024/05/16 14:00
刚开始接触map后,拿到这道题真的无法下手。
学长们讲map,他们自己知道是怎么回事,但讲不清楚。
经过好几天的努力,自己终于把map的一些东西搞清楚了,然后去写了这道题,我尽量把自己知道的map讲的清楚些。
我们看啊,map的分类的STL,什么是STL?那就是模板的意思,既然是模板,我们就不必知道内部的构造是什么了,了解它的原理就可以用了。
来个扯远的话题哈
既然是容器,那么就可以嵌套使用 怎么嵌套呢?很简单,把一个map套在另一个map里就行了
#include<bits/stdc++.h>map<string,map<string,int> > mp;
这里的赋值只讲数组赋值,因为好用而且用的多
int a;mp[string1][string2]=a;
查找,用find(),end(),和迭代器
这个是整体的map
map<string,map<string,int> > ::iterator it;
接下来就是整体map里面的小map了
map<string,int> ::iterator its;
活齐了,开始查找输出吧
for(it=mp.find();it!=mp.end();it++){ for(its=it->second.find();its!=it->second.end;its++) { }}
见到的map嵌套大概就用这么多
回归
map真的很神奇 不用管它是怎么实现的,你只需知道,当你往map里输入内容时,它就像我们人类的大脑一样,在读输入的东西的时候就按照字典序排好。(当然了,map还会标记哦,用count函数来判定关键字是否出现,其缺点是无法定位数据出现位置,但由于map的特性,即一对一的映射关系,就决定了count函数的返回值只有两个,要么是0,要么是1,出现的情况,当然是返回1了,哈哈)
好了好了,讲的差不多了(可能讲的不好,也没讲全) 来看看这道题
多组数据输入,输出 出现次数 最多 的元素
## 思路
- 输入字符串
- 存储字符串
进行字符串的出现次数比较
代码如下
#include<bits/stdc++.h>using namespace std;int main(){ int N; while(cin>>N&&N) { map<string,int> mp; vector<string> vs; string color,ss; for(int i=0; i<N; i++) { cin>>color; vs.push_back(color); mp[color]=0; //数组形式赋值 } for(int i=0; i<N; i++) { if(mp.find(vs[i])!=mp.end()) //查找元素,直到最后一个 ++mp[vs[i]]; //怎么样,很神奇吧 } //有没有发现vector也很神奇,其变量可以直接拿来当数组用 int maxn=mp[vs[0]]; ss=vs[0]; //用另一个变量将color储存起来 for(int i=1; i<N; i++) { if(mp[vs[i]]>maxn) { maxn=mp[vs[i]]; ss=vs[i]; } } cout<<ss<<endl; mp.clear(); //清空map vs.clear(); //清空vector } return 0;}
这道题到这里就结束了
以上就是自己探索的map的用法,有什么不正确的地放或不懂得评论就行了
阅读全文
0 0
- hdu Let the Balloon Rise
- HDU Let the Balloon Rise
- HDU:Let the Balloon Rise
- Hdu Let the Balloon Rise
- Let the Balloon Rise HDU
- hdu Let the Balloon Rise
- HDU 1004 Let the Balloon Rise
- HDU 1004 Let the Balloon Rise
- hdu 1004 Let the Balloon Rise
- HDU 1004 Let the Balloon Rise
- HDU--1004 Let the Balloon Rise
- hdu 1004 Let the Balloon Rise
- hdu 1004.Let the Balloon Rise
- hdu 1004 Let the Balloon Rise
- HDU 1004 Let the Balloon Rise
- hdu 1004 Let the Balloon Rise
- HDU 1004 Let the Balloon Rise
- HDU 1004 Let the Balloon Rise
- 面向对象连环踢
- VPP主要结构体分析
- intellij IDEA 无法使用gradle的原因(17-7-27)
- linux安装mongodb并创建用户
- HDU6047 Maximum Sequence(贪心,2017 HDU多校联赛 第2场)
- Let the Balloon Rise HDU
- Android消息队列和Looper及IntentService
- CSS中p和span有什么区别
- MFC中点击按钮弹出对话框
- 取汉字拼音首字母--生成不重复ID(汉字--拼音--首字母)
- Unity_面向对象设计原则_026
- [日常训练] 小Y包汤圆
- MySQL的sql_mode 模式修改 my.cnf
- python文件的基础及文件读入合并操作使用实例