您的位置:首页 > 编程语言 > C语言/C++

【C】C++标准模板库(STL)介绍--map

2018-02-28 15:42 381 查看
4.map--映射it->first it->second.find(key)---返回键为key的迭代器.erase(it).erase(key).erase(first,last).size().clear()

1100. Mars Numbers (20)

时间限制 400 ms
内存限制 65536 kB
代码长度限制 16000 B
判题程序 Standard 作者 CHEN, Yue
People on Mars count their numbers with base 13: Zero on Earth is called "tret" on Mars.
The numbers 1 to 12 on Earch is called "jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec" on Mars, respectively.
For the next higher digit, Mars people name the 12 numbers as "tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou", respectively.
For examples, the number 29 on Earth is called "hel mar" on Mars; and "elo nov" on Mars corresponds to 115 on Earth. In order to help communication between people from these two planets, you are supposed to write a program for mutual translation between Earth and Mars number systems. Input Specification:Each input file contains one test case. For each case, the first line contains a positive integer N (< 100). Then N lines follow, each contains a number in [0, 169), given either in the form of an Earth number, or that of Mars.Output Specification:For each number, print in a line the corresponding number in the other language.Sample Input:
4
29
5
elo nov
tam
Sample Output:
hel mar
may
115
13
#include<map>
#include<iostream>
#include<sstream>//getline()的头文件
#include<string>
#include<math.h>
#include<stdio.h>
using namespace std;
//0-12
string s1[13]={"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
//13的0-12倍
string s2[13]={"tret","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};
//火星转地球
map<string,int> mp;
int EorM(string str){//判断是地球数字or火星数字
char a=str[0];
if(a=='0'||a=='1'||a=='2'||a=='3'||a=='4'||
a=='5'||a=='6'||a=='7'||a=='8'||a=='9') return 1;
else return 2;
}
void initial(){
//初始化mp
int i;
for(i=0;i<13;i++){
mp[s1[i]]=i;
}
for(i=0;i<13;i++){
mp[s2[i]]=i*13;
}
}
int main(){
int n,i,j;
cin>>n;
getchar();//这里需要吸收输入完n后的回车
initial();
for(i=0;i<n;i++){
string str;
//cin>>str;
getline(cin,str);
//cin不支持录入空格,应当使用getline
if(EorM(str)==1){//地球->火星
int len=str.length();
int a=0;
for(j=len-1;j>=0;j--){
a=a+(int)(str[j]-'0')*pow(10.0,len-1-j);
}//先转换成十进制
int a1=0,a2=0;
a1=a%13;//十三进制的个位
a2=a/13;//十三进制的十位
if(a1==0) cout<<s2[a2]<<endl;//***注意这里个位不应再输出tret
else if(a2!=0) cout<<s2[a2]<<" "<<s1[a1]<<endl;
else cout<<s1[a1]<<endl;
}
else{//火星->地球
int a1,a2;
if(str.find(" ")!=string::npos){//说明中间有空格,由两个单词组成,且第一个不为tret***
string strr=str.substr(0,3);//第一个单词一定是三位,从0开始取三位
int a2=mp[strr];
str.erase(str.begin(),str.begin()+str.find(" ")+1);//将第一个单词删除
int a1=mp[str];//删除后只剩第二个单词
int a=a1+a2;
cout<<a<<endl;
}
else{
cout<<mp[str]<<endl;
}
}
}
return 0;
}

1054. The Dominant Color (20)

时间限制 100 ms
内存限制 65536 kB
代码长度限制 16000 B
判题程序 Standard 作者 CHEN, Yue
Behind the scenes in the computer's memory, color is always talked about as a series of 24 bits of information for each pixel. In an image, the color with the largest proportional area is called the dominant color. A strictly dominant color takes more than half of the total area. Now given an image of resolution M by N (for example, 800x600), you are supposed to point out the strictly dominant color.Input Specification:Each input file contains one test case. For each case, the first line contains 2 positive numbers: M (<=800) and N (<=600) which are the resolutions of the image. Then N lines follow, each contains M digital colors in the range [0, 224). It is guaranteed that the strictly dominant color exists for each input image. All the numbers in a line are separated by a space.Output Specification:For each test case, simply print the dominant color in a line.Sample Input:
5 3
0 0 255 16777215 24
24 24 0 0 24
24 0 24 24 24
Sample Output:
24
#include<map>
#include<stdio.h>
using namespace std;
map<int,int> mp;
//2^24 int类型够了
int main(){
int m,n,i,j;
scanf("%d %d",&m,&n);
for(i=0;i<n;i++){
for(j=0;j<m;j++){
int a;
scanf("%d",&a);
if(mp.find(a)!=mp.end()) mp[a]++;
else mp[a]=1;
}
}
int max=0,k;
map<int,int>::iterator it;
for(it=mp.begin();it!=mp.end();it++){
if(it->second>max){
max=it->second;
k=it->first;
}
}
printf("%d",k);
return 0;
}

1071. Speech Patterns (25)

时间限制 300 ms
内存限制 65536 kB
代码长度限制 16000 B
判题程序 Standard 作者 HOU, Qiming
People often have a preference among synonyms of the same word. For example, some may prefer "the police", while others may prefer "the cops". Analyzing such patterns can help to narrow down a speaker's identity, which is useful when validating, for example, whether it's still the same person behind an online avatar.Now given a paragraph of text sampled from someone's speech, can you find the person's most commonly used word?Input Specification:Each input file contains one test case. For each case, there is one line of text no more than 1048576 characters in length, terminated by a carriage return '\n'. The input contains at least one alphanumerical character, i.e., one character from the set [0-9 A-Z a-z].Output Specification:For each test case, print in one line the most commonly occurring word in the input text, followed by a space and the number of times it has occurred in the input. If there are more than one such words, print the lexicographically smallest one. The word should be printed in all lower case. Here a "word" is defined as a continuous sequence of alphanumerical characters separated by non-alphanumerical characters or the line beginning/end.Note that words are case insensitive. Sample Input:
Can1: "Can a can can a can?  It can!"
Sample Output:
can 5
#include<map>
#include<string>
#include<sstream>
#include<iostream>
using namespace std;
map<string,int> mp;
int youxiao(char a){
if(a>='a'&&a<='z') return 1;
else if(a>='A'&&a<='Z') return 2;
else if(a>='0'&&a<='9') return 1;
else return 3;//无效字符
}
string deal(string &str){
int len=str.length();
int i=0;
string strr;
for(i=0;i<len;i++){
if(youxiao(str[i])==1) strr=strr+str.substr(i,1);
else if(youxiao(str[i])==2){
strr[i]=str[i]-'A'+'a';
strr=strr+str.substr(i,1);
}//注意substr的用法是起始位置+字符串长度
}
return strr;//返回的有可能是空的
}//将取到的片段中,大写换小写,去掉其他符号
int main(){
string str;
getline(cin,str);
while(str.length()!=0){
string strr;
int i=0;
while(i<str.length()&&youxiao(str[i])!=3) i++;
strr=str.substr(0,i);
strr=deal(strr);//大写换小写
if(strr.size()!=0&&mp.find(strr)!=mp.end()) mp[strr]++;
else if(strr.size()!=0) mp[strr]=1;
str.erase(0,i);
while(str.length()>0&&youxiao(str[0])==3) str.erase(0,1);
}
int max=0;
string strr;
map<string,int>::iterator it;
for(it=mp.begin();it!=mp.end();it++){
if(it->second>max){
max=it->second;
strr=it->first;
}
}
if(max!=0) cout<<strr<<" "<<max<<endl;
return 0;
}//map已经按字典序排好序了
注:以上运行超时,不知道是什么原因。。。
#include<map>
#include<string>
#include<sstream>
#include<iostream>
using namespace std;
map<string,int> mp;
bool youxiao(char &a){
if(a>='0'&&a<='9') return true;
else if(a>='a'&&a<='z') return true;
else if(a>='A'&&a<='Z'){
a=a-'A'+'a';
return true;
}
else return false;
}
int main(){
string str;
getline(cin,str);
int i=0;
while(i<str.length()){
string strr;
while(i<str.length()&&youxiao(str[i])){
strr+=str[i];
i++;
}
if(strr.size()!=0&&mp.find(strr)!=mp.end()) mp[strr]++;
else if(strr.size()!=0) mp[strr]=1;
while(i<str.length()&&!youxiao(str[i])) i++;//这个循环易忘
}
int max=0;
string strr;
map<string,int>::iterator it;
for(it=mp.begin();it!=mp.end();it++){
if(it->second>max){
max=it->second;
strr=it->first;
}
}
if(max!=0) cout<<strr<<" "<<max<<endl;
return 0;
}//map已经按字典序排好序了

1022. Digital Library (30)

时间限制 1000 ms
内存限制 65536 kB
代码长度限制 16000 B
判题程序 Standard 作者 CHEN, Yue
A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you are supposed to output the resulting books, sorted in increasing order of their ID's. Input Specification:Each input file contains one test case. For each case, the first line contains a positive integer N (<=10000) which is the total number of books. Then N blocks follow, each contains the information of a book in 6 lines:Line #1: the 7-digit ID number;
Line #2: the book title -- a string of no more than 80 characters;
Line #3: the author -- a string of no more than 80 characters;
Line #4: the key words -- each word is a string of no more than 10 characters without any white space, and the keywords are separated by exactly one space;
Line #5: the publisher -- a string of no more than 80 characters;
Line #6: the published year -- a 4-digit number which is in the range [1000, 3000].
It is assumed that each book belongs to one author only, and contains no more than 5 key words; there are no more than 1000 distinct key words in total; and there are no more than 1000 distinct publishers.After the book information, there is a line containing a positive integer M (<=1000) which is the number of user's search queries. Then M lines follow, each in one of the formats shown below:1: a book title
2: name of an author
3: a key word
4: name of a publisher
5: a 4-digit number representing the year
Output Specification:For each query, first print the original query in a line, then output the resulting book ID's in increasing order, each occupying a line. If no book is found, print "Not Found" instead.Sample Input:
3
1111111
The Testing Book
Yue Chen
test code debug sort keywords
ZUCS Print
2011
3333333
Another Testing Book
Yue Chen
test code sort keywords
ZUCS Print2
2012
2222222
The Testing Book
CYLL
keywords debug book
ZUCS Print2
2011
6
1: The Testing Book
2: Yue Chen
3: keywords
4: ZUCS Print
5: 2011
3: blablabla
Sample Output:
1: The Testing Book
1111111
2222222
2: Yue Chen
1111111
3333333
3: keywords
1111111
2222222
3333333
4: ZUCS Print
1111111
5: 2011
1111111
2222222
3: blablabla
Not Found
#include<map>
#include<iostream>
#include<sstream>
#include<string>
#include<stdio.h>
#include<algorithm>
using namespace std;
multimap<string,int> title;
multimap<string,int> author;
multimap<string,int> key;
multimap<string,int> publisher;
multimap<string,int> year;
map<int,string> in;
int book[10010];
bool cmp(int a,int b){
if(a<b) return true;
else return false;
}
void print(int i,multimap<string,int> &mp,int &flag){
string strr=in[i].substr(3,in[i].length()-3);
int k=0,j=0;
//count函数求出某个键出现的次数,
//find函数返回一个迭代器,指向第一个拥有正在查找的键的实例。
if(mp.find(strr)!=mp.end()){
multimap<string,int>::iterator it=mp.find(strr);
for(k=0;k<mp.count(strr);k++,it++){
//printf("%07d\n",it->second);
book[j++]=it->second;
}
sort(book,book+j,cmp);
for(k=0;k<j;k++){
printf("%07d\n",book[k]);
}
flag=1;
}
}
void deal(string k,int id){
int j=0;
while(j<k.length()){
string strr;
while(j<k.length()&&k[j]!=' '){
strr+=k[j];
j++;
}
j++;
key.insert(pair<string,int>(strr,id));
}
}
int type(string s){
int a=(int)(s[0]-'0');
return a;
}
int main(){
int n,m,i;
cin>>n;
//getchar();
for(i=0;i<n;i++){
int id;
string t,a,k,p,y;
cin>>id;
char c=getchar();
getline(cin,t);title.insert(pair<string,int>(t,id));
getline(cin,a);author.insert(pair<string,int>(a,id));
getline(cin,k);deal(k,id);
getline(cin,p);publisher.insert(pair<string,int>(p,id));
getline(cin,y);year.insert(pair<string,int>(y,id));
//key.insert(pair<string,int>(k,id));
//keywords的添加要另外处理,因为keywords不应是一个整句子作为关键字
//multimap的添加与map不同,multimap没有定义[]操作运算
}
cin>>m;
getchar();
for(i=0;i<m;i++){
string str;
getline(cin,str);
//in[i].insert(str);
in[i]=str;
}
for(i=0;i<m;i++){
cout<<in[i]<<endl;
int a=type(in[i]);
int flag=0;
switch(a){
case 1:print(i,title,flag);break;
case 2:print(i,author,flag);break;
case 3:print(i,key,flag);break;
case 4:print(i,publisher,flag);break;
case 5:print(i,year,flag);break;
}
if(flag==0) cout<<"Not Found"<<endl;
}
return 0;
}
注:以上使用了multimap,需要额外将每一关键字的键值排序输出
#include<map>
#include<set>
#include<sstream>
#include<iostream>
#include<stdio.h>
using namespace std;
map<string,set<int> > title,author,key,publisher,year;
void print(map<string,set<int> > &mp,string &temp){//加上引用以提高速度
if(mp.find(temp)==mp.end()) cout<<"Not Found"<<endl;
else{
set<int>::iterator it;
for(it=mp[temp].begin();it!=mp[temp].end();it++){
printf("%07d\n",*it);
}
}
}
int main(){
int n,m,i,id;
//cin>>n;
scanf("%d",&n);
while(n--){
string t,a,k,p,y;
//cin>>id;
scanf("%d",&id);
getchar();
getline(cin,t);
title[t].insert(id);
getline(cin,a);
author[a].insert(id);
while(cin>>k){
key[k].insert(id);
char c=getchar();
if(c=='\n') break;
}
getline(cin,p);
publisher[p].insert(id);
getline(cin,y);
year[y].insert(id);
}
//cin>>m;
scanf("%d",&m);
while(m--){
string temp;
int type;
scanf("%d: ",&type);
getline(cin,temp);
cout<<type<<": "<<temp<<endl;
switch(type){
case 1:print(title,temp);break;
case 2:print(author,temp);break;
case 3:print(key,temp);break;
case 4:print(publisher,temp);break;
case 5:print(year,temp);break;
}
}
return 0;
}
注:相当于multimap<string,int>,字符串以及map的参数传递速度较慢,若需要作为函数的参数,尽可能加上引用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: