现在的位置: 首页 > 综合 > 正文

2013百度笔试 求字符串中连续出现相同字符的最大值

2017年12月25日 ⁄ 综合 ⁄ 共 476字 ⁄ 字号 评论关闭

题目完整描述为:用递归的方式实现一个求字符串中连续出现相同字符的最大值,如aaabbcc,连续出现a的最大值为3,abbc,连续出现字符最大的值为2。

#include <iostream>
using namespace std;
int maxnum(string &str,int first)
{
	if (str.empty())
	{
		return 0;
	}
	char begin=str.at(first);
	int count=0;
	int i=first;
	for (;i<str.length();i++)
	{
		if (begin==str.at(i))
		{
			++count;
		}
		else
		{
			break;
		}
	}
	int temp=0;
	
	if (i<str.length())
	{
		temp=maxnum(str,i);
	}
	if (count<temp)
	{
		count=temp;
	}
	return count;
}
int main()
{
	string str="aaabbccccc";
	int begin=0;
	int count=0;
	count=maxnum(str,begin);
	cout<<count<<endl;
	return 0;
}

抱歉!评论已关闭.