统计字符串中出现最多的单词和次多的单词
来源:互联网 发布:最骚的淘宝买家秀店铺 编辑:程序博客网 时间:2024/06/09 05:39
题目:统计字符串中出现最多的单词和次多的单词。
测试用例:"This This This a dog,not a cat!"
输出:This 3次 a 2次
思路:先把这个字符串分词,放入一个数组中。然后记录每个词出现的次数。先找出最多的那个词,然后将其次数置0,在找最多的那个词(原来次多的词)。
#include<iostream>using namespace std;#define MAX_COUNT_WORD 256bool isCounted[MAX_COUNT_WORD];//标记该单词已经统计过,即是否在前面出现过int times[MAX_COUNT_WORD]; //统计单词次数//判断是否为符合单词的字符bool isRightChar(char data){return data>='a'&&data<='z'||data>='A'&&data<='Z';}//判断是否为相同的字符串bool isSameStr(const char* first,const char* second){if(strlen(first)!=strlen(second))return false;while(*first++!=*second++)return false;return true;}void main(){char* data="This This This a dog,not a cat!";int length=strlen(data);int i;//统计单词个数和最大长度int count=0;int lastindex=-1;int currentindex=-1;int maxLength=0;for(i=0;i<length;++i){currentindex=i;if(!isRightChar(data[i])){if(currentindex-lastindex>1){++count;if(currentindex-lastindex-1>maxLength)maxLength=currentindex-lastindex-1;}lastindex=currentindex;}}//分词char** word=new char*[count];for(i=0;i<count;++i){word[i]=new char[maxLength+1];memset(word[i],0,maxLength+1);}int current_count=0;lastindex=-1;currentindex=-1;for(i=0;i<length;i++){currentindex=i;if(!isRightChar(data[i])){if(currentindex-lastindex>1){memcpy(word[current_count++],data+lastindex+1,currentindex-lastindex-1);}lastindex=currentindex;}}//比较字符串,统计次数int j;int temp_count=1;for(i=0;i<count;++i){if(isCounted[i])continue;for(j=i+1;j<count;++j){if(isSameStr(word[i],word[j])){isCounted[j]=true;temp_count++;}}times[i]=temp_count;temp_count=1;isCounted[i]=true;}//统计最多的次数 单词int max_times=0;int indexOfWord=0;for(i=0;i<count;++i){if(times[i]&×[i]>max_times){max_times=times[i];indexOfWord=i;}}times[indexOfWord]=0;cout<<"最多的单词次数为: "<<max_times<<" 它是: "<<word[indexOfWord]<<endl;//统计次多的次数 单词max_times=0;indexOfWord=0;for(i=0;i<count;++i){if(times[i]&×[i]>max_times){max_times=times[i];indexOfWord=i;}}cout<<"次多的单词次数为: "<<max_times<<" 它是: "<<word[indexOfWord]<<endl;}
- 统计字符串中出现最多的单词和次多的单词
- 统计一串字符串中出现次数最多和次多的单词(华为上机考试题)
- 统计一串字符串中出现次数最多和次多的单词
- 统计一串字符串中出现次数最多和次多的单词(华为上机考试题)
- 华为:统计一串字符串中出现次数最多和次多的单词(华为上机考试题)
- 【python】统计文本中出现最多次的单词
- 统计连续出现次数最多的单词
- 统计单词出现最多的个数
- JAVA-统计英文句子中出现次数最多的单词和出现的次数
- 统计单词中出现的单词数
- 一串字符串中出现次数最多的单词
- 统计字符串中每个单词出现的次数 for C++
- 统计单词出现的最多次数(Trie树)
- 统计一个文档中出现频率最多的k个单词的实现
- 针对一个文件 统计其中的单词和字符数 找出文件中出现次数最多的单词数 将文件中出现的单词按频率进行排序并输出
- 统计一篇英文文章中出现次数最多的前五个单词
- sort +awk+uniq 统计文件中出现次数最多的前10个单词
- 统计文件中出现次数最多的前10个单词
- 【帮助你完成腾达路由器的基本配置】
- ASP.Net MVC中 ViewBag, ViewData, TempData, ViewModel 的区别
- 如何高效使用Vim
- 贪心算法概述
- 好资源
- 统计字符串中出现最多的单词和次多的单词
- vb.net form New 带参数构造函数
- android ListView详解
- spring事物配置
- 计算机的存储区域C++和JAVA
- 找零钱问题(C语言实现)——贪心算法应用(1)
- connectify 用电脑做wifi 热点
- 速成笔记:Java DataBase Connectivity
- 一行代码添加的页面效果