算法:如何查询一个字符串中出现次数最多的字符?

firsthym 2009-07-13 08:34:53
算法:编写一个函数,如何查询一个字符串中出现次数最多的字符,返回该字符出现的第一个位置,如果有多个字符出现次数相同,则返回第一个字符出现的第一个位置?
我的算法很笨,关键在于有多个字符次数相同,有没有好点的算法?
...全文
707 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
ysysbaobei 2009-07-15
  • 打赏
  • 举报
回复
顶下
风老二 2009-07-14
  • 打赏
  • 举报
回复
贯彻二楼思想

#include <stdio.h>
#include <iostream>

using namespace std;


class strapp{
public:
static int index;
static char strs;
char nx;
char *p;
public:
strapp():nx(0),p(NULL){}
void set(char *tp){
if(++nx>index){index=nx;strs=*tp;}
if(!p)p=tp;
}
};
int strapp::index = 0;
char strapp::strs = 0;

char *find(char *str){
strapp s[256];
while(*str)s[*str].set(str++);
//cout<<"出现次数最多的字符为:"<<strapp::strs<<endl;
//cout<<"共出现:"<<strapp::index<<"次"<<endl;
//cout<<"首次出现的位置为:"<<(void*)s[strapp::strs].p<<endl;
return s[strapp::strs].p;
}




int main()
{
char * str = "aabbccddddeeeeeeeeeeeefggggaaa";
cout<<find(str);
}

huizhouxueyuan 2009-07-13
  • 打赏
  • 举报
回复
#include<iostream>
#include<string>
#include<map>

using namespace std;



int main()
{
string str = "abdccd";

map<char, int> ch_count;

// 记录每个字符出现的次数
for ( map<char, int>::size_type i = 0; i < str.size(); ++i)
{
ch_count[str[i]]++;
}

// 出现最多的字符
char maxChar = ch_count.begin()->first;

for(map<char, int>::iterator pos = ch_count.begin(); pos != ch_count.end(); ++pos)
{
if (pos->second > ch_count[maxChar])
{
maxChar = pos ->first;
}

// 这个条件是 同样 是目前出现最多的字符, 看谁的位置靠前
if (pos->second == ch_count[maxChar] && str.find(pos->first) < str.find(maxChar))
{
maxChar = pos ->first;
}
}
cout << "字符 " <<maxChar << " 出现 " << ch_count[maxChar] << " 次 ,第一次出现的下标是 "<< str.find(maxChar) << endl;
return 0;
}

huizhouxueyuan 2009-07-13
  • 打赏
  • 举报
回复
不是楼上~ 是说2楼
huizhouxueyuan 2009-07-13
  • 打赏
  • 举报
回复
楼上的~ 没理解好题意吧?
风老二 2009-07-13
  • 打赏
  • 举报
回复

char find(char *str){
int tmp[0xff] = {0};
char temp[2] = {0};

while(*str){
tmp[*str]++;
(tmp[*str] > temp[0])?(temp[0]=tmp[*str],temp[1]=*str):0;
str++;
}
return temp[1];

}

int main(){
char * str = "aabbccddddeeeeeeeeeeeefggggaaa";
cout<<find(str);



}

学习二楼的
Papyna 2009-07-13
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 blh 的回复:]
引用 5 楼 Papyna 的回复:
26个字母,大小写一共52个,用统计的方法计算吧。

LZ要求统计字符不是仅仅字母
[/Quote]

简单,把所有的不同字符集转换到UCS2或者UCS4格式然后做hash_map统计就是了
blh 2009-07-13
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 Papyna 的回复:]
26个字母,大小写一共52个,用统计的方法计算吧。
[/Quote]
LZ要求统计字符不是仅仅字母
Papyna 2009-07-13
  • 打赏
  • 举报
回复
26个字母,大小写一共52个,用统计的方法计算吧。
风老二 2009-07-13
  • 打赏
  • 举报
回复
看错
风老二 2009-07-13
  • 打赏
  • 举报
回复
学习2楼,请教下为什么要多一个for,mc不是已经记录了第一个最长字符吗
blh 2009-07-13
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int a[256];
char mc;

int find(char *s)
{
int n;
while (*s)
{
a[*s]++;
if (a[*s] > mc)
{
mc = a[*s];
}
s++;
}

for (n = 0; n < 256; n++)
{
if (mc == a[n])
{
printf("%u (%c)\n", n, n);
}
}

return 0;
}

int main()
{
char *s = "qewqeqwelsajlasfldsaf;dskfsdkfdksf;ksd;fsdmm fsfsjaiw iiqwjiqequweqweqywiei2312312o3h1oh3ibsgayd";

find(s);
return 0;
}

doudouHuY 2009-07-13
  • 打赏
  • 举报
回复
给每个出现的字符维护一个出现次数 第一次出现位置的信息

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧