私のような文字列を持っている:私は、この文字列のように同じ文字の最長のサブ文字列の長さを返すために私のプログラムは、(Dのサブの5 =長さ)を返す必要がありますしたいC++の文字列配列で文字の最長シーケンスを見つける方法(繰り返し)
をaabccdddddeeeacdd、
他の長さをである:
- = 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;
}
何かを考えてみましょうか? –
あなたはこれまでに何を試しましたか?あなたの試みはどうやって働いたのですか?あなたの試みにはどんな問題がありますか?あなたは* [良い質問をする方法を読む](http://stackoverflow.com/help/how-to-ask)と、[最小限の、完全で証明可能な例(http: /stackoverflow.com/help/mcve)? –
私はforループを実行した後、任意の文字が発生したら、フラグに一意の値を与え、他の文字が出現しない限りカウンタをインクリメントして配列に格納し、最大値を見つけます。 – BugAdder