Moderator: Board moderators
ptargino wrote:The test cases of this problems are wrong. Actually, you just need to verify if each substring is prefix of the original. I've done this way and got AC.
#include<stdio.h>
#include<string.h>
int pi[100001],len_st,len_t;
char st[100001],t[1001];
void prefix_function()
{
int k=0,i;
pi[0]=0;
for(i=1;i<len_t;i++)
{
while(k>0&&t[k]!=t[i])
k = pi[k];
if(t[k]==t[i])
k++;
pi[i] = k;
}
}
int main()
{
bool found;
int i,n,c,j;
scanf("%d%*c",&n);
while(n--)
{
gets(st);
len_st = strlen(st);
scanf("%d%*c",&c);
while(c--)
{
gets(t);
len_t = strlen(t);
prefix_function();
j=0;found=0;
for(i=0;i<len_st;i++)
{
while(j>0&&t[j]!=st[i])
j = pi[j];
if(t[j]==st[i])
j++;
if(j==len_t-1){found = 1; break;}
}
if(found)puts("y");
else puts("n");
}
}
return 0;
}void prefix_function()
{
int k = -1,i;
pi[0] = -1;
for(i = 1; i < len_t; i++)
{
while(k > -1 && t[k+1] != t[i])
k = pi[k];
if(t[k+1]==t[i])
k++;
pi[i] = k;
}
}
bool KMP_match(char txt[], chat pattern[]) // we search for "pattern" in "txt"
{
int i = 0, j = 0; // i - index of "txt" , j - index of "pattern"
int tlen = strlen(txt), plen = strlen(pattern);
while(1)
{
if(i==tlen) break;
if(txt[i]==pattern[j])
{
++i;
++j;
}
else if(j > 0)
{
j = pi[j-1] + 1;
}
else
++i;
if(j==plen)
return true; // we have found a successful match :)
}
return false; // no match!!
}
ptargino wrote:The test cases of this problems are wrong. Actually, you just need to verify if each substring is prefix of the original. I've done this way and got AC.
Users browsing this forum: No registered users and 1 guest