当前位置:网站首页>Pta7-5 Sina Weibo hot topics

Pta7-5 Sina Weibo hot topics

2022-06-10 00:19:00 Ink dyed maple

Sina Weibo can embed “ topic of conversation ”, The topic of the speech will be written in a pair of “#” Between , You can generate topic links , Click the link to see how many people are discussing the same or similar topics with themselves . Sina Weibo will also update the list of hot topics at any time , And put the hottest topics in an eye-catching position and recommend everyone to pay attention to .

This topic requires a simplified hot topic recommendation function , From a large number of English ( Because Chinese word segmentation is troublesome ) Analyze the topic in the microblog , Find the topics mentioned by the most microblogs .

Input format :
Enter description : Input first gives a positive integer N(≤10
5
), And then N That's ok , Each line gives an English microblog , Its length does not exceed 140 Characters . Anything contained in a pair of recent # The content in is considered to be a topic , Input guarantee # Pairs appear .

Output format :
The first line outputs the topics mentioned by the most microblogs , The second line outputs the number of microblogs mentioned . If such a topic is not unique , Then the topic with the smallest alphabetical order is output , And output... On the third line And k more …, among k Is the number of other hot topics . The input ensures that there is at least one topic .

Be careful : The two topics are considered to be the same , If you remove all symbols of non English letters and numbers 、 And ignore the case distinction , They are the same string ; At the same time, they have exactly the same participle . Except for the first letter of output , Keep only lowercase English letters and numbers , Separate the words in the original text with a space .

sample input :
4
This is a #test of topic#.
Another #Test of topic.#
This is a #Hot# #Hot# topic
Another #hot!# #Hot# topic
sample output :
Hot
2
And 1 more …

It refers to the practice of a guy %%%
Portal :https://blog.csdn.net/qq_48508278/article/details/119637352

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#pragma warning(disable:4996)

using namespace std;
const int N = 1e5 + 10;
typedef long long LL;
int a[N];
map<string, int>mp;
set<string>st[N];

void duru(string s,int x) {
    
	int flag = 0;
	int count = 0;
	string word = "";// Store intermediate results 
	string s1="";// Store the final result 
	s += ".";// Add an end sign after each character 
	for (int i = 0; i < s.size(); i++) {
    
		if (s[i] == '#') {
    
			count++;
			flag = 1;
			if (i < s.size() - 1)
				i++;
		}
		if(isalpha(s[i])&&flag||isdigit(s[i])&&flag){
    
			if (isdigit(s[i]))
				word += s[i];
			if (isalpha(s[i]))
				word += tolower(s[i]);// Convert to lowercase 
		}
		else if(flag){
    //** Ensure that there is a topic before proceeding to the next step **
			if (word != " ")
				s1 += word;
			word = " ";// The leading space to be added later 
		}
		if (count == 2) {
    
			// Explain a topic Judgment complete 
			flag = 0;
			count = 0;
			st[x].insert(s1);
			s1 = "";
			word = "";
		}
	}
}

int main() {
    
	int n;
	cin >> n;
	getchar();// This must be added with … Otherwise you will not read it completely orz
	for (int i = 0; i < n; i++) {
    
		char s[150];
		cin.getline(s,150 );
		duru(s, i);
		for (auto it:st[i]) {
    
/* cout << it << endl;*/
			mp[it] += 1;
		}
/* puts("");*/
	}
	int maxnum = 0;

	for (auto it : mp) {
    
		if (it.second > maxnum)maxnum = it.second;
	}
	int res = 0;
	for (map<string, int>::iterator it = mp.begin(); it != mp.end();it++) {
    
		if (it->second == maxnum) {
    
			//res++;
			string str = it->first;
			str[0] = str[0] - 32;// Convert to uppercase 
			cout << str << endl;
			cout << maxnum << endl;
			break;
		}

	}
	for (auto i : mp) {
    
		if (i.second == maxnum)res++;
	}
	if (res > 1)
		cout << "And " << res-1 << " more ...";
}
原网站

版权声明
本文为[Ink dyed maple]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206091639588613.html