7-5-1 map 统计英文单词个数 (300分)

7-5-1 map 统计英文单词个数 (300分)

给出一篇英文文章,现在需要统计文章中出现英文单词的数量。

输入格式:

第一行一个T,代表数据组数

对于每组数据,第一行一个n,代表文章中单词的个数,其后n行每行一个只包含小写字母的长度为1到10的字符串

输出格式:

每组数据输出若干行,每行输出单词以及它出现的次数(中间空格隔开),不同单词按单词字典序从小到大输出

保证单词出现的总次数<=1e5

输入样例:

1
8
it
is
a
pen
it
is
a
dog

输出样例:

a 2
dog 1
is 2
it 2
pen 1

AC

map清空数据使用 map.clear();

#include<bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <string>
using namespace std;

int main() {
    
    
	string s;
	int t,n;
	map<string,int> m;
	map<string,int>::iterator it;
	cin>>t;
	while(t--) {
    
    
		cin>>n;
		m.clear();//不要忘了 
		while(n--) {
    
    
			cin>>s;
			m[s]++;
		}
		for(it=m.begin(); it!=m.end(); it++)
			cout<<it->first<<" "<<it->second<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45745322/article/details/112796346