1282. Computer Game

TAG kmp

 

算是标准的kmp的题吧,不过注意 protection_dimension 的大小并没有给出,最好动态分配内存。预先开的数组如果太小会 Restrict Function。

 

我是用大数组过的。

/* source code of submission 443157, Zhongshan University Online Judge System */ #include <stdio.h> const int N=60000; int n1, n2; char s1[N+1], s2[N*50+1]; int fail[N+1]; int result; void pre() { for (int i=1,j=0; i<=n1; ++i, ++j) { fail[i]=j; while ( j>0 && s1[i]!=s1[j] ) { j=fail[j]; } } } int kmp() { for (int i=1,j=1; i<=n2-n1+1; ++i, ++j) { while ( j>0 && s2[i]!=s1[j] ) { j=fail[j]; } if ( j==n1 ) { return i-n1; } } return -1; } int main(int argc, char *argv[]) { while ( scanf("%d", &n1)!=EOF ) { for (int i=1; i<=n1; ++i) { scanf("%d", &s1[i]); } scanf("%d", &n2); for (int i=1; i<=n2; ++i) { scanf("%d", &s2[i]); } pre(); result=kmp(); if ( result == -1 ) { printf("no solution/n"); } else { printf("%d/n", result ); } } return 0; }

你可能感兴趣的:(function,System,n2)