最长不重复子串

#include <algorithm>
#include <iostream>
using namespace std;

int search(char *text){
    int lastpos[256], lmax=0, curmax=0;
    for(int i=0;i<256;i++)
        lastpos[i]=-1;
    for(int i=0; text[i]; i++){
        if(i-lastpos[ text[i] ] > curmax){
            curmax++;
            lmax = max(lmax, curmax);
        }   
        else
            curmax = i-lastpos[ text[i] ];
        lastpos[ text[i] ] = i;
    }   
    return lmax;
}
int main(){
    char raw[] = "xabcdabxb";
    cout<<search(raw)<<endl;
    return 0;
}

 

posted @ 2013-09-21 20:44  l851654152  阅读(200)  评论(0编辑  收藏  举报