您的位置:首页 > 其它

求一个字符串中最长的相同字符字串 不区分大小写

2016-03-30 11:54 288 查看
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;

string getLongest(string s)
{
int n = s.length();
if(n<=1)
return s;
int start = 0;
int count = 1;
int max = 1;
char flag = tolower(s[0]);
for(int i=1;i<n;i++)
{
if(tolower(s[i])==flag)
{
++count;
flag = tolower(s[i]);
}else{
if(max<count)
{
max = count;
start = i-max;
}
count = 1;
flag = tolower(s[i]);
}
}
// cout<<max<<endl;
// cout<<count<<endl;
// cout<<start<<endl;
if(max<count)
{
max = count;
start = n-max;
}
return s.substr(start,max);
}

int main()
{
string s;
cin>>s;
cout<<getLongest(s)<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: