HDU 1711 Number Sequence(模板题)
#include <cstdio> const int MAXN = 1000010; const int MAXL = 10010; int N, M; int textS[MAXN]; int tarS[MAXL]; int next[MAXL]; void GetNextVal( int* s, int* nextval, int len ) { int i = 0, j = -1; nextval[0] = -1; while ( i < len ) { if ( j == -1 || s[i] == s[j] ) { ++i, ++j; nextval[i] = j; } else j = nextval[j]; } return; } int KMP( int *t, int lent, int *s, int lens ) { GetNextVal( t, next, lent ); int i = 0, j = 0; while ( j < lens ) { if ( i == -1 || s[j] == t[i] ) { ++i, ++j; if ( i == lent ) return j; } else i = next[i]; } return -1; } int main() { int T; scanf( "%d", &T ); while ( T-- ) { scanf( "%d%d", &N, &M ); for ( int i = 0; i < N; ++i ) scanf( "%d", &textS[i] ); for ( int i = 0; i < M; ++i ) scanf( "%d", &tarS[i] ); int ans = KMP( tarS, M, textS, N ); if ( ans < 0 ) printf( "%d ", ans ); else printf("%d ", ans - M + 1 ); } return 0; }
HDU 1686 Oulipo(模板题)
#include <cstdio> #include <cstring> #include <cstdlib> const int MAXN = 1000010; const int MAXL = 10010; char tarS[MAXL]; char testS[MAXN]; int next[MAXL]; void GetNextVal( char *s, int len, int *p ) { int i = 0, j = -1; p[0] = -1; while ( i < len ) { if ( j == -1 || s[i] == s[j] ) { ++i, ++j; p[i] = j; } else j = p[j]; } return; } int KMP( char *t, char *s ) { int cnt = 0; int lent = strlen(t); int lens = strlen(s); GetNextVal( t, lent, next ); int i = 0, j = 0; while ( j < lens ) { if ( i == -1 || s[j] == t[i] ) { ++i, ++j; if ( i == lent ) { ++cnt; i = next[i]; } } else i = next[i]; } return cnt; } int main() { int T; scanf( "%d", &T ); while ( T-- ) { scanf( "%s%s", tarS, testS ); printf("%d ", KMP( tarS, testS ) ); } return 0; }
HDU 2203 亲和串
#include <cstdio> #include <cstring> #include <cstdlib> const int MAXN = 100010; char testS[ MAXN << 1 ]; char tarS[MAXN]; char temp[MAXN]; int next[MAXN]; void GetNextVal( char *s, int len, int *p ) { int i = 0, j = -1; p[0] = -1; while ( i < len ) { if ( j == -1 || s[i] == s[j] ) { ++i, ++j; p[i] = j; } else j = p[j]; } return; } bool KMP( char *s, int lenS, char *t, int lenT ) { GetNextVal( t, lenT, next ); int i = 0, j = 0; while ( j < lenS ) { if ( i == -1 || s[j] == t[i] ) { ++i, ++j; if ( i == lenT ) return true; } else i = next[i]; } return false; } int main() { while ( ~scanf( "%s", temp ) ) { scanf("%s", tarS ); int lenS = strlen(temp); int lenT = strlen(tarS); if ( lenT > lenS ) { puts("no"); continue; } strcpy( testS, temp ); strcpy( &testS[lenS], temp ); KMP( testS, lenS + lenS, tarS, lenT ) ? puts("yes") : puts("no"); } return 0; }
POJ 3450 Corporate Identity KMP+二分
二分串长,求出每个串长的所有子串,只要找到一个符合的子串即可。
#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> using namespace std; const int MAXN = 4010; const int MAXL = 210; int N; char str[MAXN][MAXL]; char temp[MAXL]; char anser[MAXL]; int nextval[MAXL]; int len[MAXN]; void GetNextVal( char *s, int lenth ) { int i = 0, j = -1; nextval[0] = -1; while ( i < lenth ) { if ( j == -1 || s[i] == s[j] ) { ++i, ++j; if ( s[i] != s[j] ) nextval[i] = j; else nextval[i] = nextval[j]; } else j = nextval[j]; } return; } bool KMP( char *t, char *s, int lenth, int lenn ) { GetNextVal( t, lenth ); int i = 0, j = 0; while ( j < lenn ) { if ( i == -1 || s[j] == t[i] ) { ++i, ++j; if ( i == lenth ) return true; } else i = nextval[i]; //if ( i == lenth ) return true; } return false; } bool check( int tpL ) { bool flag = false; bool ok = false; for ( int st = 0; st + tpL - 1 < len[0]; ++st ) { for ( int k = 0; k < tpL; ++k ) temp[k] = str[0][ st + k ]; temp[ tpL ] = '