forked from TECHOUS/DSKaKhel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringSearch.cpp
More file actions
145 lines (130 loc) · 2.93 KB
/
StringSearch.cpp
File metadata and controls
145 lines (130 loc) · 2.93 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* Program to search wether a substrings t exists in one string S */
#include<bits/stdc++.h>
typedef long long int ll;
#define vi vector<ll>
#define pi pair<ll,ll>
#define pb push_back
#define F first
#define S second
#define B begin()
#define E end()
#define mp make_pair
using namespace std;
void count_sort(vector<int> &p, vector<int> &c){
int n =p.size();
vector<int> cnt(n);
for(auto x: c){
cnt[x]++;
}
vector<int> p_new(n);
vector<int> pos(n);
pos[0]=0;
for(int i = 1; i < n; i++){
pos[i] = pos[i-1] + cnt[i-1];
}
for(auto x : p){
int i = c[x];
p_new[pos[i]] = x;
pos[i]++;
}
p = p_new;
}
vector<int> suffixArray(string str, int n){
str+='$';
n+=1;
vector<pair<char,int> >index;
for(int i =0; i < n; i++){
index.pb(mp(str[i],i));
}
sort(index.B,index.E);
vector<int> p(n),c(n);
for(int i = 0; i < n; i++) p[i] = index[i].S;
int idx = 0;
c[p[0]] = 0;
for(int i = 1; i < n; i++){
if(index[i].F == index[i-1].F){
c[p[i]] = c[p[i-1]];
}
else{
c[p[i]] = c[p[i-1]]+1;
}
}
int k = 1, len = 1;
while(len < n){
for(int i = 0; i < n; i++){
p[i] = (p[i]- len +n) %n;
}
count_sort(p,c);
vector<int> c_new(n);
c_new[p[0]] = 0;
for(int i = 1; i < n; i++){
pair<int,int> prev = mp(c[p[i-1]], c[(p[i-1]+len)%n]);
pair<int,int> now = mp(c[p[i]], c[ ( p[i] + len ) % n ]);
if(now == prev){
c_new[p[i]] = c_new[p[i-1]];
}
else{
c_new[p[i]] = c_new[p[i-1]]+1;
}
}
c = c_new;
len*=2;
}
return p;
}
vector<int> pr;
string str,s;
bool searchWord(int start, int end){
int mid = start + (end- start)/2;
if(end >= start){
int nstart,nend;
bool sw = true;
for(int i = 0; i < s.size(); i++){
if(s[i] == str[pr[mid]+i]){
continue;
}
else{
if(s[i] > str[pr[mid]+i]){
nstart = mid+1;
nend = end;
}
else{
nstart = start;
nend = mid-1;
}
sw = false;
break;
}
}
if(sw){
return true;
}
else{
return searchWord(nstart,nend);
}
}
else{
return false;
}
}
int main ()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>str;
int n = str.length();
pr= suffixArray(str,n);
int cas;
cin>>cas;
while(cas--){
cin>>s;
bool res = searchWord(0,str.size());
if(res){
cout<<"Yes\n";
}
else{
cout<<"No\n";
}
}
return 0;
}