/*
* 题目:最长不含重复字符的子字符串
* 解法:动态规划
* 定义函数f(i)表示以第i个字符结尾的包含当前字符的不含重复字符的子字符串的最长长度
* 如果当前的第i个字符没有出现过,那么f(i)=f(i-1)+1,如果已经出现过,设当前元素与上次出现的距离为d,如果d小于等于
* f(i-1),说明上次出现的字符出现在f(i-1)对应的最长字符串之中,f(i)=d;如果d大于f(i-1),则f(i)=f(i-1)+1
* */
* 题目:最长不含重复字符的子字符串
* 解法:动态规划
* 定义函数f(i)表示以第i个字符结尾的包含当前字符的不含重复字符的子字符串的最长长度
* 如果当前的第i个字符没有出现过,那么f(i)=f(i-1)+1,如果已经出现过,设当前元素与上次出现的距离为d,如果d小于等于
* f(i-1),说明上次出现的字符出现在f(i-1)对应的最长字符串之中,f(i)=d;如果d大于f(i-1),则f(i)=f(i-1)+1
* */
#include <stdio.h>
#include<string.h>
int longestSubstringWithoutDuplication(char* str,int len){
int curLength=1;
int maxLength=0;
int d,i;
// 创建一个长度为26的数组保存每个字符上次出现时在字符串中的下标
int position[26];
for( i=0;i<26;i++){
position[i]=-1;
}
//保存第0个字符出现时在字符串中的下标
position[str[0]-'a']=0;
for ( i=1;i<len;i++){
if (position[str[i]-'a']!=-1){
//说明此字符已经出现过
d=i-position[str[i]-'a'];
if (d>curLength){
curLength=curLength+1;
}else {
curLength=d;
}
position[str[i]-'a']=i;
}else {
//没有有出现过
curLength=curLength+1;
position[str[i]-'a']=i;
}
if (curLength>maxLength)
maxLength=curLength;
}
return maxLength;
}
int main (void){
char a[20];
gets(a);
printf("%d\n",longestSubstringWithoutDuplication(a,strlen(a)));
return 0;
}