九度oj 1530 最长不重复子串

原题链接: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

 

posted @ 2015-05-04 21:39  GadyPu  阅读(265)  评论(1编辑  收藏  举报