连续子串

由 26 个小写字母组成的 str,在 str中查找最长连续字符串不相同的子串。


如abcae, 连续子串是bcae


table记录的是字符最近出现的位置;


int maxDiff(char * str){
    int table[26] = {-1};
    memset(table,-1,sizeof(table));
    int loop = 0;
    int max_len = 0;
    int tmp_len = 0;
    int len = strlen(str);
    
    table[str[0] - 'a'] = 0;
    max_len = 1;
    tmp_len = 1;

    for (int i = 1; i < len ;++i) {
       int last = table[str[i] - 'a'];
       if (last == -1) {
           tmp_len++;
           
       }
       else if (i - tmp_len > last) {
           tmp_len++;
          
       }
       else {
           tmp_len = i - last;
          
       }
       table[str[i] - 'a'] = i;

       if (max_len < tmp_len)
           max_len = tmp_len;
    }

    return max_len;
}



int main() 
{//3*2
  
   char *a = "abcae";
   cout<<maxDiff(a)<<endl;
   return 0;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值