題目1530:最長不重復子串時間限制:1 秒
內存限制:128 兆
特殊判題:否
提交:362
解決:106
題目描述:
最長不重復子串就是從一個字符串中找到一個連續子串,該子串中任何兩個字符都不能相同,且該子串的長度是最大的。
輸入:
輸入包含多個測試用例,每組測試用例輸入一行由小寫英文字符a,b,c...x,y,z組成的字符串,字符串的長度不大於10000。
輸出:
對於每組測試用例,輸出最大長度的不重復子串長度。
樣例輸入:
absd
abba
abdffd樣例輸出:
4
2
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;
const int maxn = 10010;
char str[maxn];
bool vis[30];//hash
inline int ind(char x) {
return x - 'a';
}
void work() {
int maxv = 0;
int i, j, cnt = 0;
int len = strlen(str);
memset(vis, false, sizeof(vis));
for(i = 0; i < len; i++) {
memset(vis, false, sizeof(vis));
cnt = 0;
for(j = i; j < len; j++) { //開始沒有回溯,wa
char t = str[j];
if(!vis[ind(t)]) {
vis[ind(t)] = true;
cnt++;
if(maxv < cnt) maxv = cnt;
}else break;
}
}
printf("%d\n", maxv);
}
int main()
{
memset(str, '\0', sizeof(str));
while(scanf("%s", str) != EOF) {
getchar();
work();
memset(str, '\0', sizeof(str));
}
return 0;
}