上网统计

题目链接

上网统计

思路

  • vector yh[1010] 的理解:yh为1010大小的数组,每个数组元素为vector 类型,样例存储方式
cnt yh[cnt][0] yh[cnt][1] yh[cnt][2] yh[cnt][...]
0 goodstudyer bookshopa bookshopb
1 likespacea spaceweb juiceshop
2 likespaceb spaceweb
3 likespacec spaceweb
4 gameplayer gameweb
  • 学会迭代器的使用

注释代码

#include<iostream>
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
vector <string> yh[1010];
int n, m;
int main()
{
	cin>>n>>m;
	int cnt=0;//用户数 
	string usr, web;
	for(int j=0; j<m; j++){
		cin>>usr>>web;
		bool flag=0;//标记是否存在 
		for(int i=0; i<cnt; i++){
			string st=yh[i].front();
			if(st == usr){//如能找到用户,直接将浏览记录插入到用户名的后面 
				yh[i].push_back(web);
				flag=1;
			}
		}
		if(flag==0){//如果没有找到用户,创建用户新记录 
			yh[cnt].push_back(usr);//创建新用名 
			yh[cnt++].push_back(web);//用户名后添加网页记录 
		}
			
	} 
	for(int i=0; i<n; i++){
		vector <string>::iterator it;
		for(it=yh[i].begin(); it!=yh[i].end(); it++)
			cout<<(*it)<<" ";
		cout<<endl;
	}

	return 0;
}

posted @ 2022-10-13 09:53  TFLSNOI  阅读(40)  评论(0编辑  收藏  举报