SPOJ:FINDSR

No comments
Problem:
http://www.spoj.com/problems/FINDSR/
This problem can be solved using kmp prefix function
Below is the working code for above problem
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
void prefix(char *s,int n,int *pref)
{
 int x,y;
 int j=0,i;
 pref[0]=0;
 for(int i=1;i<n;i++)
 {
  while(j>0 && s[j]!=s[i])
  j=pref[j-1];
  if(s[i]==s[j]) 
  j++;
  pref[i]=j;
 }
 int ans=1;
 x=n-pref[n-1];
 if(n%x==0)
 ans=n/x;
 printf("%d\n",ans); 
}
int main()
{
 
 char pat[100005];
 scanf("%s",pat);
 while(strcmp(pat,"*")!=0)
 {
 int n=strlen(pat);
 int pref[n+1];
 prefix(pat,n,pref);
 scanf("%s",pat);
 }
} 

No comments :

SPOJ:NHAY

No comments
Problem:
http://www.spoj.com/problems/NHAY/
Here is given a simple problem related to string matching often called as a needle in a haystack
This problem is easily solved with Kmp algorithm
Below is the working code for above problem
#include<cstdio>
#include<cstring>
using namespace std;
char pat[1000010];
char text[1000010];
int pref[1000010];
void prefix(char *s)
{
 int n=strlen(s),flag=1;
 int j=0,i;
 pref[0]=0;
 for(int i=1;i<n;i++)
 {
  while(j>0 && s[j]!=s[i])
  j=pref[j-1];
  if(s[i]==s[j]) 
  j++;
  pref[i]=j;
 }
 
 
}
void search(char *text,char *pat)
{
 int len=strlen(text),flag=1;
 int m=strlen(pat);
 int j=0;
 for(int i=0;i<len;i++)
 {
  while(j>0 && text[i]!=pat[j])
  j=pref[j-1];
  if(text[i]==pat[j])
  j++;
  if(j==m)
  {
  printf("%d\n",i-m+1);
  j=pref[j-1];
  }
 }
 if(flag==1)
        printf("\n");
}
int main()
{
 int n;
 while(scanf("%d\n",&n)!=EOF)
        {
      memset(pref,0,sizeof(pref));
      scanf("%s",pat);
  prefix(pat);
  scanf("%s",text);
  search(text,pat);
 }
 
} 

No comments :