您的位置:首页 > 其它

统计字符串中出现最多的单词和次多的单词

2013-09-09 19:05 931 查看
题目:统计字符串中出现最多的单词和次多的单词。

测试用例:"This This This a dog,not a cat!"

输出:This 3次 a 2次

思路:先把这个字符串分词,放入一个数组中。然后记录每个词出现的次数。先找出最多的那个词,然后将其次数置0,在找最多的那个词(原来次多的词)。

#include<iostream>
using namespace std;
#define MAX_COUNT_WORD 256

bool 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐