A question about C language OJ: Time limit exceeded.
There are two problems:
If the ori string itself is shorter than pat, there is no need to match, and the direct result is 0.
If I characters have been matched, the fourth group number of the test is that pat matches from the beginning, but it actually matches from the place where pat subscripts to 3.
Finally, there are too many codes, and logical judgment affects the speed, with KMP function code.
int? KMP (Char? *ori,? Charles? *pat)
{
Charles? * Temperature? * p;
int? num? =? Strlen (Pat);
int? Me? =? 0,? j? =? 0;
int? * Next;
int? sum? =? 0;
//Find the next array
Next? =? (int? *)malloc((num? +? 1)* sizeof(int));
memset((int? *) Next,? 0,? (num? +? 1)* sizeof(int));
p? =? Temporary workers? =? Pat;
pat++;
What time? (* Pat)
{
What if? (* Pat? ==? * temperature)
{
* (Next? +? Me? +? 2)? =? j? +? 1;
pat++;
temp++;
j++;
}
other
{
pat++;
j? =? 0;
}
i++;
}
//Match string
Pat? =? p;
Me? =? 0;
What time? (*ori)
{
What if? (strlen(ori)? & lt? Stern (Pat))
Break;
What if? (*ori? ==? *pat)
{
ori++;
pat++;
i++;
What if? (* Pat? ==? '\0')
{
sum++;
Pat? =? p? +? Next [me];
Me? =? Next [me];
}
}
other
{
What if? (me? ==? 0)
ori++;
Pat? =? p? +? Next [me];
Me? =? Next [me];
}
}
Return? Sum; ?
} Then I applied for another one, mainly to store the corresponding number when 0 matches.