Wednesday 27 May 2015

light oj-1255

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char pat[1000009];
char txt[1000009];

int count;
void f_f(char *pat, int *f)
{
    f[0]=0;

    int len = 0,k=1;
   int p=strlen(pat);

     while (k < p)
     {
       if (pat[k] == pat[len])
        {
           len++;
           f[k]=len;
           k++;
        }
       else
       {
         if (len!=0) len = f[len-1];

         else
         {
             f[k]=0;
             k++;
         }
       }
    }
}

int KMPSearch(char *pat, char *txt)
{
    int p = strlen(pat);
    int t = strlen(txt);
int *f = (int *)malloc(sizeof(int)*p);

f_f(pat,f);

    int i  = 0,j=0;

     while (i < t)
    {
      if (pat[j] == txt[i])
      {
        j++;
        i++;
      }

      if (j == p)
      {
          count++;
           j = f[j-1];
         }

      else if (pat[j] != txt[i])
     {
        if (j!=0)
            j= f[j-1];

        else
            i++;

        }
}
free(f);

}
int main()
{
    int test,z=0,c;


    scanf("%d",&test);
     for(z=1;z<=test;z++){


      scanf("%s %s",txt,pat);
      count=0;
      KMPSearch(pat, txt);

    printf("Case %d: %d\n",z,count);}

    }

1 comment:

  1. Blackjack - MGM Grand Hotel & Casino Tunica
    Blackjack is a popular card game that is 김포 출장샵 played by two friends. This 아산 출장샵 variation of the game, which 군산 출장샵 is 통영 출장샵 played with two people at 제주도 출장마사지 the same time, is played by two

    ReplyDelete