-
-
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
-
Source:
-
2006 Shanghai Jiao Tong University computer Research Life Test real problem
-
Answering:
-
problem solving problems? to discuss the subject of discussion please visit: http://t.jobdu.com/thread-7817-1-1.html
#include <stdio.h> #include <string.h> char s1[1000001]; char s2[1000001]; int main () {while (scanf ("%s%s", S1,s2)!=eof) { int ans=0; char *s=s1; while (S=strstr (S,S2)) { s++; ans++; } printf ("%d\n", ans); } return 0; }/************************************************************** problem:1094 user:vhreal Language:c result:accepted time:20 ms memory:2868 kb********************************************* *******************/
Nine degrees oj-topic 1094:string Matching