[POJ2491 Scavenger Hunt、POJ2643 Election]

[关键字]:STL中map的应用

[题目大意]:因为是两道题就不一一写了,自己看吧都很水。

//================================================

[代码]:因为很水而且只是为了练习一下map所以没有分析直接上代码了

POJ2491
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<string>
#include<map>
using namespace std;

int test,st,tot,n;
bool b[500];
string s1,s2,t;
map<string,int> m1;
map<int,string> m2;
map<string,string> m3;

int main()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
scanf("%d",&test);
for (int tt=1;tt<=test;++tt)
{
printf("Scenario #%d:\n",tt);
m1.clear(),m2.clear(),m3.clear();
tot=0;
scanf("%d",&n);
memset(b,1,sizeof(b));
for (int i=1;i<n;++i)
{
cin>>s1>>s2;
if (m1.find(s1)==m1.end()) m1[s1]=++tot,m2[tot]=s1;
if (m1.find(s2)==m1.end()) m1[s2]=++tot,m2[tot]=s2;
b[m1[s2]]=0;
m3[s1]=s2;
}
for (st=1;st<=tot;++st)
if (b[st]) break;
string t=m2[st];
while (m3.find(t)!=m3.end())
{
cout<<t<<endl;
t=m3[t];
}
cout<<t<<endl<<endl;
}
system("pause");
return 0;
}
POJ2643
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<map>
using namespace std;

int n,m,a[1100],MAX,MAXJ;
string s1,s2;
char ch;
bool f;
map<string,int> MAP1;
map<int,string> MAP2;

int main()
{
scanf("%d",&n);
scanf("%c",&ch);
for (int i=1;i<=n;++i)
{
getline(cin,s1);
getline(cin,s2);
MAP1[s1]=i;
MAP2[i]=s2;
}
scanf("%d",&m);
scanf("%c",&ch);
for (int i=1;i<=m;++i)
{
getline(cin,s1);
++a[MAP1[s1]];
}
MAX=-1;
for (int i=1;i<=n;++i)
if (MAX<a[i]) MAX=a[i],MAXJ=i,f=1; else
if (MAX==a[i]) f=0;
if (!f) printf("tie\n"); else cout<<MAP2[MAXJ]<<endl;
system("pause");
return 0;
}




你可能感兴趣的:(poj)