-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcount_of_substring_in_a_string.cpp
More file actions
164 lines (161 loc) · 4.04 KB
/
count_of_substring_in_a_string.cpp
File metadata and controls
164 lines (161 loc) · 4.04 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//Inshallah , Boys played well..!!
//Saurabh Srivastava//
#pragma GCC optimize("O3", "unroll-loops")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define pb push_back
#define pf push_front
#define mk make_pair
#define f(i,j,n) for(int i=j;i<n;i++)
#define r(i,n,j) for(int i=n-1;i>=j;i--)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define Test int t;cin>>t;while(t--)
#define mod 1000000007
#define MIN INT_MIN
#define MAX INT_MAX
#define all(container) container.begin() , container.end()
#define rall(container) container.rbegin() , container.rend()
#define sz(container) (int)container.size()
#define pii pair <int , int>
#define fi first
#define se second
#define setp(x) setprecision(x)
#define maxn 1000005
int n;
string s,ss;
ll power(ll x,ll y){
x%=mod;
y%=mod;
ll var=x,ans=1;
while(y){
if(y&1)
ans=((ans%mod)*(var%mod))%mod;
var=((var%mod)*(var%mod))%mod;
y=y>>1;
}
return ans;
}
void count_sort(vector<int> &p,vector<int> &c){
int n=sz(p);
vector<int> cnt(n) , pos(n);
f(i,0,n)
cnt[c[i]]++;
pos[0]=0;
f(i,1,n)
pos[i] = pos[i-1] + cnt[i-1];
vector<int> p_new(n);
f(i,0,n){
p_new[pos[c[p[i]]]++] = p[i];
}
p=p_new;
}
void solve(){
cin >> s;
s+="$";
n=s.length();
vector<int> p(n),c(n);
vector<pair<char,int>> vp;
f(i,0,n)
vp.pb({s[i],i});
sort(all(vp));
f(i,0,n) p[i]=vp[i].se;
c[p[0]]=0;
f(i,1,n){
if(vp[i].fi==vp[i-1].fi)
c[p[i]]=c[p[i-1]];
else
c[p[i]]=c[p[i-1]]+1;
}
int k=0;
while((1<<k)<n){
f(i,0,n){
p[i]=(p[i]-(1<<k)+n)%n;
}
count_sort(p,c);
vector<int> c_new(n);
c_new[p[0]]=0;
f(i,1,n){
pii prev={c[p[i-1]],c[(p[i-1]+(1<<k))%n]};
pii now={c[p[i]],c[(p[i]+(1<<k))%n]};
if(prev==now)
c_new[p[i]]=c_new[p[i-1]];
else
c_new[p[i]]=c_new[p[i-1]]+1;
}
c=c_new;
k++;
}
int q;
cin >> q;
while(q--){
cin >> ss;
int m=sz(ss);
if(m>=n){
cout << "0\n";
continue;
}
int low=1,high=n-1;
bool flag=false;
while(low<=high){
int mid=(low+high)/2;
f(i,0,m){
if(ss[i]<s[i+p[mid]]){
high=mid-1;
break;
}
else if(ss[i]>s[i+p[mid]]){
low=mid+1;
break;
}
else if(i==m-1){
flag=true;
high=mid-1;
}
else if(i+p[mid]==n-2){
low=mid+1;
break;
}
}
}
if(flag){
ll ans=low;
low=1,high=n-1;
while(low<=high){
int mid=(low+high)/2;
f(i,0,m){
if(ss[i]<s[i+p[mid]]){
high=mid-1;
break;
}
else if(ss[i]>s[i+p[mid]]){
low=mid+1;
break;
}
else if(i==m-1){
low=mid+1;
}
else if(i+p[mid]==n-2){
low=mid+1;
break;
}
}
}
cout << high-ans+1 << endl;
}
else
cout << "0\n";
}
}
int main(){
fast;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t=1;
//cin >> t;
while(t--) solve();
}