1094.String Matching

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.