PTA词频统计

作者 DS课程组

单位 浙江大学

请编写程序,对一段英文文本,统计其中所有不同单词的个数,以及词频最大的前10%的单词。

所谓“单词”,是指由不超过80个单词字符组成的连续字符串,但长度超过15的单词将只截取保留前15个单词字符。而合法的“单词字符”为大小写字母、数字和下划线,其它字符均认为是单词分隔符。

输入格式:

输入给出一段非空文本,最后以符号#结尾。输入保证存在至少10个不同的单词。

输出格式:

在第一行中输出文本中所有不同单词的个数。注意“单词”不区分英文大小写,例如“PAT”和“pat”被认为是同一个单词。

随后按照词频递减的顺序,按照词频:单词的格式输出词频最大的前10%的单词。若有并列,则按递增字典序输出。

输入样例:

This is a test.

The word "this" is the word with the highest frequency.

Longlonglonglongword should be cut off, so is considered as the same as longlonglonglonee.  But this_8 is different than this, and this, and this...#
this line should be ignored.

输出样例:

(注意:虽然单词the也出现了4次,但因为我们只要输出前10%(即23个单词中的前2个)单词,而按照字母序,the排第3位,所以不输出。)

23
5:this
4:is

思路:

切割开每一个字符串,然后统计和排序。

方法:

先预处理,然后用string流切割,最后排序一下就结束。

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl "\n"

const ll N = 1e4+7;
string s;
map<string,ll>mp;

struct node{
	ll x;
	string str;
}v[N];

bool cmp(node a,node b){//排序规则 
	if(a.x != b.x)
		return a.x > b.x;
	return a.str < b.str;
}

void solve(){
	ll x=0;
	while(getline(cin,s)){
		if(s[s.size()-1] == '#')x=1,s[s.size()-1]=' ';//看有没有结束 
		for(ll i = 0 ; i < s.size() ; i ++)//把不要的字符变成' ',把字符小写 
			!isalnum(s[i]) && s[i] != '_' ? s[i]=' ' : s[i]=tolower(s[i]);
		stringstream ss(s);//字符流 
		while(ss >> s)//用substr截取前15个字符 
			s.size() <= 15 ? mp[s]++ : mp[s.substr(0,15)]++;
		if(x)break;//判断结束 
	}
	for(auto i : mp)//从第一个点开始存方便最后输出 
		v[x].str = i.first,v[x].x = i.second,x++;
	sort(v+1,v+x+1,cmp);//排序 
	cout << x-1 << endl;//从1开始,所以要减1 
	ll len=x/10;//需要输出的字符串个数 
	for(ll i = 1 ; i <= len ; i ++)//输出 
		cout << v[i].x << ":" << v[i].str << endl;
	return;
}

int main(){
	ll t=1;//cin >> t;
	while(t --)solve();
	return 0;
}
  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1 创建L4 1项目 2 给定一个存放了三段文章的字符串数组 点击 “文件显示”按钮 将第一段文章内容显示在文本框中 再次点击“文件显示”按钮 将第二段文章显示在文本框中 以此类推 filestr[0] "Singles Day in China is the celebration or mourning of being unattached Started by students in Nanjing in the mid 1990s the date was selected in observation of its four solitary digits "; filestr[1] "While relatively obscure in most other countries Singles Day is likely to increase in prominence as more single men in China are unable to find female partners According to a recent study by the Chinese Academy of Social Sciences more than 24 million Chinese men could find themselves without spouses by 2020 "; filestr[2] "Celebrating Singles Day comes in many forms across the Asian nation and like Christmas and Valentine"s Day it has become a multi million dollar industry "; 注意:利用给定文章或者自选文章都可以 注意我们只讨论简单的情况 文章中只有逗号和句号两种特殊符号 2 点击“词频统计”按钮 系统将文本框中的字符串包括的单词及其个数统计出来 如图2所示 提示1:文章需要经过若干次处理 大小写处理 替换特殊符号处理 分隔单词处理 提示2:利用hashtable进行统计 单词作为key 个数作为value foreach string i in strword if ht Contains i ht Add i 1 ; else ht[i] int ht[i] + 1; 提示3:格式化输出统计结果 string str string Format "{0 15} {1 3}" de Key de Value ; txtFile Text txtFile Text +str + " r n";">1 创建L4 1项目 2 给定一个存放了三段文章的字符串数组 点击 “文件显示”按钮 将第一段文章内容显示在文本框中 再次点击“文件显示”按钮 将第二段文章显示在文本框中 以此类推 filestr[0] "Singles Day in China is [更多]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值