STL容器(map)————HDU例题

来源:互联网 发布:淘宝中老年衣服 编辑:程序博客网 时间:2024/06/01 10:37

map 的使用  注意first 为key值  second 是value值

然后就是在杭电上头文件对于map的map<string,int >::iterator i;的操作。使用#include<cstring>会编译错误。。<string>则不会

对于hdu1263,由于map<string,int>存储是按KEY值的字母顺序排序,所以这里免去了排序的步骤。

STL很强大!

hdu1004:

[cpp] view plain copy
print?
  1. #include<iostream>  
  2. #include<string>  
  3. #include<map>  
  4. using namespace std;  
  5. map<string,int>M;  
  6. map<string,int>::iterator p,q;  
  7. int n;  
  8. int main()  
  9. {  
  10.     string str;  
  11.     while(scanf("%d",&n),n)  
  12.     {  
  13.        M.clear(); //清空  
  14.        while(n--)  
  15.        {  
  16.           cin>>str;  
  17.           if(M[str]==0) //插入  
  18.             M[str]=1;  
  19.           else   
  20.               M[str]++;  
  21.        }  
  22.        int k=-1;  
  23.        for(p=M.begin();p!=M.end();p++)   //查找  
  24.        {  
  25.          if((p->second)>k)  
  26.          {  
  27.             k=p->second;  
  28.             q=p;  
  29.          }  
  30.        }  
  31.       cout<<q->first<<endl;   
  32.     }  
  33.     return 0;  
  34. }  


 然后是hdu1075

[cpp] view plain copy
print?
  1. #include <stdlib.h>  
  2. #include <iostream>  
  3. #include <stdio.h>  
  4. #include<string>  
  5. #include<map>  
  6. using namespace std;  
  7. map<string,string> m;  
  8. map<string,string>::iterator it;  
  9. char c[3020];  
  10. int main()  
  11. {  
  12.     char s[22],t[22],tmp[22];  
  13.     scanf("%s",s);  
  14.     while(scanf("%s",s)!=EOF)  
  15.     {  
  16.         if(!strcmp(s,"END"))  
  17.             break;  
  18.         scanf("%s",t);  
  19.         m[t]=s;  
  20.     }  
  21.     scanf("%s",s);  
  22.     getchar();  
  23.     while(gets(c))  
  24.     {  
  25.         if(!strcmp(c,"END"))  
  26.             break;  
  27.         int len=strlen(c);  
  28.         int j=0;  
  29.         for(int i=0;i<len;i++)  
  30.         {  
  31.             if(c[i]>='a'&&c[i]<='z')  
  32.             {  
  33.                 tmp[j]=c[i];  
  34.                 j++;  
  35.             }  
  36.             else  
  37.             {  
  38.                 tmp[j]='\0';  
  39.                 it=m.find(tmp);  
  40.                 if(it!=m.end())  
  41.                 {  
  42.                     char ww[22];  
  43.                     //ww=it->second;  
  44.                     cout<<it->second;  
  45.                     //printf("%s",it->second);  
  46.                 }  
  47.                 else  
  48.                 {  
  49.                     printf("%s",tmp);  
  50.                 }  
  51.                 printf("%c",c[i]);  
  52.                 j=0;  
  53.             }  
  54.         }  
  55.         printf("\n");  
  56.     }  
  57.     return 0;  
  58. }  


最后hdu1263

[cpp] view plain copy
print?
  1. #include<iostream>  
  2. #include<string>  
  3. #include<cstdio>  
  4. #include<map>  
  5. using namespace std;  
  6.   
  7. int main()  
  8. {  
  9.     map<string,map<string,int> > a;  
  10.     map<string,int> b;  
  11.     map<string,map<string,int> >::iterator i;  
  12.     map<string,int>::iterator j;  
  13.     int T,m,tmp;string s,tb;  
  14.     cin>>T;  
  15.     while(T--)  
  16.     {  
  17.         a.clear();  
  18.         b.clear();  
  19.         cin>>m;  
  20.         while(m--)  
  21.         {  
  22.             cin>>s>>tb>>tmp;  
  23.             a[tb][s]+=tmp;  
  24.         }  
  25.         for(i=a.begin();i!=a.end();i++)  
  26.         {  
  27.             cout<<i->first<<endl;  
  28.             b=i->second;  
  29.             for(j=b.begin();j!=b.end();j++)  
  30.             {  
  31.                 cout<<"   |----"<<j->first<<"("<<j->second<<")"<<endl;  
  32.             }  
  33.         }  
  34.         if(T!=0)  
  35.             printf("\n");  
  36.     }  
  37. }