KMP

来源:互联网 发布:windows bea 000402 编辑:程序博客网 时间:2024/06/05 18:27
#include <queue>#include <algorithm>#include <iostream>#include <memory.h>using namespace std;int next[1000];void getnext(char *b,int lenb){  int k=-1,j=0;  next[0]=-1;  while(j<lenb)  {    if(b[k]==b[j]||k==-1)    {      k++;      j++;      next[j]=k;    }    else      k=next[k];  }}int kmp(char *a,char *b){  int i,j,lena=strlen(a),lenb=strlen(b);  getnext(b,lenb);  while(i<lena)  {    if(a[i]==b[j])    {      i++;      j++;    }    else    {      j=next[j];      if(j==-1)      {        j=0;        i++;      }    }    if(j==lenb)      return i-lenb;  }  return -1;}int main(int argc, char const *argv[]){char a[1000];  char b[1000];  cin>>a>>b;  cout<<kmp(a,b)<<endl;  system("pause");return 0;}

还有一个不会超时的版本的soj 1282. Computer Game 的题解。题目如下所示:

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

Brian is an enthusiast of computer games, especially those that simulate virtual reality. Now he is in front of the Star Gate. In order to open the gate he must break the protection as quickly as he can. Breaking the protection means to match a given code (a sequence of numbers) against the gate protection (a very long sequence of numbers). The starting position of the first occurrence of the code within the sequence opens the gate. Can you help him?

The code is a sequence of at most 60000 integer numbers between 0 and 255. The gate protection contains integer numbers between 0 and 255. Your program must find the first match if there is one, or report the absence of a match.

Input

The text input file contains several data sets. Each data set has the following format:

l     the length of the code

l     the sequence of numbers representing the code

l     the number of integers in the gate protection

l     the sequence of numbers representing the gate protection 

 code_dimension
integerinteger2 … integercode_dimension 
protection_dimension
integerinteger2 … integerprotection_dimension

White spaces may occur freely in the input.

Output

The results must be printed on the standard output. For each given data set, print the result on a separate line. The result is a number that represents the position (starting from zero) of the first occurrence of the code in the gate protection, or the message no solution if there is no match.

Sample Input

30 1 272 3 4 0 1 2 521 464 1 4 1 4 431 2 373 2 1 3 2 255 7

Sample Output

31no solution

Problem Source

ZSUACM Team Member

// Problem#: 1282// Submission#: 834080// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include<stdio.h>#include<string.h>int next[60000];int s1[60000],s2[1000000];void getnext(int* s, int h){next[0] = -1;for (int i = 1; i<h; ++i) {int j = i - 1;while (next[j] >= 0 && s[i - 1] !=s [next[j]]) j = next[j];next[i] = next[j] + 1;}}int KMP(int* s, int h1, int* t, int h2) {int i = 0, j = 0;while(j < h2 && i < h1){if (j == -1 || s[i] == t[j]) ++i, ++j;else j = next[j];}if (j >= h2) return i - h2;  return -1;}int main(){int n, m;while (scanf("%d", &m)!=EOF){for (int i = 0; i < m; i++) scanf("%d", &s1[i]);scanf("%d", &n);for (int i = 0; i < n; i++) scanf("%d", &s2[i]);getnext(s1, m);int ans = KMP(s2, n, s1, m);if (ans >= 0) printf("%d\n", ans);else printf("no solution\n");}return 0;}