Title Description:
Finding all occurrences of a pattern in a text are a problem that arises frequently in text-editing programs.
Typically,the text is a document being edited,and the pattern searched for was a particular word supplied by the user.
We assume that the text is a array t[1..n] of length n and that the pattern was an array p[1..m] of length m<=n.we Furt Her assume the elements of P and T is all alphabets (∑={a,b...,z}). The character arrays P and T are often called strings of characters.
We say this pattern P occurs with shift s in the text T if 0<=s<=n and t[s+1..s+m] = p[1..m] (that's if T[S+J]=P[J], For 1<=j<=m).
If P occurs with shift s in T,then we call s a valid SHIFT;OTHERWISE,WE calls a invalid shift.
Your task is to calculate the number of Vald shifts for the given text T and P Attern p.
Input:
For each case, there is strings T and P on a line,separated by a single space. You may assume both the length of T and P would not be exceed 10^6.
Output:
You should output a number in a separate Line,which indicates the number of valid shifts for the given text T and pattern P.
Sample input:
Abababab Abab
Sample output:
3
#include <stdio.h>#include<string.h>using namespacestd;intMain () {Chara[100001],b[100001]; while(SCANF ("%s%s", A, b)! =EOF) { intnum=0, I,j; intlen1=strlen (a); intLen2=strlen (b); for(i=0; i<=len1-len2;i++){ intflag=1; for(intj=0; j<len2;j++){ if(A[i+j]!=b[j]) flag=0; } if(flag==1) num++; } printf ("%d\n", num); } return 0;}
1094.String Matching