您的位置:首页 > 其它

华为机试 频率最高 频率最低的 单词

2013-05-05 14:02 246 查看
#include "stdafx.h"
#include <iostream>
#include<vector>
using namespace std;

typedef struct Node{
int length;   //单词的长度
int count;    //单词出现的次数
char word[32];   //内存对齐,30与32占的空间相同
};

void insert( vector<Node> &vNode, Node & node )
{
for ( int i = 0; i < vNode.size(); ++i )
{
if ( vNode[i].length == node.length )    //只有长度相同才进行比较
{
if ( !strcmp( vNode[i].word, node.word ) )
{
vNode[i].count++;
return;
}
}
}

node.count = 1;
vNode.push_back( node );
}

void count( char *strSrc, vector<Node> &vNode )
{
char tmpStr[32];
int i = 0;
char *pIndex = --strSrc;
while ( *pIndex++ )
{
if ( *pIndex == ' ' || *pIndex == ',' || *pIndex == '\0'  )  //找到单词的结尾
{
tmpStr[i] = '\0';
i = 0;
int tmpLen = strlen( tmpStr );

if ( tmpLen < 1 )
continue;

Node tmpNode;
tmpNode.length = tmpLen;
strcpy( tmpNode.word, tmpStr );
insert( vNode, tmpNode );

if ( *pIndex == '\0' ) return;

}
else
{
if( *pIndex>= 'a' && *pIndex <= 'z' )
tmpStr[i++] = *pIndex;
else if( *pIndex>= 'A' && *pIndex <= 'Z' )
tmpStr[i++] = *pIndex + 32;
}
}
}

int main()
{
char input[] = "hello, world world i Hello heLLo";
vector<Node> vNode;

count( input, vNode);

for( int i = 0; i <vNode.size(); ++i )
cout<<vNode[i].count<<"   "<<vNode[i].word<<endl;

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息