Runtime Error - Last executed input:"mississippi", "mississippi", but run fine locally


  • 1
    N

    I intended to implement KMP in this problem.
    But the submission result turned out to be:
    Runtime Error. Last executed input:"mississippi", "mississippi"
    It runs well locally in my VS2013.I Don't know why.
    Thanks for your time

    void next(char* needle,int P[],int m)
    {
        P[0] = 0;
        int j = 0;
        for (int i = 1; i < m; ++i)
        {
        	if (needle[i] == needle[j])
        	{
        	    j = j + 1;
        	    P[i] = P[i - 1] + 1;
        	}
        	else
        	{
        		P[i] = 0;
        		j = 0;
        	}
        }
    }
        	
    int strStr(char *haystack, char *needle) {
        int n = strlen(haystack);
        int m = strlen(needle);
        int *P = new int(m);
        next(needle, P, m);
        	
        int j = -1;
        for (int i = 0; i < n; ++i)
        {
        	while (j!=-1 && haystack[i] != needle[j+1]) j = P[j]-1;
        	if (haystack[i] == needle[j+1]) j = j + 1;
        	if (j == m-1) return i - m +1;
        }
        
        return -1;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.