题目:http://pat.zju.edu.cn/contests/pat-a-practise/1039
题解:
最后一个Case用map会超时,就自己写个hash即可。
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<sstream>
using namespace std;
vector<int> stu[26*26*26*10];
int hashx(char *name)
{
return (name[0]-'A')*26*26*10+(name[1]-'A')*26*10+(name[2]-'A')*10+(name[3]-'0');
}
int main()
{
int n,k,a,b,idx=0,t,len;
char ch[10];
scanf("%d%d",&n,&k);
for(int i=0; i<k; ++i)
{
scanf("%d%d",&a,&b);
for(int j=0; j<b; ++j)
{
scanf("%s",ch);
stu[hashx(ch)].push_back(a);
}
}
for(int i=0; i<n; ++i)
{
scanf("%s",ch);
t=hashx(ch);
len=stu[t].size();
if(len>0)
{
sort(stu[t].begin(),stu[t].end());
printf("%s %d",ch,len);
for(int j=0; j<len; ++j)
printf(" %d",stu[t][j]);
printf("\n");
}
else
printf("%s %d\n",ch,len);
}
return 0;
}
来源: http://blog.csdn.net/acm_ted/article/details/20297193