统计单词个数及词频(C++实现)
#include<iostream>
#include<fstream>
#include<string> using namespace std; struct Word //定义结构体
{
string word;
size_t length;
Word* next;
int repnum;//repeat number 重复次数
bool ifdel;//if delete 是否被删除
Word(string _word, size_t _lendth = , Word* _next = NULL, int _repnum = , bool _ifdel = false) :
word(_word), length(_lendth), next(_next), repnum(_repnum), ifdel(_ifdel){}
}; Word *head = NULL, *tail = NULL;
int size = ; //链表长度,即单词总个数
int delsum = ;//delete sum 被删除的总个数 void Push(const string& str, const size_t& len) //形成链表
{
if (NULL == head)
{
head = tail = new Word(str, len, NULL, , false);
}
else
{
tail->next = new Word(str, len, NULL, , false);
tail = tail->next;
}
size++;
} void Destory() //delete new
{
Word* ptr = head;
while (ptr)
{
Word* pt = ptr;
ptr = ptr->next;
delete pt;
}
head = tail = NULL;
size = ;
} void Readin(string& mystr) //read in 读入
{
string temps;
for (size_t i = ; i < mystr.length(); i++)
{
if (mystr[i] >= 'a'&&mystr[i] <= 'z' || mystr[i] >= 'A'&&mystr[i] <= 'Z')
{
temps += mystr[i];
}
else
{
if (!temps.empty())//不空的时候返回0
{ Push(temps, temps.length());
temps.erase(temps.begin(), temps.end());
}
}
}
}
void DeSame() //delete the same 删除相同的单词(不是真删,只是做标记)
{
Word* p = head;
while (p&&p->next)
{
while (p->ifdel&&p->next)
{
p = p->next;
}
Word* pt = p->next;
while (pt)
{
if (!pt->ifdel&&pt->word == p->word)
{
p->repnum++;
pt->ifdel = true;
delsum++;
}
pt = pt->next;
}
p = p->next;
}
} void Inputp(Word* warr[]) //input point 将未被“删除”的结点的指针传入数组
{
int i = ;
Word* pt = head;
while (pt)
{
if (!pt->ifdel)
{
warr[i] = pt;
i++;
}
pt = pt->next;
}
} void Sort(Word** warr, int start, int end) //将指针按其指向的结点的repnum从大到小排序,快排实现
{
int i = , j = ;
Word* key = NULL;
key = warr[start];
i = start;
j = end;
while (i<j)
{
while (warr[j]->repnum <= key->repnum&&i<j)j--;
warr[i] = warr[j];
while (warr[i]->repnum >= key->repnum&&i<j)i++;
warr[j] = warr[i];
}
warr[i] = key;
if (i - >start)Sort(warr, start, i - );
if (end > i + )Sort(warr, i + , end);
} void Show(Word** warr, int len)
{
for (int i = ; i < len; i++)
{
cout << warr[i]->word << " " << warr[i]->repnum << endl;
}
} int main()
{
ifstream readfile("zpc.txt", ios::in);
if (!readfile){ cout << "程序出现异常,自动退出!" << endl; return ; }
string str, str1;
while (!readfile.eof())
{
getline(readfile, str1);
str += str1;
str += ' ';
}
readfile.close();
Readin(str);
DeSame();
cout << "单词总个数(不考虑重复):" << size << endl;
cout << "除去重复后的单词个数(即重复的单词按1个计):" << size - delsum << endl;
Word** wdarr = new Word*[size - delsum];
Inputp(wdarr);
Sort(wdarr, , size - delsum - );
Show(wdarr, size - delsum);
delete[]wdarr;
Destory();
return ;
}
统计单词个数及词频(C++实现)的更多相关文章
- 第六章 第一个Linux驱动程序:统计单词个数
现在进入了实战阶段,使用统计单词个数的实例让我们了解开发和测试Linux驱动程序的完整过程.第一个Linux驱动程序是统计单词个数. 这个Linux驱动程序没有访问硬件,而是利用设备文件作为介质与应用 ...
- 第六章第一个linux个程序:统计单词个数
第六章第一个linux个程序:统计单词个数 从本章就开始激动人心的时刻——实战,去慢慢揭开linux神秘的面纱.本章的实例是统计一片文章或者一段文字中的单词个数. 第 1 步:建立 Linu x 驱 ...
- NOIP200107统计单词个数
NOIP200107统计单词个数 难度级别: A: 编程语言:不限:运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 给出一个长度不超过200的由 ...
- NOIP2001 统计单词个数
题三 统计单词个数(30分) 问题描述 给出一个长度不超过200的由小写英文字母组成的字母串(约定;该字串以每行20个字母的方式输入,且保证每行一定为20个).要求将此字母串分成k份(1<k&l ...
- Codevs_1040_[NOIP2001]_统计单词个数_(划分型动态规划)
描述 http://codevs.cn/problem/1040/ 与Codevs_1017_乘积最大很像,都是划分型dp. 给出一个字符串和几个单词,要求将字符串划分成k段,在每一段中求共有多少单词 ...
- luogu P1026 统计单词个数
题目链接 luogu P1026 统计单词个数 题解 贪心的预处理母本串从i到j的最大单词数 然后dp[i][j] 表示从前i个切了k次最优解 转移显然 代码 #include<cstdio&g ...
- Codevs 1040 统计单词个数
1040 统计单词个数 2001年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 给出一个长度不超过200的 ...
- codevs1040统计单词个数(区间+划分型dp)
1040 统计单词个数 2001年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 给出一个长度不超 ...
- P1026 统计单词个数——substr
P1026 统计单词个数 string 基本操作: substr(x,y) x是起始位置,y是长度: 返回的是这一段字符串: 先预处理sum[i][j],表示以i开头,最多的单词数: 从后往前寻找,保 ...
随机推荐
- Ceph与OpenStack的Glance相结合
http://docs.ceph.com/docs/master/rbd/rbd-openstack/?highlight=nova#kilo 在Ceoh的admin-node上进行如下操作: 1. ...
- 将/home目录从单独的分区迁移回/目录下
安装系统的时候, 将/, swap, /home这三个目录放在了三个不同的分区, 现在希望将/home目录移回/目录下. 1. umount /home, 然后在/目录下创建/home_new, 通过 ...
- 【iCore3 双核心板】DEMO 1.0 测试程序发布
iCore3 Demo V1.0 程序说明 一.概要 本资料包包含5个文件夹: 1.“arm”里是 icore3上 arm的程序包,开发环境为 KEIL 5.17: 2.“fpga”里是 icore3 ...
- mongodb 安装后 出现警告:** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
警告问题:当前mongodb 支持的最大文件数有256个,但是推荐至少1024个. 解决办法: 1.关闭现在打开的mongodb 终端窗口 2.重新打开终端并运行一下命令: sudo launchct ...
- 如何使用 vimdiff 来 git diff /svn diff
#git 如何实现vimdiffgit config --global diff.tool vimdiff git config --global difftool.prompt false git ...
- 多线程 - CyclicBarrier
一个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point).在涉及一组固定大小的线程的程序中,这些线程必须不时地互相等待,此时 CyclicBarri ...
- 改变UIButton 图片和文字的位置
//设置字体和图片之间的间距 _btnLeft.titleEdgeInsets = UIEdgeInsetsMake(0, -_btnLeft.imageView.frame.size.width, ...
- angularJs指令执行的机制==大概的三个阶段
第一阶段:加载阶段 angularJs要运行的话,需要去等待angular.js加载完成,加载完之后呢,angular就会去查找到ng-app这个指令,ng-app在每个应用里面只能出现一次, 它也就 ...
- WEB语言转义总结
后台字符串嵌入前台语言输出 web语言分为后台和前台,如果后台语言嵌入方式将字符串输出到前台语言中,需要按照前台语言的要求进行转义. 因为前台各个语言都有自身的保留字符, 用于规定自身的语法格式, ...
- EF CodeFirst 如何通过配置自动创建数据库<当模型改变时>
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精 本篇为进阶篇,也是弥补自己之前没搞明白的地方,惭愧 ...