需要自己写一个hash函数,直接用map的话最后一个case会超时
#include<map>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
typedef long long LL;
const int maxn = 5e5 + 10;
int n, k, x, y, tot, M[maxn];
char s[maxn];
vector<int> t[maxn];
int get(char *s)
{
return (s[0] - 'A') * 6760 + (s[1] - 'A') * 260 + (s[2] - 'A') * 10 + s[3] - '0';
}
int main()
{
scanf("%d%d", &n, &k);
for (int i = 1; i <= k; i++)
{
scanf("%d%d", &x, &y);
while (y--)
{
scanf("%s", s);
int k = get(s);
if (!M[k]) M[k] = ++tot;
t[M[k]].push_back(x);
}
}
for (int i = 1; i <= tot; i++) sort(t[i].begin(), t[i].end());
while (n--)
{
scanf("%s", s);
printf("%s", s);
int k = M[get(s)];
if (k)
{
printf(" %d", t[k].size());
for (int i = 0; i < t[k].size(); i++)
{
printf(" %d", t[k][i]);
}
}
else printf(" 0");
printf("\n");
}
return 0;
}
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
//1039 最后一个case超时
map<string,vector<int> > mapStu;
int n,k,x,y;
string name;
bool cmp(int a,int b){
return a<b;
}
int main(){
scanf("%d %d",&n,&k);
while(k--){
scanf("%d %d",&x,&y);
while(y--){
cin>>name;
mapStu[name].push_back(x);
}
}
while(n--){
cin>>name;
map<string,vector<int> >::iterator iter;
iter=mapStu.find(name);
if(iter!=mapStu.end()){
vector<int>:: iterator iter_vec;
printf("%s %d",name.c_str(),mapStu[name].size());
sort(mapStu[name].begin(),mapStu[name].end(),cmp);
for(iter_vec=mapStu[name].begin();iter_vec!=mapStu[name].end();iter_vec++)
printf("% d",*iter_vec);
}
else
printf("%s %d",name.c_str(),0);
printf("\n");
}
return 0;
}