Sphere Online Judge

SPOJ Problem Set (classical)

32. A Needle in the Haystack

Problem code: NHAY

Write a program that finds all occurences of a given pattern in a given input string. This is often referred to as finding a needle in a haystack.

The program has to detect all occurences of the needle in the haystack. It should take the needle and the haystack as input, and output the positions of each occurence, as shown below. The suggested implementation is the KMP algorithm, but this is not a requirement. However, a naive approach will probably exceed the time limit, whereas other algorithms are more complicated... The choice is yours.


The input consists of a number of test cases. Each test case is composed of three lines, containing:

  • the length of the needle,
  • the needle itself,
  • the haystack.

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.

The test cases come one after another, each occupying three lines, with no additional space or line breaks in between.


For each test case your program should output all positions of the needle's occurences within the haystack. If a match is found, the output should contain the position of the first character of the match. Characters in the haystack are numbered starting with zero.

For a given test case, the positions output should be sorted in ascending order, and each of these should be printed in a separate line. For two different test cases, the positions should be separated by an empty line.


Sample input:
2 na banananobano 6 foobar foo 9 foobarfoo barfoobarfoobarfoobarfoobarfoo

Sample output:



Note the double empty line in the output, which means that no match was found for the second test case.

Warning: large Input/Output data, be careful with certain languages
Added by:Michał Małafiejski
Time limit:5s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel Pentium G860 3GHz)
Languages:All except: SCM chicken
Resource:the problem was phrased and test data was supplied by Mr. Maciej 'hawk' Jarzębski

hide comments
2015-01-27 15:18:02 Dhruv Mullick
Thanks to STLs, we can simply ignore the second paragraph of input :)
2015-01-17 09:09:43 Faiz Malik
1st KMP :)
2015-01-09 19:25:34 MoHiB
Again silly mistakes coast me few runtime errors. .... Now AC..... :)
2014-12-31 12:48:37 Anmol Sood
Even with KMP , you might get TLE if you are using cin/scanf.
2014-12-27 21:31:22 Lawliet
getting a NZEC with java code....what to do?works perfectly on ideone.com http://ideone.com/LRm9LF

Last edit: 2014-12-27 22:04:11
2014-12-27 09:32:32 Abhishek
what should i choose for c++11 support ?
2014-12-24 16:48:21 ashish patel
how to know to check end of input
2014-10-23 04:15:52 Divij Bindlish
My AC Code 12700764 gives blank output for this. Weak test cases maybe?
2014-09-03 07:04:35 Eddy Cael
I can't understand... My solution with KMP runs in 1.20. But my other solution using string.find() runs in only 0.63 ... C++ library is more powerful? o.O
2014-08-31 13:11:31 Himanshu Jaju
AC with rabin-karp :)
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.