Hi,
I tried to solve SPOJ problem NHAY (https://www.spoj.pl/problems/NHAY/) using KMP algorithm.
The judge gives TLE though my code is in C++.
Please help me. Any small hint is appreciated.
Here is my code.
Hi,
I tried to solve SPOJ problem NHAY (https://www.spoj.pl/problems/NHAY/) using KMP algorithm.
The judge gives TLE though my code is in C++.
Please help me. Any small hint is appreciated.
Here is my code.
You should try simple string find function available in string container. Here is my implementation
#include<bits/stdc++.h>
using namespace std;
int main()
{
int x;
while(scanf("%d",&x)!=EOF)
{
string pat,txt;
cin>>pat;
cin>>txt;
int l=pat.size();
int m=txt.size();
if(l>m)
{
printf("\n");
}
else
{
for(int i=0;i<m;i++)
{
int k=txt.find(pat,i);
if(k>=0 && k<m)
printf("%d\n",k);
else
break;
i=k;
}
}
printf("\n");
}
return 0;
}
if u have any query fill free to ask.