map使用及例题

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

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

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

STL很强大!

hdu1004:

[cpp] view plain copy
#include
#include
#include
using namespace std;
mapM;
map::iterator p,q;
int n;
int main()
{
string str;
while(scanf("%d",&n),n)
{
M.clear(); //清空
while(n–)
{
cin>>str;
if(M[str]==0) //插入
M[str]=1;
else
M[str]++;
}
int k=-1;
for(p=M.begin();p!=M.end();p++) //查找
{
if((p->second)>k)
{
k=p->second;
q=p;
}
}
cout }
return 0;
}
#include
#include
#include
using namespace std;
mapM;
map::iterator p,q;
int n;
int main()
{
string str;
while(scanf("%d",&n),n)
{
M.clear(); //清空
while(n–)
{
cin>>str;
if(M[str]==0) //插入
M[str]=1;
else
M[str]++;
}
int k=-1;
for(p=M.begin();p!=M.end();p++) //查找
{
if((p->second)>k)
{
k=p->second;
q=p;
}
}
cout }
return 0;
}

然后是hdu1075

[cpp] view plain copy
#include
#include
#include
#include
#include
using namespace std;
map m;
map::iterator it;
char c[3020];
int main()
{
char s[22],t[22],tmp[22];
scanf("%s",s);
while(scanf("%s",s)!=EOF)
{
if(!strcmp(s,“END”))
break;
scanf("%s",t);
m[t]=s;
}
scanf("%s",s);
getchar();
while(gets©)
{
if(!strcmp(c,“END”))
break;
int len=strlen©;
int j=0;
for(int i=0;i {
if(c[i]>=‘a’&&c[i]<=‘z’)
{
tmp[j]=c[i];
j++;
}
else
{
tmp[j]=’\0’;
it=m.find(tmp);
if(it!=m.end())
{
char ww[22];
//ww=it->second;
cout //printf("%s",it->second);
}
else
{
printf("%s",tmp);
}
printf("%c",c[i]);
j=0;
}
}
printf("\n");
}
return 0;
}
#include
#include
#include
#include
#include
using namespace std;
map m;
map::iterator it;
char c[3020];
int main()
{
char s[22],t[22],tmp[22];
scanf("%s",s);
while(scanf("%s",s)!=EOF)
{
if(!strcmp(s,“END”))
break;
scanf("%s",t);
m[t]=s;
}
scanf("%s",s);
getchar();
while(gets©)
{
if(!strcmp(c,“END”))
break;
int len=strlen©;
int j=0;
for(int i=0;i {
if(c[i]>=‘a’&&c[i]<=‘z’)
{
tmp[j]=c[i];
j++;
}
else
{
tmp[j]=’\0’;
it=m.find(tmp);
if(it!=m.end())
{
char ww[22];
//ww=it->second;
cout //printf("%s",it->second);
}
else
{
printf("%s",tmp);
}
printf("%c",c[i]);
j=0;
}
}
printf("\n");
}
return 0;
}

最后hdu1263

[cpp] view plain copy
#include
#include
#include
#include
using namespace std;

int main()
{
map > a;
map b;
map >::iterator i;
map::iterator j;
int T,m,tmp;string s,tb;
cin>>T;
while(T–)
{
a.clear();
b.clear();
cin>>m;
while(m–)
{
cin>>s>>tb>>tmp;
a[tb][s]+=tmp;
}
for(i=a.begin();i!=a.end();i++)
{
cout b=i->second;
for(j=b.begin();j!=b.end();j++)
{
cout<<" |----" }
}
if(T!=0)
printf("\n");
}
}
#include
#include
#include
#include
using namespace std;

int main()
{
map > a;
map b;
map >::iterator i;
map::iterator j;
int T,m,tmp;string s,tb;
cin>>T;
while(T–)
{
a.clear();
b.clear();
cin>>m;
while(m–)
{
cin>>s>>tb>>tmp;
a[tb][s]+=tmp;
}
for(i=a.begin();i!=a.end();i++)
{
cout b=i->second;
for(j=b.begin();j!=b.end();j++)
{
cout<<" |----" }
}
if(T!=0)
printf("\n");
}
}

你可能感兴趣的:(算法)