最长不重复子串
#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;
}
一切源于对计算机的热爱

浙公网安备 33010602011771号