map的基本应用--hdu--1004&&poj--2643
来源:互联网 发布:纵横概预算软件 编辑:程序博客网 时间:2024/06/06 01:22
两道map的基本题,需要注意的东西都在注释里了。
代码如下
hdu 1004
#include<iostream>#include<cstdio>#include<map>#include<cstring>using namespace std;int main(){ map<string,int>m; int n,i,Max; string str; while(cin>>n&&n) { m.clear(); //清空map,避免上次的数据对此次产生影响 for(i=0;i<n;i++) { cin>>str; m[str]++; //map内int型对应的初值为0 } Max=0; map<string,int>::iterator ite; //声明map的迭代器 for(ite=m.begin();ite!=m.end();ite++) //遍历map { if(ite->second>Max)//first,second都必须用"->"的形式访问,而不用"." { Max=ite->second; //访问map中的前后两个元素 str=ite->first; } } cout<<str<<endl; } return 0;}
poj 2643
#include<iostream>#include<cstring>#include<cstdio>#include<map>using namespace std; int main(){ map<string,string>m1; map<string,int>m2; string str1,str2; int i,j,k,n,m,Max,flag; while(cin>>n) { m1.clear();m2.clear(); getchar(); //首次使用getline前注意吃掉回车 for(i=0;i<n;i++) { getline(cin,str1); getline(cin,str2); m1[str1]=str2; } cin>>m; getchar(); for(i=0;i<m;i++) { getline(cin,str1); //这题目有病,把满足题目中要求的这句去掉才能过 // string s=m1[str1]; //str1对应的关键字不存在则返回空字符串 // if(s.size()==0) //continue; // if(m1.find(str1)==m1.end()) //必须调用find方法 m1[str1]==m1.end()是错的 // continue; m2[str1]++; } Max=0;flag=0; map<string,int>::iterator ite; for(ite=m2.begin();ite!=m2.end();ite++) if(ite->second>Max) { Max=ite->second; str1=ite->first; } for(ite=m2.begin();ite!=m2.end();ite++) if(ite->first!=str1&&ite->second==Max) { flag=1; break; } if(flag) cout<<"tie"<<endl; else cout<<m1[str1]<<endl; }}
0 0
- map的基本应用--hdu--1004&&poj--2643
- Map的基本应用
- poj 3096(map的应用)
- HDU 2648 map的应用
- hdu 1004 Let the Balloon Rise map的应用
- HDU 1004 Let the Balloon Rise Map的应用
- map容器的应用:hdu 1004 Let the Balloon Rise
- WindowsPhone7的Map控件基本应用.
- STL中map的基本应用
- map与set的基本应用
- POJ 2153 Rank List map的应用
- hdu 1075 map函数的应用
- HDU 4287 Intelligent IME map的应用
- hdu-1075 map映射表的应用。
- hdu-6015map容器的应用
- HDU 2072 单词数 map的应用
- HDU 4302 MAP应用
- POJ 1363 Rails (栈的基本应用)
- 练习题 斐波那契数列
- cocos2dx学习之路——毕业工作感悟(一)
- Linux Kernel —— DebugFS介绍
- 第一篇——我的博客起因
- hdu 1551(二分)
- map的基本应用--hdu--1004&&poj--2643
- UVA 11402 - Ahoy, Pirates!(线段树)
- Lua编程学习
- hd 2013 蟠桃记
- 网络流题集
- hd 2044 一只小蜜蜂
- #257 A.B
- bzoj1648 [Usaco2006 Dec]Cow Picnic 奶牛野餐
- 回调函数(求两个数的最大值,最小值,和)