Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are probably overwriting crucial memory.</p> <blockquote> <p>The length of the needle is only limited by the memory available to your program, so do not make any assumptions - instead, read the length and allocate memory as needed. The haystack is not limited in size, which implies that your program should not read the whole haystack at once. The KMP algorithm is stream-based, i.e. it processes the haystack character by character, so this is not a problem.</p> </blockquote> <p>You can count on some inputs being longer than 10000 <code>char</code>s. That means you're using unallocated memory, with unpredictable consequences.</p> <p>More predictable is that - as already mentioned by Jonathan Leffler - your <code>substr</code> will generally not be 0-terminated, so the <code>strcmp</code> can only return 0 if perchance <code>substr</code> is immediately followed by a '\0' character, thus you're likely to miss occurrences of <code>needle</code> in <code>haystack</code> (and likely to clobber haystack in the course of your algorithm).</p> <p>And your algorithm is the naive algorithm (somewhat enhanced by scanning specifically for the first character of <code>needle</code>), that is probably too slow:</p> <blockquote> <p>However, a naive approach will probably exceed the time limit, whereas other algorithms are more complicated... The choice is yours.</p> </blockquote> <p>You should for each test case</p> <ol> <li>read the length of the needle</li> <li>allocate enough space for the needle (including the 0-terminator)</li> <li>read in the needle</li> <li>read in the haystack in chunks, interleaved with scanning for the needle</li> </ol> <p>SPOJ recommends using the KMP algorithm, not without reason. Using the Boyer-Moore algorithm is a good alternative, but makes handling matches that cross chunk boundaries more complicated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload