讲话模式
讲话模式
原题链接:https://oj.haizeix.com/problem/577
每个人讲话都有口头禅,现在给出一个字符串,需要求出其中出现次数最多的单词。
输入:
输入一行,若干个长度小于或等于 10 的只包含大小写字母的字符串。
输出:
输出一行,为出现次数最多的单词和它出现的次数,以一个空格隔开。如果出现次数最多的单词有多个,则输出字典序最小的那个。这个单词必须完全以小写的形式输出。注意,所说的单词不区分大小写字母。
样例:
1Can a can can a can It can
1can 5
代码
解法1:定义两个变量string、int,轮回最终保留一个出现次数最多的结果,如果有多个则保留字典序最小的单词。
解法2:当处理完所有的数据后,遍历一遍数组保留最终的结果。
123456789101112131415161718192021222324252627282930#include<iostream>#include<map>#include<string>using namespace std;map<string, int> mp;string ans;//最终答案int count;//答案出现的次数int main() { string t; //大小写转换 while (cin >> t) { for (int i = 0; i < t.size(); ++i) { if (t[i] >= 'A' && t[i] <= 'Z') { t[i] += ('a' - 'A'); } } mp[t]++; } //数据处理 使用有序map for (auto it = mp.begin(); it != mp.end(); ++it) { if (it->second > count) { ans = it->first; count = it->second; } } cout << ...
上网统计
上网统计
原题链接:https://oj.haizeix.com/problem/566
在一个网络系统中有 N 个用户、 M 次上网记录。每个用户可以自己注册一个用户名,每个用户名只包含小写字母且长度小于 1000 的字符串,每次上网日志里都会有记录,现在请统计每个用户每次浏览了多少个网页。注意,有可能有用户没有访问记录。
输入:
第 1 行包含两个正整数 N (1 ≤ N ≤ 1000) 和 M (1 ≤ M ≤ 5000)。
第 2 ~ M+1 行,每行包含 2 个字符串,分别表示用户名和浏览的网页名。
输出:
共 x 行,x 表示有访问记录的用户数量,
每行的第一个字符串时用户名,接下来的若干字符串是这个用户依次浏览的网页名(之间用一个空格隔开)。
按照用户名出现的次序排序输出。
样例:
123456785 7goodstudyer bookshopalikespacea spacewebgoodstudyer bookshopblikespaceb spaceweblikespacec spaceweblikespacea juiceshopgameplayer gameweb
12345goodstudyer bookshopa bookshopblikespacea spaceweb juiceshoplikespaceb spaceweblikespacec spacewebgameplayer gameweb
代码12345678910111213141516171819202122232425262728293031323334353637#include<iostream>#include<string>#include<vector>#include<unordered_map>using namespace std;int cnt;//当前用户访问记录所在的行数int n;//用户的个数int recordNum;//上网记录的条数unordered_map<string, int> mp;//用于记录vector在第几行的信息(unordered_map满足按用户名出现的次序进行输出)int main() { cin >> n >> r ...