forked from sdssudhu/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSUB_PROB.cpp
More file actions
56 lines (47 loc) · 938 Bytes
/
Copy pathSUB_PROB.cpp
File metadata and controls
56 lines (47 loc) · 938 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Substring Problem
#include <bits/stdc++.h>
using namespace std;
int *generatePsArray(string &pattern) {
int *psa = new int[pattern.length()];
for(int i = 1, j = 0; i < pattern.length(); ++i) {
if(pattern[i] == pattern[j]) {
psa[i] = j + 1;
++i; ++j;
}
else if(j != 0)
j = psa[j - 1];
else {
psa[i] = 0;
++i;
}
}
return psa;
}
bool search(string &text,string &pattern) {
int *psa = generatePsArray(pattern);
int i, j;
for(i = 0, j = 0; i < text.length() && j < pattern.length();) {
if(text[i] == pattern[j]) {
++i; ++j;
}
else if(j != 0)
j = psa[j - 1];
else ++i;
}
delete[] psa;
return j == pattern.length();
}
int main() {
int n;
string text;
cin>>text>>n;
for(int i = 0; i < n; ++i) {
string pattern;
cin>>pattern;
if(search(text, pattern))
cout<<"Y"<<endl;
else
cout<<"N"<<endl;
}
return 0;
}