L2-019 悄悄关注 【字典树 或 STL(map+set)】

版权声明:本文为博主原创文章,转载请注明出处( • ̀ω•́ )✧ https://blog.csdn.net/wangws_sb/article/details/88617788

题目传送门

新浪微博上有个“悄悄关注”,一个用户悄悄关注的人,不出现在这个用户的关注列表上,但系统会推送其悄悄关注的人发表的微博给该用户。现在我们来做一回网络侦探,根据某人的关注列表和其对其他用户的点赞情况,扒出有可能被其悄悄关注的人。

输入格式:

输入首先在第一行给出某用户的关注列表,格式如下:

人数N 用户1 用户2 …… 用户N

其中N是不超过5000的正整数,每个用户ii=1, ..., N)是被其关注的用户的ID,是长度为4位的由数字和英文字母组成的字符串,各项间以空格分隔。

之后给出该用户点赞的信息:首先给出一个不超过10000的正整数M,随后M行,每行给出一个被其点赞的用户ID和对该用户的点赞次数(不超过1000),以空格分隔。注意:用户ID是一个用户的唯一身份标识。题目保证在关注列表中没有重复用户,在点赞信息中也没有重复用户。

输出格式:

我们认为被该用户点赞次数大于其点赞平均数、且不在其关注列表上的人,很可能是其悄悄关注的人。根据这个假设,请你按用户ID字母序的升序输出可能是其悄悄关注的人,每行1个ID。如果其实并没有这样的人,则输出“Bing Mei You”。

输入样例1:

10 GAO3 Magi Zha1 Sen1 Quan FaMK LSum Eins FatM LLao
8
Magi 50
Pota 30
LLao 3
Ammy 48
Dave 15
GAO3 31
Zoro 1
Cath 60

输出样例1:

Ammy
Cath
Pota

输入样例2:

11 GAO3 Magi Zha1 Sen1 Quan FaMK LSum Eins FatM LLao Pota
7
Magi 50
Pota 30
LLao 48
Ammy 3
Dave 15
GAO3 31
Zoro 29

输出样例2:

Bing Mei You

解题思路:两种做法可以用字典树标记已经被关注的人,被点赞次数多而又不在字典树中的就要输出,难点是按字典序输出,我是用优先队列做的,其实用sort直接排序也是可以的,后来看了大神的解法,发现这个题可以直接用map和set做,思路跟字典树差不多,但是要简单得多,附上两种代码如下:

//解法一:字典树
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N=1e5+7;
const int M_len=65;
struct person
{
	string name;
	int num;
}arr[N];
struct node
{
    node *nxt[M_len];
    int flag;
    node(){
        memset(nxt,0,sizeof(nxt));
        flag=0;
    }
};
node *root = new node;
void ins(char *s,node *now)
{
    int len=strlen(s);
    for(int i=0;i<len;i++)
    {
    	int to;
    	if(s[i]>='A'&&s[i]<='Z') to=s[i]-'A';
		else if(s[i]>='a'&&s[i]<='z') to=s[i]-'a'+26; 
		else if(s[i]>='0'&&s[i]<='9') to=s[i]-'0'+52;
        if(now->nxt[to]==NULL){
            now->nxt[to]=new node;
        }
        now=now->nxt[to];
    }
    now->flag=1;
}
int _find(string s)
{
    int len=s.size();
    node *now=root;
    for(int i=0;i<len;i++)
    {
    	int to;
    	if(s[i]>='A'&&s[i]<='Z') to=s[i]-'A';
		else if(s[i]>='a'&&s[i]<='z') to=s[i]-'a'+26; 
		else if(s[i]>='0'&&s[i]<='9') to=s[i]-'0'+52;
        if(now->nxt[to]==NULL) return 0;
        now=now->nxt[to];
    }
    return now->flag;
}
bool cmp(string a,string b)
{
	return a<b;
}
int n,m;
char s[10];

priority_queue <string> v;
stack <string> ss;
int main() 
{
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>s;
		ins(s,root);
	}
	cin>>m;
	int sum=0;
	for(int i=0;i<m;i++){
		cin>>arr[i].name>>arr[i].num;
		sum+=arr[i].num;
	}
	int ave=sum/m;

	for(int i=0;i<m;i++){
		if(arr[i].num>ave){
			if(!_find(arr[i].name))
			v.push(arr[i].name);

		}
	}
	int cnt=v.size();
	if(cnt==0) cout<<"Bing Mei You"<<endl;
	else{
		while(!v.empty()){
			ss.push(v.top());
			v.pop();
		}
		while(!ss.empty())
		{
			cout<<ss.top()<<endl;
			ss.pop();
		}

	}
	return 0;
}
//解法二:map+set
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
const int N=1e5+7;
const int M_len=65;
struct person
{
	string name;
	int num;
}arr[N];

int n,M;
map <string,int> m;
set <string> s;
string a;
int main() 
{
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a;
		m[a]=1;
	}
	cin>>M;
	int sum=0;
	for(int i=0;i<M;i++){
		cin>>arr[i].name>>arr[i].num;
		sum+=arr[i].num;
	}
	double ave=sum*1.0/M;
	for(int i=0;i<M;i++){
		if(m[arr[i].name]) continue;
		if(arr[i].num<ave) continue;
		s.insert(arr[i].name);
	}
	int cnt=s.size();
	if(cnt==0) cout<<"Bing Mei You"<<endl;
	else{
		set<string>::iterator it;
		for(it=s.begin();it!=s.end();it++)
			cout<<*it<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/wangws_sb/article/details/88617788