Sunday

#include <iostream>
using namespace std;


int cnt = 0;
int sunday() {
char src[]="jfkdsahdiojdaigfgthlipjgffg";
char des[]="gffg";
int next[26];
int i, j, pos, sum;
int len_s = strlen( src );
int len_d = strlen( des );

for( i=0; i<26; ++i ) next[i] = len_d + 1;
for( i=0; i<len_d; ++i ) next[des[i]-'a'] = len_d - i;

pos = sum = 0;
while( pos < (len_s-len_d+1) ) {
for( i=0; i<len_d; ++i ) {
++ cnt;
if( src[pos+i] != des[i] ) {
pos += next[ src[pos+len_d] - 'a'];
break;
}
}
if( i == len_d ) return pos;
}
return -1;
}


int main() {
//int cnt = 0;
printf( "%d\n", sunday() );
printf( "cnt = %d\n", cnt );
return 0;
}

你可能感兴趣的:(include)