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;
}

 

好的,以下是一篇英文文章需要统计出现英文单词数量: Title: The Benefits of Reading Reading is an activity that has numerous benefits for individuals of all ages. Whether you are a child just learning to read or an adult looking to expand your knowledge, reading can provide a wealth of advantages. One of the primary benefits of reading is that it can improve your cognitive abilities. When you read, your brain is forced to process new information and make connections between different ideas. This can help to improve your memory, increase your attention span, and enhance your critical thinking skills. In addition to these cognitive benefits, reading can also have a positive impact on your emotional well-being. Many people find that reading can be a form of escapism, allowing them to temporarily forget about their problems and immerse themselves in a different world. This can help to reduce stress and anxiety, and promote feelings of relaxation and calm. Furthermore, reading can also be a great way to expand your knowledge and learn new things. Whether you are interested in history, science, or literature, there are countless books available on a wide range of topics. By reading regularly, you can broaden your horizons and gain a deeper understanding of the world around you. Overall, there are many benefits to reading, both for your mind and your emotions. So why not pick up a book today and start reaping the rewards? 以上是一篇英文文章需要统计出现英文单词数量
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值