原題鏈接:http://ac.jobdu.com/problem.php?pid=1530
字符串簡單題,看似O(n^2)的復雜度10000的數據量會tle,其實最長不重復子串不超過26個嘛。。。
如下:
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 using std::max; 7 const int Max_N = 10010; 8 int vis[26]; 9 char buf[Max_N]; 10 void solve(){ 11 int i, j, ans = 0, n = strlen(buf); 12 for (i = 0; i < n - 1; i++){ 13 int t = 1; 14 memset(vis, 0, sizeof(vis)); 15 vis[buf[i] - 'a'] = 1; 16 for (j = i + 1; j < n; j++){ 17 if (!vis[buf[j] - 'a']){ 18 vis[buf[j] - 'a'] = 1; 19 t++; 20 } else { 21 break; 22 } 23 } 24 ans = max(ans, t); 25 } 26 printf("%d\n", ans); 27 } 28 int main(){ 29 #ifdef LOCAL 30 freopen("in.txt", "r", stdin); 31 freopen("out.txt", "w+", stdout); 32 #endif 33 while (~scanf("%s", buf)) solve(); 34 return 0; 35 } View Code