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;
}
PTA第八章7-2 统计英文单词个数相关推荐
- split函数python统计英文单词_Python实现统计英文单词个数及字符串分割代码
Python实现统计英文单词个数及字符串分割代码 这篇文章主要介绍了Python实现统计英文单词个数及字符串分割方法,本文分别给出代码实例,需要的朋友可以参考下 字符串分割 代码如下: str=&qu ...
- 7-5-1 map 统计英文单词个数
7-5-1 map 统计英文单词个数 给出一篇英文文章,现在需要统计文章中出现英文单词的数量. 输入格式: 第一行一个T,代表数据组数 对于每组数据,第一行一个n,代表文章中单词的个数,其后n行每行一 ...
- python统计英文单词个数_统计英文单词的个数的python代码 及 字符串分割
字符串分割 str="a|and|hello|||ab" alist = str.split('|') print alist结果 str="a hello{这里换成5个 ...
- python英文字符串单词个数统计_Python实现统计英文单词个数及字符串分割代码
字符串分割 代码如下: str="a|and|hello|||ab" alist = str.split('|') print alist 结果 代码如下: str="a ...
- python统计英文单词个数_Python实现统计英文单词个数及字符串分割代码
字符串分割 代码如下: str="a|and|hello|||ab" alist = str.split('|') print alist 结果 代码如下: str="a ...
- PTA map 统计英文单词个数
给出一篇英文文章,现在需要统计文章中出现英文单词的数量. 输入格式: 第一行一个T,代表数据组数 对于每组数据,第一行一个n,代表文章中单词的个数,其后n行每行一个只包含小写字母的长度为1到10的字符 ...
- PTA 7-14 统计英文单词个数
7-14 统计英语单词个数 英语王老师正在开发一个英语作文线上系统,需要自动计算文中的单词个数.在最初的版本中,王老师并不打算加入拼写错误检查.因此,连续的英文字母都被认为是一个"单词&qu ...
- 简单的统计英文单词个数
给出一篇英文文章,现在需要统计文章中出现英文单词的数量. 输入格式: 第一行一个T,代表数据组数 对于每组数据,第一行一个n,代表文章中单词的个数,其后n行每行一个只包含小写字母的长度为1到10的字符 ...
- 5 统计英文单词个数
给出一篇英文文章,现在需要统计文章中出现英文单词的数量. 输入格式: 第一行一个T,代表数据组数 对于每组数据,第一行一个n,代表文章中单词的个数,其后n行每行一个只包含小写字母的长度为1到10的字符 ...
最新文章
- 10个月产品演化之路-快速试错,快速反应,探索产品成功之道
- 使用PyCharm定义QQ变量
- IOS经常使用的性能优化策略
- TreeMap、HashMap、LinkedHashMap的区别
- html自动计时器,html计时器
- 学计算机的逻辑学博士,逻辑学博士点
- Postman,Insomnia用户登录请求验证码错误的原因
- 彻底搞懂vertical-align 底线、基线、中线的含义
- SQL Server卸载不干净和重新安装问题
- winForm c#导出Excel
- js的变量命名需要遵循什么规则?
- AWE2021:加速拥抱数字化 开启智慧生活新纪元
- 《一名网络工程师的自我修养》--子网划分
- Unity Shader:实现菲涅尔+色散效果以及相关原理解析
- delphiXE关于线程和多线程、线程的同步与异步执行
- 使用idea搭建SSM框架,并成功运行。
- 信任别人计算机和网络,【媒库文选】人们可能更信任计算机而不是人类
- 让动画不再僵硬:Facebook Rebound Android动画库介绍
- xss漏洞-DVWA跨站攻击盗取用户cookie值
- 安全函数最全总结:字符串拷贝,内存拷贝,字符串格式化等