输入:abcc 输出:3
输入:baddabce 输出: 5
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main() {
string str;
getline(cin, str);
int count = 0;
int max = 0;
vector<char> vec;
for (int i = 0; i < str.size(); ++i) {
//在vec中搜索当前字符
int j;
for (j = 0; j < vec.size(); ++j) {
if (vec[j] == str[i])
break;
}
if (j == vec.size())//表示当前字符没有出现在vec中
vec.push_back(str[i]);
else {
vec.clear();
vec.push_back(str[i]);
}
count = vec.size();
if (count > max)
max = count;
}
cout << max << endl;
system("pause");
}
本文介绍了一种用于寻找字符串中最长不重复子串的算法实现。通过遍历输入字符串并使用向量跟踪已出现过的字符,该算法能有效地找出最长的不包含重复字符的子串长度。示例代码展示了如何实现这一算法,并给出了具体输入输出的例子。
1506





