#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++实现)的更多相关文章

  1. 第六章 第一个Linux驱动程序:统计单词个数

    现在进入了实战阶段,使用统计单词个数的实例让我们了解开发和测试Linux驱动程序的完整过程.第一个Linux驱动程序是统计单词个数. 这个Linux驱动程序没有访问硬件,而是利用设备文件作为介质与应用 ...

  2. 第六章第一个linux个程序:统计单词个数

    第六章第一个linux个程序:统计单词个数 从本章就开始激动人心的时刻——实战,去慢慢揭开linux神秘的面纱.本章的实例是统计一片文章或者一段文字中的单词个数.  第 1 步:建立 Linu x 驱 ...

  3. NOIP200107统计单词个数

    NOIP200107统计单词个数 难度级别: A: 编程语言:不限:运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 给出一个长度不超过200的由 ...

  4. NOIP2001 统计单词个数

    题三 统计单词个数(30分) 问题描述 给出一个长度不超过200的由小写英文字母组成的字母串(约定;该字串以每行20个字母的方式输入,且保证每行一定为20个).要求将此字母串分成k份(1<k&l ...

  5. Codevs_1040_[NOIP2001]_统计单词个数_(划分型动态规划)

    描述 http://codevs.cn/problem/1040/ 与Codevs_1017_乘积最大很像,都是划分型dp. 给出一个字符串和几个单词,要求将字符串划分成k段,在每一段中求共有多少单词 ...

  6. luogu P1026 统计单词个数

    题目链接 luogu P1026 统计单词个数 题解 贪心的预处理母本串从i到j的最大单词数 然后dp[i][j] 表示从前i个切了k次最优解 转移显然 代码 #include<cstdio&g ...

  7. Codevs 1040 统计单词个数

    1040 统计单词个数 2001年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 给出一个长度不超过200的 ...

  8. codevs1040统计单词个数(区间+划分型dp)

    1040 统计单词个数 2001年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold   题目描述 Description 给出一个长度不超 ...

  9. P1026 统计单词个数——substr

    P1026 统计单词个数 string 基本操作: substr(x,y) x是起始位置,y是长度: 返回的是这一段字符串: 先预处理sum[i][j],表示以i开头,最多的单词数: 从后往前寻找,保 ...

随机推荐

  1. usaco 2016 Feb 负载平衡

    题目大意:平面上一堆点,用两条平行于坐标轴的直线将其分为四部分,使得点数最多的一部分最少 第一维枚举,第二维三分,点集用两棵树状数组维护 #include<bits/stdc++.h> # ...

  2. C# 代码笔记

    一.使循环不卡 Application.DoEvents(); System.Threading.Thread.Sleep(5); 二.计算代码运行时间 Stopwatch sw = new Stop ...

  3. Yii源码阅读笔记(二十六)

    Application 类中设置路径的方法和调用ServiceLocator(服务定位器)加载运行时的组件的方法注释: /** * Handles the specified request. * 处 ...

  4. SeasLog-An effective,fast,stable log extension for PHP

    github: https://github.com/Neeke/SeasLog @author Chitao.Gao [neeke@php.net] @交流群 312910117 简介 为什么使用S ...

  5. 解决ssh登录后闲置时间过长而断开连接

     ++++++++++++++++++++++++++++ #!/usr/bin/env bash password_5="Uxxx7"host_5="129.x.x.1 ...

  6. 【后台测试】手把手教你jmeter压测

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处.  转载请注明出处:http://www.cnblogs.com/by-dream/p/5611555.html 我知道我迟早是要踏上了后台测试之路 ...

  7. Java 创建过滤器 解析xml文件

    今天写了一个过滤器demo,现在是解析actions.xml文件,得到action中的业务规则:不需要导入任何jar包 ActionFilter过滤器类: package accp.com.xh.ut ...

  8. LeetCode Power of Three

    原题链接在这里:https://leetcode.com/problems/power-of-three/ 与Power of Two类似.检查能否被3整除,然后整除,再重复检查结果. Time Co ...

  9. Linux用户配置sudo权限

    Linux用户配置sudo权限 创建sudo权限用户 #useradd supope #passwd supope #visudo #supope         ALL=(ALL)       AL ...

  10. ERROR 1010 (HY000): Error dropping database (can't rmdir '.\qpweb', errno: 41) 删库失败问题的解决

    Win8 下,MySQL5.5,root 用户登录 MySQL 5.5 Command Line Client,删除 qpweb 数据,执行命令 drop database qpweb;报错信息:ER ...