代码
#include <cstring>
#include <cstdlib>
/*
令appear所有元素置为-1;
1. diff = i-appear[str[i]];
2. arrCnt[i] = arrCnt[i-1]+1; (arrCnt[i-1] < diff)
= diff (arrCnt[i-1] >= diff)
3. appear[str[i]]=i;
4. goto 1;
*/
int max_unique_substr(char * str)
{
int len = (int)strlen(str);
int appear[256]={0};
memset(appear,-1,sizeof(appear));
int preCnt=1;
appear[str[0]]=0;
int ret = 1;
for(int i=1;i<len;++i)
{
int dif = i-appear[str[i]];
if(dif>preCnt)
{
++preCnt;
if(preCnt>ret)
{
ret = preCnt;
}
}
else
{
preCnt = dif;
}
appear[str[i]] = i;
}
return ret;
}