统计单词数
code
#include<cstring>
#include<iostream>
#include<vector>
#include<sstream>
#include<algorithm>
#include<iomanip>
using namespace std;
struct Node{
string str;
int time=1;
Node(string s){//initial function
str=s;
};
};
int main()
{
ios::sync_with_stdio(false);
vector<Node> vec;
string str;
getline(cin,str);
istringstream is(str);//cut sentences by spaces
string s;
int flag=0;
int i=0;
int max_len=0;
while(is>>s){
//remove '.,' and to upper
if(s.find('.')!=-1){
s.erase(s.find('.'),1);
}else if(s.find(',')!=-1){
s.erase(s.find(','),1);
}
transform(s.begin(),s.end(),s.begin(),::toupper);//source begun and end,target place,funtion point
if(s.length()>max_len){
max_len=s.length();
}
Node *n=new Node(s);
for(i=0;i<vec.size();++i){
if(n->str==vec[i].str){
flag=1;
break;
}
}
if(flag==0){
vec.push_back(*n);
}else {
vec[i].time++;
flag=0;
}
}
for(i=0;i<vec.size();++i){
cout<<setw(max_len)<<vec[i].str<<':';
for(int j=0;j<vec[i].time;++j){
cout<<'*';
}
cout<<vec[i].time<<'\n';
}
return 0;
}
des
资源限制
时间限制:1.0s 内存限制:512.0MB
问题描述
统计输入英文文章段落中不同单词(单词有大小写之分, 但统计时忽略大小写)各自出现的次数。 输入段落中所含单词的总数不超过100,最长单词的长度不超过20个字母.
输入格式
一个包含若干句子的段落, 每个句子由若干英文单词组成. 除空格, 逗号和句号外, 这些输入的句子中不含其他非字母字符, 并且, 逗号和句号紧跟在它前面的英文单词后面, 中间没有空格. 段落最后一个字符是回车符, 表示输入结束.
输出格式
若段落中共有M个不同的英文单词,则按照其在段落中出现的先后顺序输出M行,各行的格式为: 单词中所有字母均用大写形式输出(最长的单词顶格输出,它前面没有多余的空格; 其余单词与其右对齐)+冒号+N个*号+该单词在段落中的出现次数N
样例输入
This is a test. This test is easy. This is a test. This test is easy.
样例输出
THIS:****4
IS:****4
A:**2
TEST:****4
EASY:**2
作者:Jesee
出处:https://www.cnblogs.com/jeseesmith/p/15889595.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理