PTA第八章7-2 统计英文单词个数

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

输入格式:

第一行一个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

THE FIRST:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
	int T,n;
	cin>>T;
	vector<string> v1;
	vector<string> v2; 
 	string temp_in,temp_out;
	while(T != 0)
	{
		cin>>n;
		for(int i = 0; i < n; i++)
		{
			cin>>temp_in;
			v1.push_back(temp_in); 
		}
		sort(v1.begin(),v1.end());
		temp_out = v1[0];
		int count = 1;
		for(int i = 1; i < n; i++)
		{
			if(temp_out == v1[i])
			{
				count++;
			}else
			{
				cout<<temp_out<<" "<<count<<endl;
				temp_out = v1[i];
				count = 1;
			}
		}
        cout<<temp_out<<" "<<count<<endl;
		T--;
	}
	return 0;
}

输出与样例相同,但结果是 前三个运行超时,后两个答案错误;没找到原因;

找到原因:又忘记清空数组了,每次都忘记QWQ

THE SCOND:

看了老师提示,使用map,map的值与键不相等,用map更轻松

#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
int main()
{
	int T,n;
	cin>>T;
//	vector<string> v1;
//	vector<string> v2; 
	map<string, int> v3;
 	string temp_in,temp_out;
	while(T != 0)
	{
		cin>>n;
		for(int i = 0; i < n; i++)
		{
			cin>>temp_in;
			v3[temp_in] += 1; 
		}
//		sort(v1.begin(),v1.end());
//		temp_out = v1[0];
//		int count = 1;
		cout<<"test"<<endl;
		for(auto it = v3.begin(); it != v3.end(); it++)
		{
//			if(temp_out == v1[i])
//			{
//				count++;
//			}else
//			{
				cout<<it.first<<" "<<it.second<<endl;
//				temp_out = v1[i];
//				count = 1;
//			}
		}
//		cout<<temp_out<<" "<<count<<endl;
		T--;
	}
	return 0;
}

cout<<it.first<<" "<<it.second<<endl;

[Error] 'struct std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, int> >' has no member named 'first'

[错误]“struct std::\u Rb\u tree\u iterator<std::pair<const std::basic\u string<char>,int>>”没有名为“first”的成员 

[Error] 'struct std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, int> >' has no member named 'second'

课本第253页是

auto pa = *it;

cout<<pa.first<<" "<<pa.second<<endl; 

如果换成cout<<*it.first<<" "<<*it.second<<endl;

还是与上面相同的编译错误

THE LAST:

加入clear()后没有再测试用vector的程序是否运行通过

#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
int main()
{
	int T,n;
	cin>>T;
//	vector<string> v1;
//	vector<string> v2; 
	map<string, int> v3;
 	string temp_in,temp_out;
	while(T != 0)
	{
		cin>>n;
		for(int i = 0; i < n; i++)
		{
			cin>>temp_in;
			v3[temp_in] += 1; 
		}
//		sort(v1.begin(),v1.end());
//		temp_out = v1[0];
//		int count = 1;
// 		cout<<"test"<<endl;
		for(auto it = v3.begin(); it != v3.end(); it++)
		{
//			if(temp_out == v1[i])
//			{
//				count++;
//			}else
//			{
				auto pa = *it;
				cout<<pa.first<<" "<<pa.second<<endl;
//				temp_out = v1[i];
//				count = 1;
//			}
		}
//		cout<<temp_out<<" "<<count<<endl;
		T--;
        v3.clear();
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值