九度OJ 1530 最长不重复子串

题目地址:http://ac.jobdu.com/problem.php?pid=1530

题目描述:

最长不重复子串就是从一个字符串中找到一个连续子串,该子串中任何两个字符都不能相同,且该子串的长度是最大的。

输入:

输入包含多个测试用例,每组测试用例输入一行由小写英文字符a,b,c...x,y,z组成的字符串,字符串的长度不大于10000。

输出:

对于每组测试用例,输出最大长度的不重复子串长度。

样例输入:
absd
abba
abdffd
样例输出:
4
2
4
来源:
阿尔卡特2013年实习生招聘笔试题
#include <stdio.h>
 
int MaxLength(char str[]){
    int index = 0;
    int i;
    int tmp = 0;
    int max = 0;
    int hash[26];
    int alphabet;
 
    for (i=0; i<26; ++i)
        hash[i] = 0;
    while (str[index]){
        alphabet = str[index] - 'a';
        if (hash[alphabet] == 0){
            hash[alphabet] = 1;
            if (++tmp > max)
                max = tmp;
        }
        else{
            for (i=0; i<26; ++i)
                hash[i] = 0;
            hash[str[index] - 'a'] = 1;
            i = index - 1;
            while (i >= 0 && str[i] != str[index]){
                alphabet = str[i] - 'a';
                hash[alphabet] = 1;
                --i;
            }
            tmp = index - i;
        }
        ++index;
    }
    return max;
}
 
int main(void){
    char str[10010];
 
    while (scanf ("%s", str) != EOF){
        printf ("%d
", MaxLength(str));
    }
 
    return 0;
}


原文地址:https://www.cnblogs.com/liushaobo/p/4373777.html