HDU 1686 Oulipo (kmp), hdu1686oulipokmp
Problem DescriptionThe French author Georges Perec (1936-1982) once wrote a book, La disparition, without the letter 'E '. he was a member of the Oulipo group. A quote from the book:
Tout avait Pair normal, mais tout s 'affirmait faux. tout avait Fair normal, d 'abord, puis surgissait l 'inhumain, l' affolant. il aurait voulu savoir o locale s 'articulait l' association qui l 'unissait au roman: stir son tapis, assaillant à tout instant son imagination, l 'intuition d' un tabou, la vision d 'un mal obscur, d' un quoi vacant, d' un non-dit: la vision, l 'avision d' un oubli commandant tout, o ù s' Abolissait la raison: tout avait l 'air normal mais...
Perec wowould probably have scored high (or rather, low) in the following contest. people are asked to write a perhaps even meaningful text on some subject with as few occurrences of a given "word" as possible. our task is to provide the jury with a program that counts these occurrences, in order to obtain a ranking of the competitors. these competitors often write very long texts with nonsense meaning; a sequence of 500,000 consecutive's is not unusual. and they never use spaces.
So we want to quickly find out how often a word, I. e ., a given string, occurs in a text. more formally: given the alphabet {'A', 'B', 'C ',..., 'Z'} and two finite strings over that alphabet, a word W and a text T, count the number of occurrences of W in T. all the consecutive characters of W must exactly match consecutive characters of T. occurrences may overlap.
InputThe first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:
One line with the word W, a string over {'A', 'B', 'C ',..., 'Z'}, with 1 ≤ | W | ≤10,000 (here | W | denotes the length of the string W ).
One line with the text T, a string over {'A', 'B', 'C ',..., 'Z'}, with | W | ≤ | T | ≤ 1,000,000.
OutputFor every test case in the input file, the output shoshould contain a single number, on a single line: the number of occurrences of the word W in the text T.
Sample Input
3BAPCBAPCAZAAZAZAZAVERDIAVERDXIVYERDIAN
Sample Output
130
Source
Kmp Template
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define N 1000005char a[N],b[N];int ans,next[N];void getfail(char *b){int i,j;int len=strlen(b);next[0]=-1;i=0;j=-1;while(i<len){if(j==-1||b[i]==b[j]){i++;j++;next[i]=j;}elsej=next[j];}}void kmp(char *a,char *b){int i,j;int lena=strlen(a);int lenb=strlen(b);i=j=0;while(i<lena){if(j==-1||a[i]==b[j]){i++;j++;}elsej=next[j];if(j==lenb){ans++;j=next[j];}}}int main(){int i,j,t;scanf("%d",&t);while(t--){scanf("%s%s",b,a);getfail(b);ans=0;kmp(a,b);printf("%d\n",ans);}return 0;}
ACM hdu 1711 (KMP), the result is displayed, how can the AC fail?
# Include <iostream>
Using namespace std;
Int m, n, str1 [1000005], str2 [10005], next [10005];
Void getnext (int str2 [])
{
Int I = 0, j =-1;
Next [0] =-1;
While (I <m-1)
{
If (j =-1 | str2 [I] = str2 [j])
{
I ++;
J ++;
If (str2 [I]! = Str2 [j])
Next [I] = j;
Else
Next [I] = next [j];
}
Else j = next [j];
}
}
Int kmp (int str1 [], int str2 [], int pos)
{
Int I = pos, j = 0;
While (I <n & j <m)
{
If (j =-1 | str1 [I] = str2 [j])
{
I ++;
J ++;
}
Else
J = next [j];
}
If (j> = m)
Return I-m + 1;
Else
Return-1;
}
Int main ()
{
Int t, I;
Cin> t;
While (t --)
{
// If (t = 0) you miss the last group of input
// Return 0;
Cin> n> m;
For (I = 0; I <n; I ++)
Cin> str1 [I];
For (I = 0; I <m; I ++)
Cin> str2 [I];
Getnext (str2 );
Cout <kmp (str1, str2, 0) <endl;
}
Return 0;
}
Acm hdu 1711 KMP algorithm, where is the problem in my program? It is always WA. What is the error?
# Include <iostream>
Using namespace std;
Const int n= 1000005;
Const int M = 10005;
Int a1 [N], a2 [M], Next [M];
Void getnext (int m) // your getnext is faulty.
{
Int I = 2, j = 0;
Next [0] =-1;
Next [1] = 0;
While (I <m)
{
If (a2 [I-1] = a2 [j])
{
J ++;
Next [I] = j;
I ++;
}
Else if (j> 0) j = Next [j];
Else
{
Next [I] = 0;
I ++;
}
}
}
Int kmp (int n, int m)
{
Int j = 0, I = 0;
While (I <n)
{
If (a1 [I] = a2 [j])
{
++ I, ++ j;
If (j = m) break;
}
Else if (j = 0) ++ I;
Else j = Next [j];
}
If (j = m) return I-j + 1;
Return-1;
}
Int main ()
{
Int cases;
Int m, n, I;
Cin> cases;
While (cases --)
{
Cin> n> m;
For (I = 0; I <n; ++ I)
Cin> a1 [I];
For (I = 0; I <m; ++ I)
Cin> a2 [I];
Getnext (m );
Cout <kmp (n, m) <endl;
}
Return 0;
}