【发布时间】:2020-08-26 07:12:50
【问题描述】:
我编写了一个简单的程序来查找 C++ 中给定字符串中具有不同字符的最长子字符串。我的代码适用于某些输入,但不适用于其他输入。此外,它为相同的输入提供了不同的输出。我哪里错了?
int main() {
int t;
cin >>t;
while(t--){
string s;
cin >> s;
int n = s.length();
int maxlen = 0;
for(int i = 0; i < n; i++){
int count = 0;
int arr[26] = {0};
bool isDist = true;
int j = i;
while(isDist){
if(arr[(int)s[j] - (int)'a'] == 0){
count++;
arr[(int)s[j] - (int)'a'] = 1;
j++;
} else {
isDist = false;
}
}
if(count > maxlen) maxlen = count;
}
cout << maxlen << endl;
}
return 0;
}
对于以下输入:
3
aewergrththy
aewergrththy
aewergrththy
我的代码输出:
5
4
4
感谢您的帮助,谢谢!
【问题讨论】: