我試圖找到沒有重複字符的最長的子串。 我有一個布爾向量來跟蹤256個ASCII字符。C++中最長的非重複子串
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string s = "aaaaaaaaaaaaasadfrhtytbgrbrsvsrhvsg";
vector<bool> v(256, false);
int j = 0, len = 0, index = 0;
for(int i = 0; i < s.length(); i++)
{
if(!v[s[i]])
{
j++;
if(j > len)
{
len = j;
index = i - j;
}
v[s[i]] = true;
}
else
{
j = 0;
v.clear();
}
}
cout << s.substr(index, len) + " " << len << endl;
}
我可以理解爲什麼它給人的輸出adfrht 6
,whreas正確的輸出是sadfrhty 8
你是說你不能或不能理解? – Ediac
你有沒有時間複雜度限制?或者無論執行多長時間,您只需要答案。 – Rasool
@算法算法似乎是正確的。 – adrian008