C++ 如何在字符串数组中查找字符(重复)的最长序列?

C++ 如何在字符串数组中查找字符(重复)的最长序列?,c++,string,C++,String,我有一个字符串像:aabccdddeeacdd 我希望我的程序返回相同字符的最长子字符串的长度,就像这个字符串应该返回的一样(5=d的子字符串的长度) 其他长度为: a=2,1 b=1 c=2,1 d=5,2 e=3 所以最长的长度如果是d 我的代码: #include<iostream> #include<string> #include<vector> #include<algorithm> using namespace std; i

我有一个字符串像:aabccdddeeacdd

我希望我的程序返回相同字符的最长子字符串的长度,就像这个字符串应该返回的一样(5=d的子字符串的长度)

其他长度为:

  • a=2,1
  • b=1
  • c=2,1
  • d=5,2
  • e=3
所以最长的长度如果是d

我的代码:

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

int main(){
    char flag;
    vector<int> v;
    int init = 0,count; // init for checking first character in loop
    string s = "aabbbcccdddddeeeaacc";
    count = 0;
    for(int i=0;i<s.length();i++){
        if(init == 0){
            flag = s[i];
            count++;
            init = 1;
        }
        else{
            if(s[i] == flag){
                count++;
            }

            else{
                flag = s[i];
                v.push_back(count);
                count = 0;
            }
        }
    }

    cout<<*max_element(v.begin(),v.end());

    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
字符标志;
向量v;
int init=0,count;//检查循环中第一个字符的init
字符串s=“aabbbcccdddeeaacc”;
计数=0;

对于(int i=0;i这个解决方案似乎有效

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

int main(){
    char flag;
    vector<int> v;
    int init = 0,count; // init for checking first character in loop
    string s = "aaaaaaabbbcccddddddeeeaacc";
    count = 0;
    for(int i=0;i<s.length();i++){
        if(init == 0){
            flag = s[i];
            count++;
            init = 1;
        }
        else{
            if(s[i] == flag){
                count++;
            }

            else{
                flag = s[i];
                v.push_back(count);
                count = 1;
            }
        }
    }

    cout<<*max_element(v.begin(),v.end());

    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
字符标志;
向量v;
int init=0,count;//检查循环中第一个字符的init
字符串s=“aaaaaaaabbbcccddddeeeaacc”;
计数=0;
对于(int i=0;i
#包括
#包括
使用名称空间std;
int main(){
int=0;
字符串s=“aabbbcccdddeeaacc”;
对于(int i=0;i
#包括
#包括
int main(){
std::字符串str;
getline(标准::cin,str);
int i=0;
size_t str_size=str.size();
std::pair max={'',0};
而(i最大秒)
max={str[i],j-i};
i=j;
}

std::你有什么想法吗?让我们知道。到目前为止你尝试了什么?你的尝试是如何起作用的,还是不起作用的?你的尝试有什么问题?你有,并且知道如何创建一个?我想做一个for循环,然后当任何字符出现时,给标志一个唯一的值并增加一个计数器,只要其他字符不发生,然后将计数存储在数组中并找到最大值。我尝试了这段代码,但它似乎不起作用,我仍在尝试,如果它起作用,我将很快更新代码。从一个大小为26的整数数组开始。您不需要将计数存储在数组中。只需存储在运行中遇到的最大值。@Gaurav Sehgal感谢建议很多:)它似乎很有效总是测试代码的边界情况,例如,最后一组不被计算。是的,对@Adrian Maire我还没有想过,你能帮我做些什么修改吗?@BugAdder只要做
if(count>max){max=count;}在每次迭代中,不只是在代码< >代码> >代码中。它修复了最后一个组问题。这似乎有效,但你确信它是有效的吗?你使用2来循环,虽然看起来很小,很容易,但是我如何在C++中使用正则表达式?对不起,我在C++中没有使用正则表达式。这不是2个循环。它是2个语句,但它们是JUS。t字符串上一次迭代的两部分,因此它基本上只是一个循环。你在内部循环中测试字符串长度以外的字符吗?@K.Kirsz嘿,很好,你也解决了最后几个字符的问题,你能帮我重构代码吗?请解释它是如何工作的,为什么这样做很好。这可能会帮助人们寻找答案.
#include<iostream>
#include<string>

using namespace std;

int main(){
    char flag;
    int max;
    int init = 0,count; // init for checking first character in loop
    string s = "aaaaaaabbbcccddddddeeeeeeeeeeaacc";
    count = 0;
    max = 0;
    for(int i=0;i<s.length();i++){
        if(init == 0){
            flag = s[i];
            count++;
            init = 1;
        }
        else{
            if(s[i] == flag){
                count++;
            }

            else{
                flag = s[i];
                if(count > max){
                    max = count;
                }
                count = 1;
            }
        }
    }

    cout<<max;

    return 0;
}
#include<iostream>
#include<string>

using namespace std;

int main(){
    int longest = 0;
    string s = "aabbbcccdddddeeeaacc";
    for(int i=0; i<s.length();){
        char current = s[i];
        int currLen = 0;
        for(;i<s.length() && current == s[i]; ++i)
            ++currLen;
        if(currLen > longest)
            longest = currLen ;
    }

    cout<<longest;

    return 0;
}
#include <iostream>
#include <string>

int main() {

    std::string str;
    getline(std::cin, str);

    int i = 0;
    size_t str_size = str.size();
    std::pair<char, int> max = {' ', 0};

    while (i < str_size) {
        int j = i;
        while (str[++j] == str[i])
            ;
        if (j - i > max.second)
            max = {str[i], j - i};
        i = j;
    }

    std::cout << max.first << std::endl << max.second;
    return 0;
}