一、vector用法汇总
#include <cstdio>
#include<vector>
#include<iostream>
using namespace std;
int main()
{
vector<int> vi;
for(int i=1;i<=5;i++)
{
vi.push_back(i);
}
vector<int>::iterator it=vi.begin();
for(int i=0;i<5;i++)
{
//printf("%d ",*(it+i));
printf("%d ",vi[i]);
}
printf("\n");
for(vector<int>::iterator it=vi.begin();it!=vi.end();it++)
{
printf("%d ",*it);
}
printf("\n");
vi.pop_back();
cout<<vi.size()<<endl;//4
vi.clear();
cout<<vi.size()<<endl;//0
for(int i=1;i<=5;i++)
{
vi.push_back(i);
}//1 2 3 4 5
vi.insert(vi.begin()+2,-1);
for(int i=0;i<vi.size();i++)
{
printf("%d ",vi[i]);
}//1 2 -1 3 4 5
printf("\n");
vi.erase(vi.begin()+2);//1 2 3 4 5
vi.erase(vi.begin(),vi.begin()+2);
for(int i=0;i<vi.size();i++)
{
printf("%d ",vi[i]);
}//3 4 5
return 0;
}
二、例题
1039 Course List for Student (25分)
Zhejiang University has 40000 students and provides 2500 courses. Now given the student name lists of all the courses, you are supposed to output the registered course list for each student who comes for a query.
输入样例
11 5
4 7
BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
1 4
ANN0 BOB5 JAY9 LOR6
2 7
ANN0 BOB5 FRA8 JAY9 JOE4 KAT3 LOR6
3 1
BOB5
5 9
AMY7 ANN0 BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
ZOE1 ANN0 BOB5 JOE4 JAY9 FRA8 DON2 AMY7 KAT3 LOR6 NON9
输出样例
ZOE1 2 4 5
ANN0 3 1 2 5
BOB5 5 1 2 3 4 5
JOE4 1 2
JAY9 4 1 2 4 5
FRA8 3 2 4 5
DON2 2 4 5
AMY7 1 5
KAT3 3 2 4 5
LOR6 4 1 2 4 5
NON9 0
题解
#include <bits/stdc++.h>`
using namespace std;
const int N=40010; //总人数
const int M=26*26*26*10; //由姓名散列成的数字上界
vector<int> selectCourse[M]; //每个学生选择的课程编号
int getID(char name[])
{
int id=0;
for(int i=0;i<3;i++)
{
id=id*26+(name[i]-'A');
}
id=id*10+(name[3]-'0');
return id;
}
int main()
{
char name[5];
int n,k;
scanf("%d%d",&n,&k);
for(int i=0;i<k;i++)
{
int course,x;
scanf("%d%d",&course,&x);
for(int j=0;j<x;j++)
{
scanf("%s",name);
int id=getID(name);
selectCourse[id].push_back(course);
}
}
for(int i=0;i<n;i++)
{
scanf("%s",name);
int id=getID(name);
sort(selectCourse[id].begin(),selectCourse[id].end());
printf("%s %d",name,selectCourse[id].size());
for(int j=0;j<selectCourse[id].size();j++)
{
printf(" %d",selectCourse[id][j]);
}
printf("\n");
}
return 0;
}