SUB_PROB - Substring Problem

no tags 

String matching is an important problem in computer science research and finds applications in bioinformatics, data mining, pattern recognition, internet security and many more areas.

The problem we consider here is a smaller version of it. You are given a string M and N other strings smaller in length than M. You have to find whether each of these N strings is a substring of M. All strings consist of only alphanumeric characters.

You are required to write a C/CPP code to solve the problem.

Input

Input to the program consists of a series of lines. The first line contains the string M (no more than 100000 characters long). The next line contains an integer N (<1000) the number of query strings. Each of the next N lines contain a string S (each of which is no more than 2000 characters long).

Output

Output should consist of N lines each with a character 'Y' or 'N' indicating whether the string S is a substring of string M or not.

Example

Input:
abghABCDE
2
abAB
ab

Output:
N
Y

Note: The test data for this problem not only consist of the official test cases from the contest, as well some cases of my own.

A test case was added on 25.7.2010, after rejudging 3 users lose accepted.


hide comments
phoemur: 2018-12-12 01:45:18

Don't waste your time with linear search algorithms like KMP, Boyer-Moore, Rabin Karp, Z algorithm, etc...

If you don't use the (...) algorithm or something like that you will get TLE.

nidhi_061: 2018-06-28 09:28:37

I am using Rabin Karp algorithm. It is passing only 7 test cases and every test case provided in the toolkit. can someone please check the code? https://ideone.com/QAUSut

Last edit: 2018-06-28 11:10:17
imkiller: 2018-05-30 12:02:08

Getting TLE using KMP what to do?

kuszi: 2015-10-10 10:52:59

@tehnar: This is possible however, it will be much safer if your codes will not really on white spaces encoding.

tehnar: 2015-10-10 10:10:27

**** it. End of line in this task is "\r\n" instead of "\n"

I suppose it's because of tests were generated on Windows.
Isn't it possible, when adding new problem to SPOJ, to generate all the tests on server? Or at least run validator to check correctness of tests

Last edit: 2015-10-10 10:46:16
SHASHI BHUSHAN KUMAR: 2015-08-03 12:48:25

nice que

(Tjandra Satria Gunawan)(曾毅昆): 2015-05-25 23:51:10

If you getting WA, try duplicate pattern, I got 4 WAs because of that..
here is one example:
input:
a
2
a
a
output should be:
Y
Y

Martin Radev: 2015-05-05 18:16:13

@Md.shamiul islam The idea of KMP is right, but you have to improve it. For example, say that you have a pattern TONIKA and then another pattern NIKET and the text BARTONIKETA. When searching, you have found TONIK, but the next character is E. Note that NIK which is a suffix of TONIK is a prefix of NIK. => It makes sense to continue with NIKET at E, right?
This is the idea behind Aho-Corasick automation. Read about it ;)

Md.shamiul islam: 2015-05-05 17:47:15

I use KMP but TLE what can i do now

gratitude: 2015-02-13 05:04:11

getting tle using suffix array :(


Added by::(){ :|: & };:
Date:2010-07-02
Time limit:0.100s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:ASM32-GCC MAWK BC C-CLANG C NCSHARP C++ 4.3.2 CPP CPP14 CPP14-CLANG C99 COBOL COFFEE D-CLANG D-DMD DART ELIXIR FANTOM FORTH GOSU GRV JS-MONKEY JULIA KTLN NIM OBJC OBJC-CLANG OCT PICO PROLOG PYPY PYPY3 R RACKET RUST CHICKEN SQLITE SWIFT UNLAMBDA VB.NET
Resource:Codefest 2010 CW R#2