output
stringlengths 52
181k
| instruction
stringlengths 296
182k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
void qmax(int &x, int y) {
if (x < y) x = y;
}
void qmin(long long &x, long long y) {
if (x > y) x = y;
}
inline long long read() {
char s;
long long k = 0, base = 1;
while ((s = getchar()) != '-' && s != EOF && !(isdigit(s)))
;
if (s == EOF) exit(0);
if (s == '-') base = -1, s = getchar();
while (isdigit(s)) {
k = k * 10 + (s ^ '0');
s = getchar();
}
return k * base;
}
inline void write(int x) {
static char cnt, num[15];
cnt = 0;
if (!x) {
putchar('0');
return;
}
for (; x; x /= 10) num[++cnt] = x % 10;
for (; cnt; putchar(num[cnt--] + 48))
;
}
int n, m, q;
char b[1010], a[1010];
int dp[1010][1010];
int Next[1010];
int main() {
n = read(), m = read(), q = read();
scanf("%s", b + 1);
scanf("%s", a + 1);
int p;
Next[1] = 0;
p = 0;
for (int i = 2; i <= m; i++) {
while (p && a[p + 1] != a[i]) p = Next[p];
if (a[p + 1] == a[i]) {
p++;
Next[i] = p;
}
}
p = 0;
for (int i = 1; i <= n; i++) {
while ((p == m) || (p && a[p + 1] != b[i])) p = Next[p];
if (a[p + 1] == b[i]) {
p++;
}
if (p == m) {
dp[i - m + 1][i] = 1;
}
}
for (int l = m; l < n; l++)
for (int i = 1, j = i + l; j <= n; i++, j++) {
dp[i][j] = dp[i][j - 1];
if (j - m + 1 > 0) dp[i][j] = dp[i][j - 1] + dp[j - m + 1][j];
}
int X, Y;
while (q--) {
X = read();
Y = read();
printf("%d\n", dp[X][Y]);
}
return 0;
}
| ### Prompt
Please create a solution in cpp to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void qmax(int &x, int y) {
if (x < y) x = y;
}
void qmin(long long &x, long long y) {
if (x > y) x = y;
}
inline long long read() {
char s;
long long k = 0, base = 1;
while ((s = getchar()) != '-' && s != EOF && !(isdigit(s)))
;
if (s == EOF) exit(0);
if (s == '-') base = -1, s = getchar();
while (isdigit(s)) {
k = k * 10 + (s ^ '0');
s = getchar();
}
return k * base;
}
inline void write(int x) {
static char cnt, num[15];
cnt = 0;
if (!x) {
putchar('0');
return;
}
for (; x; x /= 10) num[++cnt] = x % 10;
for (; cnt; putchar(num[cnt--] + 48))
;
}
int n, m, q;
char b[1010], a[1010];
int dp[1010][1010];
int Next[1010];
int main() {
n = read(), m = read(), q = read();
scanf("%s", b + 1);
scanf("%s", a + 1);
int p;
Next[1] = 0;
p = 0;
for (int i = 2; i <= m; i++) {
while (p && a[p + 1] != a[i]) p = Next[p];
if (a[p + 1] == a[i]) {
p++;
Next[i] = p;
}
}
p = 0;
for (int i = 1; i <= n; i++) {
while ((p == m) || (p && a[p + 1] != b[i])) p = Next[p];
if (a[p + 1] == b[i]) {
p++;
}
if (p == m) {
dp[i - m + 1][i] = 1;
}
}
for (int l = m; l < n; l++)
for (int i = 1, j = i + l; j <= n; i++, j++) {
dp[i][j] = dp[i][j - 1];
if (j - m + 1 > 0) dp[i][j] = dp[i][j - 1] + dp[j - m + 1][j];
}
int X, Y;
while (q--) {
X = read();
Y = read();
printf("%d\n", dp[X][Y]);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int N, M, Q;
string S, T;
int ok[1010];
void solve() {
int i, j, k, l, r, x, y;
string s;
cin >> N >> M >> Q >> S >> T;
for (i = 0; i < (N); i++) {
ok[i + 1] = ok[i];
if (i + M <= N) {
x = 1;
for (j = 0; j < (M); j++)
if (S[i + j] != T[j]) x = 0;
ok[i + 1] += x;
}
}
while (Q--) {
cin >> x >> y;
y -= M - 1;
if (y < x) {
cout << 0 << endl;
} else {
cout << ok[y] - ok[x - 1] << endl;
}
}
}
int main(int argc, char** argv) {
string s;
int i;
if (argc == 1) ios::sync_with_stdio(false), cin.tie(0);
for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s += '\n';
for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin);
cout.tie(0);
solve();
return 0;
}
| ### Prompt
In Cpp, your task is to solve the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int N, M, Q;
string S, T;
int ok[1010];
void solve() {
int i, j, k, l, r, x, y;
string s;
cin >> N >> M >> Q >> S >> T;
for (i = 0; i < (N); i++) {
ok[i + 1] = ok[i];
if (i + M <= N) {
x = 1;
for (j = 0; j < (M); j++)
if (S[i + j] != T[j]) x = 0;
ok[i + 1] += x;
}
}
while (Q--) {
cin >> x >> y;
y -= M - 1;
if (y < x) {
cout << 0 << endl;
} else {
cout << ok[y] - ok[x - 1] << endl;
}
}
}
int main(int argc, char** argv) {
string s;
int i;
if (argc == 1) ios::sync_with_stdio(false), cin.tie(0);
for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s += '\n';
for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin);
cout.tie(0);
solve();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s, t;
int n, m, q, l, r;
cin >> n >> m >> q;
cin >> s >> t;
int arr[10000] = {0};
for (int i = 0; i <= n - m; ++i) {
arr[i + 1] = arr[i] + (s.substr(i, m) == t);
}
while (q--) {
cin >> l >> r;
if (r - l + 1 < m)
cout << "0\n";
else
cout << arr[r - m + 1] - arr[l - 1] << endl;
}
return 0;
}
| ### Prompt
Please create a solution in CPP to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
string s, t;
int n, m, q, l, r;
cin >> n >> m >> q;
cin >> s >> t;
int arr[10000] = {0};
for (int i = 0; i <= n - m; ++i) {
arr[i + 1] = arr[i] + (s.substr(i, m) == t);
}
while (q--) {
cin >> l >> r;
if (r - l + 1 < m)
cout << "0\n";
else
cout << arr[r - m + 1] - arr[l - 1] << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, pre[2005], q;
char s[2005], t[2005];
int main() {
scanf("%d%d%d", &n, &m, &q);
scanf("%s%s", s, t);
for (int i = 0; i < n - m + 1; i++) {
int flag = 1;
for (int j = 0; j < m; j++)
if (s[i + j] != t[j]) {
flag = 0;
break;
}
if (flag) pre[i + 1]++;
}
for (int i = 1; i <= n; i++) pre[i] += pre[i - 1];
while (q--) {
int l, r;
scanf("%d%d", &l, &r);
printf("%d\n", pre[max(r - m + 1, l - 1)] - pre[l - 1]);
}
return 0;
}
| ### Prompt
Your task is to create a CPP solution to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, pre[2005], q;
char s[2005], t[2005];
int main() {
scanf("%d%d%d", &n, &m, &q);
scanf("%s%s", s, t);
for (int i = 0; i < n - m + 1; i++) {
int flag = 1;
for (int j = 0; j < m; j++)
if (s[i + j] != t[j]) {
flag = 0;
break;
}
if (flag) pre[i + 1]++;
}
for (int i = 1; i <= n; i++) pre[i] += pre[i - 1];
while (q--) {
int l, r;
scanf("%d%d", &l, &r);
printf("%d\n", pre[max(r - m + 1, l - 1)] - pre[l - 1]);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, Q, cnt = 0;
int a[1010], L[1010];
char s[1010], t[1010];
int main() {
scanf("%d%d%d", &n, &m, &Q);
scanf("%s%s", s, t);
for (int i = 0; i < n; i++) {
if (s[i] == t[0]) {
bool bo = 0;
for (int j = 1; j < m; j++) {
if (s[i + j] != t[j]) {
bo = 1;
break;
}
}
if (!bo) {
a[cnt] = i + 1;
L[i + 1] = cnt;
cnt++;
}
}
}
int x, y;
while (Q--) {
scanf("%d%d", &x, &y);
int sum = 0;
for (int i = 0; i < cnt; i++) {
if (a[i] >= x && a[i] + m - 1 <= y) sum++;
if (a[i] > y) break;
}
printf("%d\n", sum);
}
return 0;
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, Q, cnt = 0;
int a[1010], L[1010];
char s[1010], t[1010];
int main() {
scanf("%d%d%d", &n, &m, &Q);
scanf("%s%s", s, t);
for (int i = 0; i < n; i++) {
if (s[i] == t[0]) {
bool bo = 0;
for (int j = 1; j < m; j++) {
if (s[i + j] != t[j]) {
bo = 1;
break;
}
}
if (!bo) {
a[cnt] = i + 1;
L[i + 1] = cnt;
cnt++;
}
}
}
int x, y;
while (Q--) {
scanf("%d%d", &x, &y);
int sum = 0;
for (int i = 0; i < cnt; i++) {
if (a[i] >= x && a[i] + m - 1 <= y) sum++;
if (a[i] > y) break;
}
printf("%d\n", sum);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, m, q;
string s, t;
cin >> n >> m >> q >> s >> t;
map<long long, long long> d;
long long cont = 0;
for (long long i = (0); i < (long long)(n); i++) {
if (s.substr(i, m) == t) {
d[i] = ++cont;
}
}
while (q--) {
long long l, r;
cin >> l >> r;
l--;
r--;
auto it = d.lower_bound(l);
auto jt = d.upper_bound(r - m + 1);
if (jt == d.begin()) {
puts("0");
continue;
}
jt--;
long long x = jt->second - it->second + 1;
if (x < 0) x = 0;
printf("%lld\n", x);
}
}
| ### Prompt
Develop a solution in CPP to the problem described below:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, m, q;
string s, t;
cin >> n >> m >> q >> s >> t;
map<long long, long long> d;
long long cont = 0;
for (long long i = (0); i < (long long)(n); i++) {
if (s.substr(i, m) == t) {
d[i] = ++cont;
}
}
while (q--) {
long long l, r;
cin >> l >> r;
l--;
r--;
auto it = d.lower_bound(l);
auto jt = d.upper_bound(r - m + 1);
if (jt == d.begin()) {
puts("0");
continue;
}
jt--;
long long x = jt->second - it->second + 1;
if (x < 0) x = 0;
printf("%lld\n", x);
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int fx[] = {-1, 1, 0, 0};
int fy[] = {0, 0, -1, 1};
int dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
int kx[] = {1, 1, -1, -1, 2, 2, -2, -2};
int ky[] = {2, -2, 2, -2, 1, -1, 1, -1};
bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
return a.first < b.first;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m, q;
cin >> n >> m >> q;
string s, t;
cin >> s >> t;
s = '?' + s;
int ara[1010], sum[1010];
memset(ara, 0, sizeof(ara)), memset(sum, 0, sizeof(sum));
for (int i = 1; i <= n - m + 1; i++) {
ara[i] = 1;
for (int j = 0; j < t.size(); j++) {
if (s[i + j] != t[j]) {
ara[i] = 0;
break;
}
}
sum[i] = sum[i - 1] + ara[i];
}
for (int i = 1; i <= q; i++) {
int l, r;
cin >> l >> r;
if (r - l + 1 < m) {
cout << "0\n";
} else {
cout << sum[r - m + 1] - sum[l - 1] << "\n";
}
}
return 0;
}
| ### Prompt
Please formulate a CPP solution to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int fx[] = {-1, 1, 0, 0};
int fy[] = {0, 0, -1, 1};
int dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
int kx[] = {1, 1, -1, -1, 2, 2, -2, -2};
int ky[] = {2, -2, 2, -2, 1, -1, 1, -1};
bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
return a.first < b.first;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m, q;
cin >> n >> m >> q;
string s, t;
cin >> s >> t;
s = '?' + s;
int ara[1010], sum[1010];
memset(ara, 0, sizeof(ara)), memset(sum, 0, sizeof(sum));
for (int i = 1; i <= n - m + 1; i++) {
ara[i] = 1;
for (int j = 0; j < t.size(); j++) {
if (s[i + j] != t[j]) {
ara[i] = 0;
break;
}
}
sum[i] = sum[i - 1] + ara[i];
}
for (int i = 1; i <= q; i++) {
int l, r;
cin >> l >> r;
if (r - l + 1 < m) {
cout << "0\n";
} else {
cout << sum[r - m + 1] - sum[l - 1] << "\n";
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int main() {
int n, m, q;
cin >> n >> m >> q;
string a, b;
cin >> a >> b;
int mark[1000];
memset(mark, 0, sizeof(mark));
for (int i = 0; i < n; i++) {
if (a[i] == b[0]) {
int flag = 1, j;
for (j = 1; j < m && i + j < n; j++) {
if (a[i + j] != b[j]) {
flag = 0;
break;
}
}
if (flag && j == m) mark[i] = 1;
}
}
while (q--) {
int l, r;
scanf("%d%d", &l, &r);
int cnt = 0;
for (int i = l - 1; i <= r - 1; i++) {
if (mark[i] == 1 && i + m - 1 <= r - 1) cnt++;
}
cout << cnt << endl;
}
return 0;
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int main() {
int n, m, q;
cin >> n >> m >> q;
string a, b;
cin >> a >> b;
int mark[1000];
memset(mark, 0, sizeof(mark));
for (int i = 0; i < n; i++) {
if (a[i] == b[0]) {
int flag = 1, j;
for (j = 1; j < m && i + j < n; j++) {
if (a[i + j] != b[j]) {
flag = 0;
break;
}
}
if (flag && j == m) mark[i] = 1;
}
}
while (q--) {
int l, r;
scanf("%d%d", &l, &r);
int cnt = 0;
for (int i = l - 1; i <= r - 1; i++) {
if (mark[i] == 1 && i + m - 1 <= r - 1) cnt++;
}
cout << cnt << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e3 + 5;
char a[maxn], b[maxn];
int sum[maxn];
int n, m, q;
void solve() {
for (int i = 0; i < n; i++) {
int flag = 1;
for (int j = 0; j < m; j++)
if (a[i + j] != b[j]) {
flag = 0;
break;
}
if (flag) sum[i + 1]++;
sum[i + 1] += sum[i];
}
}
int main() {
scanf("%d %d %d", &n, &m, &q);
scanf(" %s %s", a, b);
solve();
while (q--) {
int l, r;
scanf("%d %d", &l, &r);
if (r - m + 1 >= 0 && r - m + 1 >= l - 1)
printf("%d\n", sum[r - m + 1] - sum[l - 1]);
else
puts("0");
}
return 0;
}
| ### Prompt
Generate a CPP solution to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e3 + 5;
char a[maxn], b[maxn];
int sum[maxn];
int n, m, q;
void solve() {
for (int i = 0; i < n; i++) {
int flag = 1;
for (int j = 0; j < m; j++)
if (a[i + j] != b[j]) {
flag = 0;
break;
}
if (flag) sum[i + 1]++;
sum[i + 1] += sum[i];
}
}
int main() {
scanf("%d %d %d", &n, &m, &q);
scanf(" %s %s", a, b);
solve();
while (q--) {
int l, r;
scanf("%d %d", &l, &r);
if (r - m + 1 >= 0 && r - m + 1 >= l - 1)
printf("%d\n", sum[r - m + 1] - sum[l - 1]);
else
puts("0");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, q;
int ans[1005];
int main() {
while (scanf("%d%d%d", &n, &m, &q) != EOF) {
string s1, s2;
cin >> s1 >> s2;
for (int i = 0; i + m <= n; i++) {
if (s1.substr(i, m) == s2) ans[i + 1]++;
}
for (int i = 2; i <= n; i++) ans[i] += ans[i - 1];
while (q--) {
int li, ri;
scanf("%d%d", &li, &ri);
ri = ri - m + 1;
printf("%d\n", max(0, ans[max(0, ri)] - ans[li - 1]));
}
}
}
| ### Prompt
In Cpp, your task is to solve the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, q;
int ans[1005];
int main() {
while (scanf("%d%d%d", &n, &m, &q) != EOF) {
string s1, s2;
cin >> s1 >> s2;
for (int i = 0; i + m <= n; i++) {
if (s1.substr(i, m) == s2) ans[i + 1]++;
}
for (int i = 2; i <= n; i++) ans[i] += ans[i - 1];
while (q--) {
int li, ri;
scanf("%d%d", &li, &ri);
ri = ri - m + 1;
printf("%d\n", max(0, ans[max(0, ri)] - ans[li - 1]));
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
char s[1002], t[1002];
int v[1002];
int main() {
int n, m, q, l, r, sol, i, ok, j;
cin >> n >> m >> q;
cin.get();
cin.getline(s, 1001);
cin.getline(t, 1001);
for (i = 0; i < n - m + 1; i++) {
ok = 1;
for (j = i; j <= i + m - 1; j++)
if (t[j - i] != s[j]) {
ok = 0;
break;
}
if (ok == 1) v[i + m - 1] += 1;
}
for (i = 1; i < n; i++) v[i] += v[i - 1];
for (i = 1; i <= q; i++) {
cin >> l >> r;
l--;
r--;
l += m - 1;
if (l > r)
cout << 0 << '\n';
else {
sol = v[r] - v[l - 1];
cout << sol << '\n';
}
}
return 0;
}
| ### Prompt
Generate a CPP solution to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
char s[1002], t[1002];
int v[1002];
int main() {
int n, m, q, l, r, sol, i, ok, j;
cin >> n >> m >> q;
cin.get();
cin.getline(s, 1001);
cin.getline(t, 1001);
for (i = 0; i < n - m + 1; i++) {
ok = 1;
for (j = i; j <= i + m - 1; j++)
if (t[j - i] != s[j]) {
ok = 0;
break;
}
if (ok == 1) v[i + m - 1] += 1;
}
for (i = 1; i < n; i++) v[i] += v[i - 1];
for (i = 1; i <= q; i++) {
cin >> l >> r;
l--;
r--;
l += m - 1;
if (l > r)
cout << 0 << '\n';
else {
sol = v[r] - v[l - 1];
cout << sol << '\n';
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const long long INF = 1LL << 60;
const long long mod = 1e9 + 7;
const long double eps = 1e-8;
const long double pi = acos(-1.0);
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
vector<int> Zalgo(const string& s) {
int n = (int)s.size();
vector<int> res(n);
res[0] = n;
int i = 1, j = 0;
while (i < n) {
while (i + j < n && s[j] == s[i + j]) ++j;
res[i] = j;
if (j == 0) {
++i;
continue;
}
int k = 1;
while (i + k < n && k + res[k] < j) res[i + k] = res[k], ++k;
i += k, j -= k;
}
return res;
}
void solve() {
int n, m, q;
cin >> n >> m >> q;
string s, t;
cin >> s >> t;
string u = t + s;
auto res = Zalgo(u);
vector<int> cnt(n + 1, 0);
for (int i = 0; i < n; i++) {
cnt[i + 1] = cnt[i] + (res[m + i] >= m ? 1 : 0);
}
for (int _ = 0; _ < q; _++) {
int l, r;
cin >> l >> r;
cout << cnt[max(r - m + 1, l - 1)] - cnt[l - 1] << endl;
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const long long INF = 1LL << 60;
const long long mod = 1e9 + 7;
const long double eps = 1e-8;
const long double pi = acos(-1.0);
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
vector<int> Zalgo(const string& s) {
int n = (int)s.size();
vector<int> res(n);
res[0] = n;
int i = 1, j = 0;
while (i < n) {
while (i + j < n && s[j] == s[i + j]) ++j;
res[i] = j;
if (j == 0) {
++i;
continue;
}
int k = 1;
while (i + k < n && k + res[k] < j) res[i + k] = res[k], ++k;
i += k, j -= k;
}
return res;
}
void solve() {
int n, m, q;
cin >> n >> m >> q;
string s, t;
cin >> s >> t;
string u = t + s;
auto res = Zalgo(u);
vector<int> cnt(n + 1, 0);
for (int i = 0; i < n; i++) {
cnt[i + 1] = cnt[i] + (res[m + i] >= m ? 1 : 0);
}
for (int _ = 0; _ < q; _++) {
int l, r;
cin >> l >> r;
cout << cnt[max(r - m + 1, l - 1)] - cnt[l - 1] << endl;
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
void z_function(string &s, vector<int> &z) {
int n = s.length(), l = -1, r = -1;
z.clear();
z.resize(n, 0);
z[0] = n;
for (int i = 0; i < n; i++) {
if (i <= r) z[i] = min(r - i + 1, z[i - l]);
while (z[i] + i < n && s[z[i]] == s[z[i] + i]) z[i]++;
if (z[i] + i - 1 > r) {
l = i;
r = i + z[i] - 1;
}
}
}
int n, m, q, l, r;
string s, t;
vector<int> z, pr;
int main() {
cin >> n >> m >> q;
cin >> s >> t;
n = s.length();
m = t.length();
s = t + '#' + s;
z_function(s, z);
pr.resize(n, 0);
if (z[m + 1] == m) pr[0] = 1;
for (int i = 1; i < n; i++) {
pr[i] = pr[i - 1];
if (z[m + 1 + i] == m) pr[i]++;
}
for (int i = 0; i < q; i++) {
cin >> l >> r;
l--;
r--;
if (r - m + 1 < 0) {
cout << 0 << endl;
continue;
}
if (l == 0)
cout << pr[r - m + 1];
else
cout << max(pr[r - m + 1] - pr[l - 1], 0);
cout << endl;
}
return 0;
}
| ### Prompt
Create a solution in cpp for the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void z_function(string &s, vector<int> &z) {
int n = s.length(), l = -1, r = -1;
z.clear();
z.resize(n, 0);
z[0] = n;
for (int i = 0; i < n; i++) {
if (i <= r) z[i] = min(r - i + 1, z[i - l]);
while (z[i] + i < n && s[z[i]] == s[z[i] + i]) z[i]++;
if (z[i] + i - 1 > r) {
l = i;
r = i + z[i] - 1;
}
}
}
int n, m, q, l, r;
string s, t;
vector<int> z, pr;
int main() {
cin >> n >> m >> q;
cin >> s >> t;
n = s.length();
m = t.length();
s = t + '#' + s;
z_function(s, z);
pr.resize(n, 0);
if (z[m + 1] == m) pr[0] = 1;
for (int i = 1; i < n; i++) {
pr[i] = pr[i - 1];
if (z[m + 1 + i] == m) pr[i]++;
}
for (int i = 0; i < q; i++) {
cin >> l >> r;
l--;
r--;
if (r - m + 1 < 0) {
cout << 0 << endl;
continue;
}
if (l == 0)
cout << pr[r - m + 1];
else
cout << max(pr[r - m + 1] - pr[l - 1], 0);
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MX = 1e3;
int sum[MX + 2];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, m, q, l, r;
string str, str2;
cin >> n >> m >> q;
cin >> str >> str2;
for (long long i = 0; i < n - m + 1; i++) {
sum[i] = (str.substr(i, m) == str2);
}
while (q-- > 0) {
cin >> l >> r;
l--, r--;
r = r - m + 1;
int ans = 0;
for (long long i = l; i <= r; i++) ans += sum[i];
cout << ans << '\n';
}
}
| ### Prompt
In Cpp, your task is to solve the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MX = 1e3;
int sum[MX + 2];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, m, q, l, r;
string str, str2;
cin >> n >> m >> q;
cin >> str >> str2;
for (long long i = 0; i < n - m + 1; i++) {
sum[i] = (str.substr(i, m) == str2);
}
while (q-- > 0) {
cin >> l >> r;
l--, r--;
r = r - m + 1;
int ans = 0;
for (long long i = l; i <= r; i++) ans += sum[i];
cout << ans << '\n';
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<int> vet;
int p[201010];
void kmp(string &s, int sz) {
p[0] = 0;
int idx = 0, n = (int)s.size();
for (int i = 1; i < n; i++) {
while (s[idx] != s[i] && idx > 0) idx = p[idx - 1];
if (s[idx] == s[i]) idx++;
p[i] = idx;
if (p[i] == sz) {
vet.push_back(i - sz - sz + 1);
}
}
}
int solve(int l, int r) {
if (l > r) return 0;
int a = lower_bound(vet.begin(), vet.end(), l) - vet.begin();
int b = upper_bound(vet.begin(), vet.end(), r) - vet.begin();
return b - a;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, q, l, r;
string s, t;
cin >> n >> m >> q;
cin >> s >> t;
string aux = t + "$" + s;
kmp(aux, (int)t.size());
sort(vet.begin(), vet.end());
while (q--) {
cin >> l >> r;
cout << solve(l, r - (int)t.size() + 1) << "\n";
}
return 0;
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> vet;
int p[201010];
void kmp(string &s, int sz) {
p[0] = 0;
int idx = 0, n = (int)s.size();
for (int i = 1; i < n; i++) {
while (s[idx] != s[i] && idx > 0) idx = p[idx - 1];
if (s[idx] == s[i]) idx++;
p[i] = idx;
if (p[i] == sz) {
vet.push_back(i - sz - sz + 1);
}
}
}
int solve(int l, int r) {
if (l > r) return 0;
int a = lower_bound(vet.begin(), vet.end(), l) - vet.begin();
int b = upper_bound(vet.begin(), vet.end(), r) - vet.begin();
return b - a;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, q, l, r;
string s, t;
cin >> n >> m >> q;
cin >> s >> t;
string aux = t + "$" + s;
kmp(aux, (int)t.size());
sort(vet.begin(), vet.end());
while (q--) {
cin >> l >> r;
cout << solve(l, r - (int)t.size() + 1) << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
using namespace std;
const int N = 1e3 + 5;
int sum[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
int n, m, q, l, r;
string s, t;
cin >> n >> m >> q;
cin >> s >> t;
for (int i = 0; i + m - 1 < n; i++) {
bool f = true;
for (int j = 0; j < m; j++) {
if (s[i + j] != t[j]) {
f = false;
break;
}
}
if (f) sum[i + 1]++;
}
for (int i = 1; i <= n; i++) sum[i] += sum[i - 1];
while (q--) {
cin >> l >> r;
r = r - m + 1;
if (r >= l)
cout << sum[r] - sum[l - 1] << endl;
else
cout << 0 << endl;
}
return 0;
}
| ### Prompt
Generate a CPP solution to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
using namespace std;
const int N = 1e3 + 5;
int sum[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
int n, m, q, l, r;
string s, t;
cin >> n >> m >> q;
cin >> s >> t;
for (int i = 0; i + m - 1 < n; i++) {
bool f = true;
for (int j = 0; j < m; j++) {
if (s[i + j] != t[j]) {
f = false;
break;
}
}
if (f) sum[i + 1]++;
}
for (int i = 1; i <= n; i++) sum[i] += sum[i - 1];
while (q--) {
cin >> l >> r;
r = r - m + 1;
if (r >= l)
cout << sum[r] - sum[l - 1] << endl;
else
cout << 0 << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const double PI = 2 * acos(0.0), EPS = 1e-9;
const int INF = 1e9, NINF = -1e9, MOD = 1e9 + 7;
void GO() { cout << fixed << setprecision(10); }
int main() {
GO();
int n, m, q, r, l, c, a[1001] = {};
string second, t;
cin >> n >> m >> q >> second >> t;
for (int i = 0; i <= n - m; i++)
if (second.substr(i, m) == t) a[i] = 1;
while (q--) {
c = 0;
cin >> r >> l;
for (int i = r - 1; i <= l - m; i++)
if (a[i]) c++;
cout << c << "\n";
}
return 0;
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double PI = 2 * acos(0.0), EPS = 1e-9;
const int INF = 1e9, NINF = -1e9, MOD = 1e9 + 7;
void GO() { cout << fixed << setprecision(10); }
int main() {
GO();
int n, m, q, r, l, c, a[1001] = {};
string second, t;
cin >> n >> m >> q >> second >> t;
for (int i = 0; i <= n - m; i++)
if (second.substr(i, m) == t) a[i] = 1;
while (q--) {
c = 0;
cin >> r >> l;
for (int i = r - 1; i <= l - m; i++)
if (a[i]) c++;
cout << c << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1111;
string s, t;
int sum[N];
int n, m, q;
signed main() {
ios_base::sync_with_stdio(false);
cin >> n >> m >> q >> s >> t;
for (int i = (1); i <= (n); i++) {
bool found = true;
for (int j = (0); j <= (m - 1); j++) {
int ii = i - 1 + j;
if (ii < n && s[ii] != t[j]) {
found = false;
break;
}
}
sum[i] = sum[i - 1] + found;
}
while (q--) {
int l, r;
cin >> l >> r;
int ans = 0;
if (r - m + 1 >= 1) {
ans = max(0, sum[r - m + 1] - sum[l - 1]);
}
cout << ans << "\n";
}
return 0;
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1111;
string s, t;
int sum[N];
int n, m, q;
signed main() {
ios_base::sync_with_stdio(false);
cin >> n >> m >> q >> s >> t;
for (int i = (1); i <= (n); i++) {
bool found = true;
for (int j = (0); j <= (m - 1); j++) {
int ii = i - 1 + j;
if (ii < n && s[ii] != t[j]) {
found = false;
break;
}
}
sum[i] = sum[i - 1] + found;
}
while (q--) {
int l, r;
cin >> l >> r;
int ans = 0;
if (r - m + 1 >= 1) {
ans = max(0, sum[r - m + 1] - sum[l - 1]);
}
cout << ans << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int maxn = 1010;
char a[maxn], b[maxn];
int n, m, q;
vector<int> pos;
void solve() {
while (q--) {
int l, r, tr;
scanf("%d%d", &l, &r);
tr = r - m + 1;
if (l > tr)
puts("0");
else {
printf("%d\n",
upper_bound(pos.begin(), pos.end(), tr) - pos.begin() -
(lower_bound(pos.begin(), pos.end(), l) - pos.begin()));
}
}
}
int main() {
cin >> n >> m >> q;
cin >> a + 1;
cin >> b + 1;
for (int i = 1; i <= n; ++i) {
bool ok = true;
for (int j = 0; j < m; ++j) {
if (i + j > n || (a[i + j] != b[j + 1])) {
ok = false;
break;
}
}
if (ok) pos.push_back(i);
}
solve();
return 0;
}
| ### Prompt
Your task is to create a cpp solution to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int maxn = 1010;
char a[maxn], b[maxn];
int n, m, q;
vector<int> pos;
void solve() {
while (q--) {
int l, r, tr;
scanf("%d%d", &l, &r);
tr = r - m + 1;
if (l > tr)
puts("0");
else {
printf("%d\n",
upper_bound(pos.begin(), pos.end(), tr) - pos.begin() -
(lower_bound(pos.begin(), pos.end(), l) - pos.begin()));
}
}
}
int main() {
cin >> n >> m >> q;
cin >> a + 1;
cin >> b + 1;
for (int i = 1; i <= n; ++i) {
bool ok = true;
for (int j = 0; j < m; ++j) {
if (i + j > n || (a[i + j] != b[j + 1])) {
ok = false;
break;
}
}
if (ok) pos.push_back(i);
}
solve();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, q, dp[10004];
string s, t;
int main() {
cin >> n >> m >> q >> s >> t;
for (int i = 0, ok = true; i <= n - m; ++i, ok = true) {
for (int j = 0; j < m; ++j) {
if (s[i + j] != t[j]) ok = false;
}
dp[i + 1] = dp[i] + ok;
}
for (int i = 0, l, r; i < q; ++i) {
cin >> l >> r;
cout << (l > r - m + 1 ? 0 : dp[r - m + 1] - dp[l - 1]) << endl;
}
}
| ### Prompt
Please create a solution in Cpp to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, q, dp[10004];
string s, t;
int main() {
cin >> n >> m >> q >> s >> t;
for (int i = 0, ok = true; i <= n - m; ++i, ok = true) {
for (int j = 0; j < m; ++j) {
if (s[i + j] != t[j]) ok = false;
}
dp[i + 1] = dp[i] + ok;
}
for (int i = 0, l, r; i < q; ++i) {
cin >> l >> r;
cout << (l > r - m + 1 ? 0 : dp[r - m + 1] - dp[l - 1]) << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, q;
cin >> n >> m >> q;
string s1, s2;
cin >> s1 >> s2;
int occ[n];
int sta[n];
int sum[n];
for (int o = 0; o < n; o++) {
occ[o] = 0;
sum[o] = 0;
sta[o] = 0;
}
int v = 0;
for (int e = 0; e < n - m + 1; e++) {
int fl = 1;
for (int l = 0; l < m; l++) {
if (s1[e + l] != s2[l]) {
fl = 0;
break;
}
}
if (fl == 1) occ[e + m - 1] = 1;
}
for (int g = 0; g < n; g++) {
v = v + occ[g];
sum[g] = v;
}
for (int a = 1; a <= q; a++) {
int p, q;
cin >> p >> q;
if (q - p < m - 1)
cout << "0" << endl;
else if (p != 1)
cout << sum[q - 1] - sum[p - 2 + m - 1] << endl;
else
cout << sum[q - 1] << endl;
}
}
| ### Prompt
Create a solution in CPP for the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, q;
cin >> n >> m >> q;
string s1, s2;
cin >> s1 >> s2;
int occ[n];
int sta[n];
int sum[n];
for (int o = 0; o < n; o++) {
occ[o] = 0;
sum[o] = 0;
sta[o] = 0;
}
int v = 0;
for (int e = 0; e < n - m + 1; e++) {
int fl = 1;
for (int l = 0; l < m; l++) {
if (s1[e + l] != s2[l]) {
fl = 0;
break;
}
}
if (fl == 1) occ[e + m - 1] = 1;
}
for (int g = 0; g < n; g++) {
v = v + occ[g];
sum[g] = v;
}
for (int a = 1; a <= q; a++) {
int p, q;
cin >> p >> q;
if (q - p < m - 1)
cout << "0" << endl;
else if (p != 1)
cout << sum[q - 1] - sum[p - 2 + m - 1] << endl;
else
cout << sum[q - 1] << endl;
}
}
``` |
#include <bits/stdc++.h>
int n, m, q, a0, a1;
char s[1003], t[1003];
int pp[1003];
int main() {
scanf("%d %d %d", &n, &m, &q);
scanf("%s", s);
scanf("%s", t);
for (int i = 0; i <= n - m; ++i) {
int j = 0;
for (; j < m && s[i + j] == t[j]; ++j)
;
if (j == m) pp[i] = 1;
}
for (int i = 1; i <= n - m; ++i) pp[i] += pp[i - 1];
for (int i = 0; i < q; ++i) {
scanf("%d %d", &a0, &a1);
a0--;
a1--;
if (a0 + m - 1 <= a1) {
if (a0 > 0)
printf("%d\n", pp[a1 - m + 1] - pp[a0 - 1]);
else
printf("%d\n", pp[a1 - m + 1]);
} else
printf("0\n");
}
return 0;
}
| ### Prompt
Create a solution in CPP for the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
int n, m, q, a0, a1;
char s[1003], t[1003];
int pp[1003];
int main() {
scanf("%d %d %d", &n, &m, &q);
scanf("%s", s);
scanf("%s", t);
for (int i = 0; i <= n - m; ++i) {
int j = 0;
for (; j < m && s[i + j] == t[j]; ++j)
;
if (j == m) pp[i] = 1;
}
for (int i = 1; i <= n - m; ++i) pp[i] += pp[i - 1];
for (int i = 0; i < q; ++i) {
scanf("%d %d", &a0, &a1);
a0--;
a1--;
if (a0 + m - 1 <= a1) {
if (a0 > 0)
printf("%d\n", pp[a1 - m + 1] - pp[a0 - 1]);
else
printf("%d\n", pp[a1 - m + 1]);
} else
printf("0\n");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int z[1000005];
string s;
void Z() {
int n = s.size(), l = 0, r = 0, i;
for (i = 0; i < n; i++) {
if (i > r) {
l = r = i;
while (r < n && s[r] == s[r - l]) {
r++;
}
z[i] = r - l;
r--;
} else {
if (i + z[i - l] <= r) {
z[i] = z[i - l];
} else {
l = i;
while (r < n && s[r] == s[r - l]) {
r++;
}
z[i] = r - l;
r--;
}
}
}
}
int main() {
int n, m, q;
scanf("%d %d %d", &n, &m, &q);
string d, t;
cin >> d >> t;
s = t + '$' + d;
Z();
vector<int> a;
for (int i = 0; i < t.size() + d.size() + 1; i++) {
if (z[i] >= t.size()) {
a.push_back(i - t.size() - 1);
}
}
int i, l, r, u, v, mi;
for (i = 0; i < q; i++) {
scanf("%d %d", &u, &v);
u--;
v--;
l = 0, r = a.size() - 1;
if (r == -1) {
printf("0\n");
continue;
}
while (l < r) {
mi = (l + r) >> 1;
if (a[mi] >= u) {
r = mi;
} else {
l = mi + 1;
}
}
if ((a[r] < u) || (a[r] > v) || (a[r] + t.size() - 1 > v)) {
printf("0\n");
continue;
}
int idxst = r;
l = 0, r = a.size() - 1;
while (l < r) {
mi = (l + r + 1) >> 1;
if (a[mi] + t.size() - 1 <= v) {
l = mi;
} else {
r = mi - 1;
}
}
if ((a[l] < u) || (a[l] > v)) {
printf("0\n");
continue;
}
int ans = l - idxst + 1;
printf("%d\n", ans);
}
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int z[1000005];
string s;
void Z() {
int n = s.size(), l = 0, r = 0, i;
for (i = 0; i < n; i++) {
if (i > r) {
l = r = i;
while (r < n && s[r] == s[r - l]) {
r++;
}
z[i] = r - l;
r--;
} else {
if (i + z[i - l] <= r) {
z[i] = z[i - l];
} else {
l = i;
while (r < n && s[r] == s[r - l]) {
r++;
}
z[i] = r - l;
r--;
}
}
}
}
int main() {
int n, m, q;
scanf("%d %d %d", &n, &m, &q);
string d, t;
cin >> d >> t;
s = t + '$' + d;
Z();
vector<int> a;
for (int i = 0; i < t.size() + d.size() + 1; i++) {
if (z[i] >= t.size()) {
a.push_back(i - t.size() - 1);
}
}
int i, l, r, u, v, mi;
for (i = 0; i < q; i++) {
scanf("%d %d", &u, &v);
u--;
v--;
l = 0, r = a.size() - 1;
if (r == -1) {
printf("0\n");
continue;
}
while (l < r) {
mi = (l + r) >> 1;
if (a[mi] >= u) {
r = mi;
} else {
l = mi + 1;
}
}
if ((a[r] < u) || (a[r] > v) || (a[r] + t.size() - 1 > v)) {
printf("0\n");
continue;
}
int idxst = r;
l = 0, r = a.size() - 1;
while (l < r) {
mi = (l + r + 1) >> 1;
if (a[mi] + t.size() - 1 <= v) {
l = mi;
} else {
r = mi - 1;
}
}
if ((a[l] < u) || (a[l] > v)) {
printf("0\n");
continue;
}
int ans = l - idxst + 1;
printf("%d\n", ans);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<int> l, r;
int main() {
string s, t;
int n, m, q, x, y;
cin >> n >> m >> q >> s >> t;
for (int i = 0; i < n; i++) {
int flag = 1;
if (s[i] == t[0]) {
for (int j = i, k = 0; j < n && k < m; j++, k++)
if (s[j] != t[k]) {
flag = 0;
break;
}
if (flag) {
l.push_back(i + 1);
r.push_back(i + m);
}
}
}
while (q--) {
cin >> x >> y;
int a, cnt = 0;
a = lower_bound(l.begin(), l.end(), x) - l.begin();
for (int i = a; i < l.size(); i++) {
if (l[i] >= x && r[i] <= y) cnt++;
if (r[i] > y) break;
}
cout << cnt << endl;
}
return 0;
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> l, r;
int main() {
string s, t;
int n, m, q, x, y;
cin >> n >> m >> q >> s >> t;
for (int i = 0; i < n; i++) {
int flag = 1;
if (s[i] == t[0]) {
for (int j = i, k = 0; j < n && k < m; j++, k++)
if (s[j] != t[k]) {
flag = 0;
break;
}
if (flag) {
l.push_back(i + 1);
r.push_back(i + m);
}
}
}
while (q--) {
cin >> x >> y;
int a, cnt = 0;
a = lower_bound(l.begin(), l.end(), x) - l.begin();
for (int i = a; i < l.size(); i++) {
if (l[i] >= x && r[i] <= y) cnt++;
if (r[i] > y) break;
}
cout << cnt << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int64_t MOD = 1e9 + 7;
const int64_t N = 2e6;
bool isQuery = false;
int64_t power(int64_t x, int64_t n) {
if (n == 0)
return 1;
else if (n % 2 == 0)
return power(x, n / 2) * power(x, n / 2);
else
return x * power(x, n / 2) * power(x, n / 2);
}
int64_t powerMod(int64_t x, int64_t n, int64_t mod) {
if (n == 0)
return 1;
else if (n % 2 == 0)
return (powerMod(x, n / 2, mod) * powerMod(x, n / 2, mod)) % mod;
else
return (x * powerMod(x, n / 2, mod) * powerMod(x, n / 2, mod)) % mod;
}
void solve() {
int64_t n, m, q;
cin >> n >> m >> q;
string s, t;
cin >> s >> t;
vector<int64_t> dp(n, 0), pref(n, -1), suff(n, -1), id;
int64_t ans = 0, p = -1;
for (int64_t i = 0, j; i < n; i++) {
for (j = 0; j < m && i + j < n; j++) {
if (s[i + j] != t[j]) break;
}
if (j == m)
pref[i + m - 1] = i + m - 1, id.push_back(i + m - 1), dp[i + m - 1] = 1;
}
for (int64_t i = 1; i < n; i++)
if (pref[i] == -1) pref[i] = pref[i - 1];
for (int64_t i = n - 1, j; i >= 0; i--) {
for (j = m - 1; j >= 0 && i - j >= 0; j--) {
if (s[i - (m - 1 - j)] != t[j]) break;
}
if (j == -1) suff[i - m + 1] = i;
}
for (int64_t i = n - 2; i >= 0; i--)
if (suff[i] == -1) suff[i] = suff[i + 1];
for (int64_t i = 1; i < n; i++) dp[i] += dp[i - 1];
while (q--) {
int64_t l, r;
cin >> l >> r;
l--, r--;
if (pref[r] == -1 || suff[l] == -1 | pref[r] < suff[l])
cout << 0;
else if (suff[l] > 0)
cout << dp[pref[r]] - dp[suff[l] - 1];
else
cout << dp[pref[r]];
cout << endl;
}
}
signed main() {
int64_t t = 1;
if (isQuery) cin >> t;
while (t--) {
solve();
cout << endl;
}
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int64_t MOD = 1e9 + 7;
const int64_t N = 2e6;
bool isQuery = false;
int64_t power(int64_t x, int64_t n) {
if (n == 0)
return 1;
else if (n % 2 == 0)
return power(x, n / 2) * power(x, n / 2);
else
return x * power(x, n / 2) * power(x, n / 2);
}
int64_t powerMod(int64_t x, int64_t n, int64_t mod) {
if (n == 0)
return 1;
else if (n % 2 == 0)
return (powerMod(x, n / 2, mod) * powerMod(x, n / 2, mod)) % mod;
else
return (x * powerMod(x, n / 2, mod) * powerMod(x, n / 2, mod)) % mod;
}
void solve() {
int64_t n, m, q;
cin >> n >> m >> q;
string s, t;
cin >> s >> t;
vector<int64_t> dp(n, 0), pref(n, -1), suff(n, -1), id;
int64_t ans = 0, p = -1;
for (int64_t i = 0, j; i < n; i++) {
for (j = 0; j < m && i + j < n; j++) {
if (s[i + j] != t[j]) break;
}
if (j == m)
pref[i + m - 1] = i + m - 1, id.push_back(i + m - 1), dp[i + m - 1] = 1;
}
for (int64_t i = 1; i < n; i++)
if (pref[i] == -1) pref[i] = pref[i - 1];
for (int64_t i = n - 1, j; i >= 0; i--) {
for (j = m - 1; j >= 0 && i - j >= 0; j--) {
if (s[i - (m - 1 - j)] != t[j]) break;
}
if (j == -1) suff[i - m + 1] = i;
}
for (int64_t i = n - 2; i >= 0; i--)
if (suff[i] == -1) suff[i] = suff[i + 1];
for (int64_t i = 1; i < n; i++) dp[i] += dp[i - 1];
while (q--) {
int64_t l, r;
cin >> l >> r;
l--, r--;
if (pref[r] == -1 || suff[l] == -1 | pref[r] < suff[l])
cout << 0;
else if (suff[l] > 0)
cout << dp[pref[r]] - dp[suff[l] - 1];
else
cout << dp[pref[r]];
cout << endl;
}
}
signed main() {
int64_t t = 1;
if (isQuery) cin >> t;
while (t--) {
solve();
cout << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, m, i, q, j, k, l, count, c, p = 0, a[1001] = {0};
cin >> n >> m >> q;
string s, t;
cin >> s >> t;
for (i = 0; i <= n - m; i++) {
if (s.substr(i, m) == t) a[i] = {1};
}
for (i = 1; i <= q; i++) {
long long d, e;
c = 0;
cin >> d >> e;
for (j = d - 1; j <= e - m; j++) {
if (a[j]) c++;
}
cout << c << "\n";
}
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, m, i, q, j, k, l, count, c, p = 0, a[1001] = {0};
cin >> n >> m >> q;
string s, t;
cin >> s >> t;
for (i = 0; i <= n - m; i++) {
if (s.substr(i, m) == t) a[i] = {1};
}
for (i = 1; i <= q; i++) {
long long d, e;
c = 0;
cin >> d >> e;
for (j = d - 1; j <= e - m; j++) {
if (a[j]) c++;
}
cout << c << "\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, q;
string s;
string t;
int d[1001];
bool check(string s) {
for (int i = 0; i < s.length(); i++) {
if (s[i] != t[i]) return false;
}
return true;
}
int main() {
cin >> n >> m >> q;
cin >> s >> t;
for (int i = 0; i < n - m + 1; i++) {
string tmp = "";
for (int j = i; j < i + m; j++) {
tmp += s[j];
}
if (check(tmp)) {
d[i + m]++;
}
}
for (int i = 1; i <= n; i++) {
d[i] += d[i - 1];
}
while (q--) {
int l, r;
scanf("%d%d", &l, &r);
int len = r - l + 1;
if (len < t.length()) {
cout << 0 << "\n";
continue;
}
cout << d[r] - d[l + m - 2] << "\n";
}
return 0;
}
| ### Prompt
Please create a solution in Cpp to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, q;
string s;
string t;
int d[1001];
bool check(string s) {
for (int i = 0; i < s.length(); i++) {
if (s[i] != t[i]) return false;
}
return true;
}
int main() {
cin >> n >> m >> q;
cin >> s >> t;
for (int i = 0; i < n - m + 1; i++) {
string tmp = "";
for (int j = i; j < i + m; j++) {
tmp += s[j];
}
if (check(tmp)) {
d[i + m]++;
}
}
for (int i = 1; i <= n; i++) {
d[i] += d[i - 1];
}
while (q--) {
int l, r;
scanf("%d%d", &l, &r);
int len = r - l + 1;
if (len < t.length()) {
cout << 0 << "\n";
continue;
}
cout << d[r] - d[l + m - 2] << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int a[1001];
int main() {
int i, n, m, q, j, l, r;
string str1, str2;
cin >> n >> m >> q;
cin >> str1;
cin >> str2;
for (i = 0; i <= n - m; i++) {
for (j = 0; j < m; j++) {
if (str1[i + j] != str2[j]) {
break;
}
}
if (j == m) a[i + 1] = 1;
}
for (i = 2; i <= n; i++) {
a[i] = a[i] + a[i - 1];
}
for (i = 1; i <= q; i++) {
cin >> l >> r;
if (r - l + 1 < m) {
cout << 0 << endl;
continue;
}
if (l == 1) {
cout << a[r - m + 1] << endl;
;
} else {
cout << a[r - m + 1] - a[l - 1] << endl;
}
}
}
| ### Prompt
Create a solution in Cpp for the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int a[1001];
int main() {
int i, n, m, q, j, l, r;
string str1, str2;
cin >> n >> m >> q;
cin >> str1;
cin >> str2;
for (i = 0; i <= n - m; i++) {
for (j = 0; j < m; j++) {
if (str1[i + j] != str2[j]) {
break;
}
}
if (j == m) a[i + 1] = 1;
}
for (i = 2; i <= n; i++) {
a[i] = a[i] + a[i - 1];
}
for (i = 1; i <= q; i++) {
cin >> l >> r;
if (r - l + 1 < m) {
cout << 0 << endl;
continue;
}
if (l == 1) {
cout << a[r - m + 1] << endl;
;
} else {
cout << a[r - m + 1] - a[l - 1] << endl;
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10005;
int Next[maxn];
char str[maxn], th[maxn];
int change(char s[]) {
int i = 0, j = 1;
Next[i] = 0;
while (s[j]) {
if (s[i] == s[j]) {
i++;
Next[j] = i;
j++;
} else {
if (i > 0) {
i = Next[i - 1];
} else {
Next[j] = i;
j++;
}
}
}
for (int i = j - 1; i > 0; i--) {
Next[i] = Next[i - 1];
}
Next[0] = -1;
return j;
}
int num[maxn] = {0};
int main() {
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
scanf("%s%s", str, th);
int l = change(th);
int i = 0, j = 0, cnt = 0;
while (str[i]) {
if (str[i] == th[j]) {
if (j == l - 1) {
num[i - m + 1]++;
cnt++;
j = Next[j];
} else {
i++;
j++;
}
} else {
if (j >= 0) {
j = Next[j];
} else {
i++;
j = 0;
}
}
}
while (k--) {
int l, r;
scanf("%d%d", &l, &r);
l--;
r--;
int sum = 0;
for (int i = l; i <= r; i++) {
if ((r - i + 1) >= m) sum += num[i];
}
printf("%d\n", sum);
}
return 0;
}
| ### Prompt
Create a solution in CPP for the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10005;
int Next[maxn];
char str[maxn], th[maxn];
int change(char s[]) {
int i = 0, j = 1;
Next[i] = 0;
while (s[j]) {
if (s[i] == s[j]) {
i++;
Next[j] = i;
j++;
} else {
if (i > 0) {
i = Next[i - 1];
} else {
Next[j] = i;
j++;
}
}
}
for (int i = j - 1; i > 0; i--) {
Next[i] = Next[i - 1];
}
Next[0] = -1;
return j;
}
int num[maxn] = {0};
int main() {
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
scanf("%s%s", str, th);
int l = change(th);
int i = 0, j = 0, cnt = 0;
while (str[i]) {
if (str[i] == th[j]) {
if (j == l - 1) {
num[i - m + 1]++;
cnt++;
j = Next[j];
} else {
i++;
j++;
}
} else {
if (j >= 0) {
j = Next[j];
} else {
i++;
j = 0;
}
}
}
while (k--) {
int l, r;
scanf("%d%d", &l, &r);
l--;
r--;
int sum = 0;
for (int i = l; i <= r; i++) {
if ((r - i + 1) >= m) sum += num[i];
}
printf("%d\n", sum);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, q;
string s, t;
cin >> n >> m >> q;
cin >> s >> t;
vector<int> v;
v.clear();
if (n < m) {
for (int i = 0; i < q; i++) {
int a, b;
cin >> a >> b;
cout << "0" << endl;
}
return 0;
}
for (int i = 0; i <= s.size() - t.size(); i++) {
int j;
int val = i;
for (j = 0; j < t.size(); j++) {
if (t[j] == s[val++])
continue;
else
break;
}
if (j == t.size()) v.push_back(i);
}
if (v.size() == 0) {
for (int i = 0; i < q; i++) {
int a, b;
cin >> a >> b;
cout << "0" << endl;
}
return 0;
}
for (int i = 0; i < q; i++) {
int a, b;
cin >> a >> b;
a = a - 1;
b = b - 1;
int count = 0;
for (int p = 0; p < v.size(); p++) {
if (v[p] >= a && v[p] + m - 1 <= b) count++;
}
cout << count << endl;
}
return 0;
}
| ### Prompt
In cpp, your task is to solve the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, q;
string s, t;
cin >> n >> m >> q;
cin >> s >> t;
vector<int> v;
v.clear();
if (n < m) {
for (int i = 0; i < q; i++) {
int a, b;
cin >> a >> b;
cout << "0" << endl;
}
return 0;
}
for (int i = 0; i <= s.size() - t.size(); i++) {
int j;
int val = i;
for (j = 0; j < t.size(); j++) {
if (t[j] == s[val++])
continue;
else
break;
}
if (j == t.size()) v.push_back(i);
}
if (v.size() == 0) {
for (int i = 0; i < q; i++) {
int a, b;
cin >> a >> b;
cout << "0" << endl;
}
return 0;
}
for (int i = 0; i < q; i++) {
int a, b;
cin >> a >> b;
a = a - 1;
b = b - 1;
int count = 0;
for (int p = 0; p < v.size(); p++) {
if (v[p] >= a && v[p] + m - 1 <= b) count++;
}
cout << count << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, q, sum[1001];
string s1, s2;
int main() {
cin >> n >> m >> q >> s1 >> s2;
for (int i = 1; i <= n; i += 1) {
sum[i] += sum[i - 1];
if (i > n - m + 1) {
continue;
}
bool f = true;
for (int j = i; j < i + m; j += 1) {
if (s1[j - 1] != s2[j - i]) {
f = false;
break;
}
}
if (f) {
sum[i + m - 1] += 1;
}
}
for (int i = 0; i < q; i += 1) {
int a, b;
cin >> a >> b;
if (a + m - 2 > b) {
cout << 0 << endl;
continue;
}
cout << sum[b] - sum[a + m - 2] << endl;
}
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, q, sum[1001];
string s1, s2;
int main() {
cin >> n >> m >> q >> s1 >> s2;
for (int i = 1; i <= n; i += 1) {
sum[i] += sum[i - 1];
if (i > n - m + 1) {
continue;
}
bool f = true;
for (int j = i; j < i + m; j += 1) {
if (s1[j - 1] != s2[j - i]) {
f = false;
break;
}
}
if (f) {
sum[i + m - 1] += 1;
}
}
for (int i = 0; i < q; i += 1) {
int a, b;
cin >> a >> b;
if (a + m - 2 > b) {
cout << 0 << endl;
continue;
}
cout << sum[b] - sum[a + m - 2] << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, q;
char s[1005], t[1005];
int sum[1005];
int main() {
scanf("%d%d%d", &n, &m, &q);
scanf(" %s %s", s + 1, t + 1);
for (int i = 1; i <= n; i++) {
int good = 1;
for (int j = 1; j <= m; j++) {
if (s[i + j - 1] != t[j]) good = 0;
}
sum[i] = sum[i - 1] + good;
}
while (q--) {
int l, r;
scanf("%d%d", &l, &r);
r = max(l - 1, r - m + 1);
printf("%d\n", sum[r] - sum[l - 1]);
}
}
| ### Prompt
Generate a CPP solution to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, q;
char s[1005], t[1005];
int sum[1005];
int main() {
scanf("%d%d%d", &n, &m, &q);
scanf(" %s %s", s + 1, t + 1);
for (int i = 1; i <= n; i++) {
int good = 1;
for (int j = 1; j <= m; j++) {
if (s[i + j - 1] != t[j]) good = 0;
}
sum[i] = sum[i - 1] + good;
}
while (q--) {
int l, r;
scanf("%d%d", &l, &r);
r = max(l - 1, r - m + 1);
printf("%d\n", sum[r] - sum[l - 1]);
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int dp[2000];
char str[2000], tr[2000];
int main() {
int i, j, k, l, m, n, q;
scanf("%d%d%d", &n, &m, &q);
scanf("%s", str + 1);
scanf("%s", tr + 1);
for (int i = 1; str[i]; i++) {
for (j = 1, k = i; tr[j] && str[k]; j++, k++) {
if (tr[j] != str[k]) break;
}
dp[i] += dp[i - 1];
if (j <= m) continue;
dp[i]++;
}
while (q--) {
scanf("%d%d", &l, &k);
if ((k - l + 1) < m) {
printf("0\n");
continue;
}
int v = k - m + 1;
int ans = dp[v] - dp[l - 1];
cout << ans << endl;
}
}
| ### Prompt
Create a solution in Cpp for the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int dp[2000];
char str[2000], tr[2000];
int main() {
int i, j, k, l, m, n, q;
scanf("%d%d%d", &n, &m, &q);
scanf("%s", str + 1);
scanf("%s", tr + 1);
for (int i = 1; str[i]; i++) {
for (j = 1, k = i; tr[j] && str[k]; j++, k++) {
if (tr[j] != str[k]) break;
}
dp[i] += dp[i - 1];
if (j <= m) continue;
dp[i]++;
}
while (q--) {
scanf("%d%d", &l, &k);
if ((k - l + 1) < m) {
printf("0\n");
continue;
}
int v = k - m + 1;
int ans = dp[v] - dp[l - 1];
cout << ans << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1001;
int a[maxn], cnt = 0;
char s1[maxn], s2[maxn];
int main() {
int n, m, q;
scanf("%d%d%d", &n, &m, &q);
cin >> s1 >> s2;
for (int i = 0; i < n; i++) {
if (s1[i] == s2[0]) {
bool fa = 1;
int now = i;
for (int j = 1; j < m; j++) {
now++;
if (s2[j] != s1[now]) {
fa = 0;
break;
}
}
if (fa == 1) {
a[cnt++] = i + 1;
}
}
}
while (q--) {
int u, v;
scanf("%d%d", &u, &v);
int sum = 0;
for (int i = 0; i < cnt; i++) {
if (u <= a[i] && v >= a[i] + m - 1) sum++;
}
printf("%d\n", sum);
}
return 0;
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1001;
int a[maxn], cnt = 0;
char s1[maxn], s2[maxn];
int main() {
int n, m, q;
scanf("%d%d%d", &n, &m, &q);
cin >> s1 >> s2;
for (int i = 0; i < n; i++) {
if (s1[i] == s2[0]) {
bool fa = 1;
int now = i;
for (int j = 1; j < m; j++) {
now++;
if (s2[j] != s1[now]) {
fa = 0;
break;
}
}
if (fa == 1) {
a[cnt++] = i + 1;
}
}
}
while (q--) {
int u, v;
scanf("%d%d", &u, &v);
int sum = 0;
for (int i = 0; i < cnt; i++) {
if (u <= a[i] && v >= a[i] + m - 1) sum++;
}
printf("%d\n", sum);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
long long z[3000019], arr[3000019], pre[3000019];
void fnc(string s) {
memset(z, 0, sizeof(z));
long long l = 0, r = 0;
long long n = s.size();
for (long long i = 1; i <= n; i++) {
if (i <= r) z[i] = min(r - i + 1, z[i - l]);
while (i + z[i] <= n && s[z[i]] == s[i + z[i]]) z[i]++;
if (i + z[i] - 1 > r) {
l = i;
r = i + z[i] - 1;
}
}
z[0] = n;
return;
}
signed main() {
std::ios::sync_with_stdio(false);
long long n, m, q, i, l, r;
string s, t, str;
cin >> n >> m >> q;
cin >> s >> t;
str = t + "$" + s;
fnc(str);
for (i = m + 1; i <= n + m; i++) arr[i - m] = (z[i] == m);
for (i = 1; i <= n; i++) pre[i] = arr[i] + pre[i - 1];
while (q--) {
cin >> l >> r;
if (r - l + 1 < m) {
cout << 0 << endl;
continue;
}
cout << pre[r - m + 1] - pre[l - 1] << endl;
}
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
long long z[3000019], arr[3000019], pre[3000019];
void fnc(string s) {
memset(z, 0, sizeof(z));
long long l = 0, r = 0;
long long n = s.size();
for (long long i = 1; i <= n; i++) {
if (i <= r) z[i] = min(r - i + 1, z[i - l]);
while (i + z[i] <= n && s[z[i]] == s[i + z[i]]) z[i]++;
if (i + z[i] - 1 > r) {
l = i;
r = i + z[i] - 1;
}
}
z[0] = n;
return;
}
signed main() {
std::ios::sync_with_stdio(false);
long long n, m, q, i, l, r;
string s, t, str;
cin >> n >> m >> q;
cin >> s >> t;
str = t + "$" + s;
fnc(str);
for (i = m + 1; i <= n + m; i++) arr[i - m] = (z[i] == m);
for (i = 1; i <= n; i++) pre[i] = arr[i] + pre[i - 1];
while (q--) {
cin >> l >> r;
if (r - l + 1 < m) {
cout << 0 << endl;
continue;
}
cout << pre[r - m + 1] - pre[l - 1] << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main(int argc, const char* argv[]) {
int64_t n, m, q;
string s, t;
cin >> n >> m >> q >> s >> t;
vector<pair<int, int>> ins;
for (int i = 0; i < s.size(); ++i) {
if (i + t.size() - 1 < s.size() && s.substr(i, t.size()) == t) {
ins.push_back(make_pair(i, i + t.size() - 1));
}
}
for (int i = 0; i < q; ++i) {
int64_t l, r, cnt = 0;
cin >> l >> r;
--l;
--r;
for (auto now : ins) {
if (now.first >= l && now.second <= r) {
++cnt;
}
}
cout << cnt << '\n';
}
return 0;
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main(int argc, const char* argv[]) {
int64_t n, m, q;
string s, t;
cin >> n >> m >> q >> s >> t;
vector<pair<int, int>> ins;
for (int i = 0; i < s.size(); ++i) {
if (i + t.size() - 1 < s.size() && s.substr(i, t.size()) == t) {
ins.push_back(make_pair(i, i + t.size() - 1));
}
}
for (int i = 0; i < q; ++i) {
int64_t l, r, cnt = 0;
cin >> l >> r;
--l;
--r;
for (auto now : ins) {
if (now.first >= l && now.second <= r) {
++cnt;
}
}
cout << cnt << '\n';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, q;
cin >> n >> m >> q;
string s, t;
cin >> s >> t;
vector<int> I;
for (int i = 0; i < n - m + 1; i++) {
if (t == s.substr(i, m)) I.push_back(i);
}
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
l--;
r--;
int ans = 0;
for (int j = 0; j < I.size(); j++) {
if (I[j] < l)
continue;
else if (I[j] + m - 1 > r)
break;
else
ans++;
}
cout << ans << endl;
}
return 0;
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, q;
cin >> n >> m >> q;
string s, t;
cin >> s >> t;
vector<int> I;
for (int i = 0; i < n - m + 1; i++) {
if (t == s.substr(i, m)) I.push_back(i);
}
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
l--;
r--;
int ans = 0;
for (int j = 0; j < I.size(); j++) {
if (I[j] < l)
continue;
else if (I[j] + m - 1 > r)
break;
else
ans++;
}
cout << ans << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, m, q;
cin >> n >> m >> q;
string s, t;
cin >> s >> t;
vector<int> v;
for (int i = 0; i < int((s).size()); i++) {
int j;
for (j = 0; j < int((t).size()) && (i + j) < int((s).size()); j++) {
if (s[j + i] != t[j]) break;
}
if (j == t.size()) v.push_back(i);
}
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
l--;
r--;
r = r - (int)t.size() + 1;
if (l > r) {
cout << 0 << endl;
continue;
}
int ind1 = lower_bound((v).begin(), (v).end(), l) - v.begin();
int ind2 = upper_bound((v).begin(), (v).end(), r) - v.begin();
cout << (ind2 - ind1) << endl;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int kickstart = 0;
int test = 1;
for (int i = 0; i < test; i++) {
if (kickstart)
cout << "Case #" << i + 1 << ": ", solve(), cout << endl;
else
solve();
}
return 0;
}
| ### Prompt
Please formulate a cpp solution to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, m, q;
cin >> n >> m >> q;
string s, t;
cin >> s >> t;
vector<int> v;
for (int i = 0; i < int((s).size()); i++) {
int j;
for (j = 0; j < int((t).size()) && (i + j) < int((s).size()); j++) {
if (s[j + i] != t[j]) break;
}
if (j == t.size()) v.push_back(i);
}
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
l--;
r--;
r = r - (int)t.size() + 1;
if (l > r) {
cout << 0 << endl;
continue;
}
int ind1 = lower_bound((v).begin(), (v).end(), l) - v.begin();
int ind2 = upper_bound((v).begin(), (v).end(), r) - v.begin();
cout << (ind2 - ind1) << endl;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int kickstart = 0;
int test = 1;
for (int i = 0; i < test; i++) {
if (kickstart)
cout << "Case #" << i + 1 << ": ", solve(), cout << endl;
else
solve();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, q, dp[1010][1010];
char s[1010], t[1010];
bool used[1010];
int main() {
scanf("%d%d%d", &n, &m, &q);
scanf("%s", s + 1);
scanf("%s", t);
for (int i = 1; i + m - 1 <= n; i++) {
bool f = true;
for (int j = 0; j < m; j++) {
if (s[i + j] != t[j]) {
f = false;
break;
}
}
used[i] = f;
}
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j++) {
dp[i][j] = max(dp[i][j], dp[i][j - 1]);
if (j - m + 1 >= i && used[j - m + 1]) {
dp[i][j]++;
}
}
}
while (q--) {
int l, r;
scanf("%d%d", &l, &r);
printf("%d\n", dp[l][r]);
}
return 0;
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, q, dp[1010][1010];
char s[1010], t[1010];
bool used[1010];
int main() {
scanf("%d%d%d", &n, &m, &q);
scanf("%s", s + 1);
scanf("%s", t);
for (int i = 1; i + m - 1 <= n; i++) {
bool f = true;
for (int j = 0; j < m; j++) {
if (s[i + j] != t[j]) {
f = false;
break;
}
}
used[i] = f;
}
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j++) {
dp[i][j] = max(dp[i][j], dp[i][j - 1]);
if (j - m + 1 >= i && used[j - m + 1]) {
dp[i][j]++;
}
}
}
while (q--) {
int l, r;
scanf("%d%d", &l, &r);
printf("%d\n", dp[l][r]);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, q;
cin >> n >> m >> q;
string s, t;
cin >> s >> t;
vector<bool> occ(n);
for (int i = 0; i < n; i++) {
if (s.substr(i, m) == t) {
occ[i] = 1;
}
}
vector<int> prefixOcc(n + 1);
prefixOcc[0] = 0;
for (int i = 1; i <= n; i++) {
prefixOcc[i] = prefixOcc[i - 1] + occ[i - 1];
}
while (q--) {
int left, right;
cin >> left >> right;
left--;
right--;
if (right - left + 1 < m or right - m + 1 < 0) {
cout << '0' << endl;
;
continue;
}
int prefLeft = prefixOcc[left];
int prefRight = prefixOcc[right - m + 2];
cout << prefRight - prefLeft << endl;
}
}
| ### Prompt
Generate a Cpp solution to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, q;
cin >> n >> m >> q;
string s, t;
cin >> s >> t;
vector<bool> occ(n);
for (int i = 0; i < n; i++) {
if (s.substr(i, m) == t) {
occ[i] = 1;
}
}
vector<int> prefixOcc(n + 1);
prefixOcc[0] = 0;
for (int i = 1; i <= n; i++) {
prefixOcc[i] = prefixOcc[i - 1] + occ[i - 1];
}
while (q--) {
int left, right;
cin >> left >> right;
left--;
right--;
if (right - left + 1 < m or right - m + 1 < 0) {
cout << '0' << endl;
;
continue;
}
int prefLeft = prefixOcc[left];
int prefRight = prefixOcc[right - m + 2];
cout << prefRight - prefLeft << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
string s, t;
long long m, n, q, dp[1010][1010];
int main() {
cin >> n >> m >> q;
cin >> s >> t;
for (int i = 0; i <= n - m; i++) {
bool b = false;
for (int j = i; j < i + m; j++) {
if (s[j] != t[j - i]) b = true;
}
if (b == false) {
dp[i][i + m] = 1;
}
}
for (int i = 0; i <= n; i++) {
for (int j = 0; j < n; j++) {
if (i >= m) {
dp[j][j + i] = dp[j][j + i] + dp[j][j + i - 1] + dp[j + 1][j + i] -
dp[j + 1][j + i - 1];
}
}
}
for (int i = 0, a, b; i < q; i++) {
cin >> a >> b;
cout << dp[a - 1][b] << endl;
}
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
string s, t;
long long m, n, q, dp[1010][1010];
int main() {
cin >> n >> m >> q;
cin >> s >> t;
for (int i = 0; i <= n - m; i++) {
bool b = false;
for (int j = i; j < i + m; j++) {
if (s[j] != t[j - i]) b = true;
}
if (b == false) {
dp[i][i + m] = 1;
}
}
for (int i = 0; i <= n; i++) {
for (int j = 0; j < n; j++) {
if (i >= m) {
dp[j][j + i] = dp[j][j + i] + dp[j][j + i - 1] + dp[j + 1][j + i] -
dp[j + 1][j + i - 1];
}
}
}
for (int i = 0, a, b; i < q; i++) {
cin >> a >> b;
cout << dp[a - 1][b] << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long MOD = 1e9 + 7;
vector<long long> build(string s) {
int n = s.size();
vector<long long> kmp(n, 0);
for (int i = 1; i < n; i++) {
int j = kmp[i - 1];
while (j > 0 && s[i] != s[j]) {
j = kmp[j - 1];
}
if (s[i] == s[j]) j++;
kmp[i] = j;
}
return kmp;
}
void icchhipadey() {
long long n, m, q;
cin >> n >> m >> q;
string s1, s2;
cin >> s1 >> s2;
while (q--) {
long long l, r;
cin >> l >> r;
string s = s1.substr(l - 1, r - l + 1);
long long ind = 0;
long long cnt = 0;
string temp = s2 + "$" + s;
vector<long long> kmp = build(temp);
for (auto x : kmp) {
if (x == m) {
cnt++;
}
}
cout << cnt << endl;
}
}
int main() { icchhipadey(); }
| ### Prompt
Create a solution in cpp for the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long MOD = 1e9 + 7;
vector<long long> build(string s) {
int n = s.size();
vector<long long> kmp(n, 0);
for (int i = 1; i < n; i++) {
int j = kmp[i - 1];
while (j > 0 && s[i] != s[j]) {
j = kmp[j - 1];
}
if (s[i] == s[j]) j++;
kmp[i] = j;
}
return kmp;
}
void icchhipadey() {
long long n, m, q;
cin >> n >> m >> q;
string s1, s2;
cin >> s1 >> s2;
while (q--) {
long long l, r;
cin >> l >> r;
string s = s1.substr(l - 1, r - l + 1);
long long ind = 0;
long long cnt = 0;
string temp = s2 + "$" + s;
vector<long long> kmp = build(temp);
for (auto x : kmp) {
if (x == m) {
cnt++;
}
}
cout << cnt << endl;
}
}
int main() { icchhipadey(); }
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 20;
int sum[maxn];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m, q;
cin >> n >> m >> q;
string s, t;
cin >> s >> t;
for (int i = 0; i < n; i++)
sum[i + 1] = sum[i] + (i + m <= n && s.substr(i, m) == t);
while (q--) {
int l, r;
cin >> l >> r;
r -= m;
l--;
if (r < l)
cout << 0 << endl;
else
cout << sum[r + 1] - sum[l] << endl;
}
}
| ### Prompt
Create a solution in Cpp for the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 20;
int sum[maxn];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m, q;
cin >> n >> m >> q;
string s, t;
cin >> s >> t;
for (int i = 0; i < n; i++)
sum[i + 1] = sum[i] + (i + m <= n && s.substr(i, m) == t);
while (q--) {
int l, r;
cin >> l >> r;
r -= m;
l--;
if (r < l)
cout << 0 << endl;
else
cout << sum[r + 1] - sum[l] << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, m, q;
cin >> n >> m >> q;
string s;
cin >> s;
string t;
cin >> t;
string str1 = s;
size_t pos1;
vector<long long> arr;
pos1 = str1.find(t);
while (pos1 != string::npos) {
arr.push_back(pos1);
pos1 = str1.find(t, pos1 + 1);
}
while (q--) {
long long l, r;
cin >> l >> r;
long long count = 0;
for (long long i = 0; i < arr.size(); i++) {
if (arr[i] >= (l - 1) && (r - 1) >= (arr[i] + m - 1)) {
count++;
}
}
cout << count << endl;
}
return 0;
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, m, q;
cin >> n >> m >> q;
string s;
cin >> s;
string t;
cin >> t;
string str1 = s;
size_t pos1;
vector<long long> arr;
pos1 = str1.find(t);
while (pos1 != string::npos) {
arr.push_back(pos1);
pos1 = str1.find(t, pos1 + 1);
}
while (q--) {
long long l, r;
cin >> l >> r;
long long count = 0;
for (long long i = 0; i < arr.size(); i++) {
if (arr[i] >= (l - 1) && (r - 1) >= (arr[i] + m - 1)) {
count++;
}
}
cout << count << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int64_t MOD = 1e9 + 7;
const int64_t N = 2e6;
bool isQuery = false;
int64_t power(int64_t x, int64_t n) {
if (n == 0)
return 1;
else if (n % 2 == 0)
return power(x, n / 2) * power(x, n / 2);
else
return x * power(x, n / 2) * power(x, n / 2);
}
int64_t powerMod(int64_t x, int64_t n, int64_t mod) {
if (n == 0)
return 1;
else if (n % 2 == 0)
return (powerMod(x, n / 2, mod) * powerMod(x, n / 2, mod)) % mod;
else
return (x * powerMod(x, n / 2, mod) * powerMod(x, n / 2, mod)) % mod;
}
void solve() {
int64_t n, m, q;
cin >> n >> m >> q;
string s, t;
cin >> s >> t;
vector<int64_t> dp(n, 0), pref(n, -1), suff(n, -1), id;
int64_t ans = 0, p = -1;
for (int64_t i = 0, j; i < n; i++) {
for (j = 0; j < m && i + j < n; j++) {
if (s[i + j] != t[j]) break;
}
if (j == m)
pref[i + m - 1] = i + m - 1, id.push_back(i + m - 1), dp[i + m - 1] = 1;
}
for (int64_t i = 1; i < n; i++)
if (pref[i] == -1) pref[i] = pref[i - 1];
for (int64_t i = n - 1, j; i >= 0; i--) {
for (j = m - 1; j >= 0 && i - j >= 0; j--) {
if (s[i - (m - 1 - j)] != t[j]) break;
}
if (j == -1) suff[i - m + 1] = i;
}
for (int64_t i = n - 2; i >= 0; i--)
if (suff[i] == -1) suff[i] = suff[i + 1];
for (int64_t i = 1; i < n; i++) dp[i] += dp[i - 1];
while (q--) {
int64_t l, r;
cin >> l >> r;
l--, r--;
if (pref[r] == -1 || suff[l] == -1 | pref[r] < suff[l])
cout << 0;
else if (suff[l] > 0)
cout << dp[pref[r]] - dp[suff[l] - 1];
else
cout << dp[pref[r]];
cout << endl;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int64_t t = 1;
if (isQuery) cin >> t;
while (t--) {
solve();
cout << endl;
}
}
| ### Prompt
Generate a Cpp solution to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int64_t MOD = 1e9 + 7;
const int64_t N = 2e6;
bool isQuery = false;
int64_t power(int64_t x, int64_t n) {
if (n == 0)
return 1;
else if (n % 2 == 0)
return power(x, n / 2) * power(x, n / 2);
else
return x * power(x, n / 2) * power(x, n / 2);
}
int64_t powerMod(int64_t x, int64_t n, int64_t mod) {
if (n == 0)
return 1;
else if (n % 2 == 0)
return (powerMod(x, n / 2, mod) * powerMod(x, n / 2, mod)) % mod;
else
return (x * powerMod(x, n / 2, mod) * powerMod(x, n / 2, mod)) % mod;
}
void solve() {
int64_t n, m, q;
cin >> n >> m >> q;
string s, t;
cin >> s >> t;
vector<int64_t> dp(n, 0), pref(n, -1), suff(n, -1), id;
int64_t ans = 0, p = -1;
for (int64_t i = 0, j; i < n; i++) {
for (j = 0; j < m && i + j < n; j++) {
if (s[i + j] != t[j]) break;
}
if (j == m)
pref[i + m - 1] = i + m - 1, id.push_back(i + m - 1), dp[i + m - 1] = 1;
}
for (int64_t i = 1; i < n; i++)
if (pref[i] == -1) pref[i] = pref[i - 1];
for (int64_t i = n - 1, j; i >= 0; i--) {
for (j = m - 1; j >= 0 && i - j >= 0; j--) {
if (s[i - (m - 1 - j)] != t[j]) break;
}
if (j == -1) suff[i - m + 1] = i;
}
for (int64_t i = n - 2; i >= 0; i--)
if (suff[i] == -1) suff[i] = suff[i + 1];
for (int64_t i = 1; i < n; i++) dp[i] += dp[i - 1];
while (q--) {
int64_t l, r;
cin >> l >> r;
l--, r--;
if (pref[r] == -1 || suff[l] == -1 | pref[r] < suff[l])
cout << 0;
else if (suff[l] > 0)
cout << dp[pref[r]] - dp[suff[l] - 1];
else
cout << dp[pref[r]];
cout << endl;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int64_t t = 1;
if (isQuery) cin >> t;
while (t--) {
solve();
cout << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int st[1001];
int main() {
int n, m, q;
cin >> n >> m >> q;
string s;
cin >> s;
string t;
cin >> t;
st[0] = 0;
for (int i = 1; i <= n - m + 1; i++) {
bool b = true;
for (int j = i; j < i + m; j++) {
if (s.at(j - 1) != t.at(j - i)) {
b = false;
break;
}
}
if (b) {
st[i] = st[i - 1] + 1;
} else {
st[i] = st[i - 1];
}
}
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
if (r - l < m - 1) {
cout << 0 << endl;
} else {
cout << st[r - m + 1] - st[l - 1] << endl;
}
}
return 0;
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int st[1001];
int main() {
int n, m, q;
cin >> n >> m >> q;
string s;
cin >> s;
string t;
cin >> t;
st[0] = 0;
for (int i = 1; i <= n - m + 1; i++) {
bool b = true;
for (int j = i; j < i + m; j++) {
if (s.at(j - 1) != t.at(j - i)) {
b = false;
break;
}
}
if (b) {
st[i] = st[i - 1] + 1;
} else {
st[i] = st[i - 1];
}
}
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
if (r - l < m - 1) {
cout << 0 << endl;
} else {
cout << st[r - m + 1] - st[l - 1] << endl;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long const MOD = 1e9 + 7;
long long const N = 1e3 + 10;
long long ara[N + 1];
long long bra[N + 1];
int main() {
(ios_base::sync_with_stdio(false), cin.tie(NULL));
long long n, m, q;
cin >> n >> m >> q;
string str, s;
cin >> str >> s;
for (long long i = 0; i < n - m + 1; i++) {
long long j = 0, pre = i;
while (j < m) {
if (str[i] == s[j]) {
i++;
j++;
} else
break;
}
if (j == m) ara[pre + 1]++;
i = pre;
}
for (long long i = 1; i <= n + 10; i++) {
ara[i] += ara[i - 1];
}
while (q--) {
long long a, b;
cin >> a >> b;
b = b - m + 1;
cout << max(0ll, ara[max(b, 0ll)] - ara[a - 1]) << endl;
}
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long const MOD = 1e9 + 7;
long long const N = 1e3 + 10;
long long ara[N + 1];
long long bra[N + 1];
int main() {
(ios_base::sync_with_stdio(false), cin.tie(NULL));
long long n, m, q;
cin >> n >> m >> q;
string str, s;
cin >> str >> s;
for (long long i = 0; i < n - m + 1; i++) {
long long j = 0, pre = i;
while (j < m) {
if (str[i] == s[j]) {
i++;
j++;
} else
break;
}
if (j == m) ara[pre + 1]++;
i = pre;
}
for (long long i = 1; i <= n + 10; i++) {
ara[i] += ara[i - 1];
}
while (q--) {
long long a, b;
cin >> a >> b;
b = b - m + 1;
cout << max(0ll, ara[max(b, 0ll)] - ara[a - 1]) << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, q, lct1, lct2;
string s, t;
int z[3005];
int cnt;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m >> q;
cin >> s >> t;
s = t + s;
int len = s.length();
z[0] = 0;
for (int i = 1, l = 0, r = 0; i < len; ++i) {
if (i <= r) z[i] = min(z[i - l], r - i + 1);
while (i + z[i] < len && s[z[i]] == s[i + z[i]]) z[i]++;
if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1;
}
for (int i = 1; i <= q; ++i) {
cnt = 0;
cin >> lct1 >> lct2;
for (int i = lct1 + m - 1; i <= lct2 + m - 1; ++i)
if (z[i] >= m && i + m - 1 <= lct2 + m - 1) cnt++;
cout << cnt << "\n";
}
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, q, lct1, lct2;
string s, t;
int z[3005];
int cnt;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m >> q;
cin >> s >> t;
s = t + s;
int len = s.length();
z[0] = 0;
for (int i = 1, l = 0, r = 0; i < len; ++i) {
if (i <= r) z[i] = min(z[i - l], r - i + 1);
while (i + z[i] < len && s[z[i]] == s[i + z[i]]) z[i]++;
if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1;
}
for (int i = 1; i <= q; ++i) {
cnt = 0;
cin >> lct1 >> lct2;
for (int i = lct1 + m - 1; i <= lct2 + m - 1; ++i)
if (z[i] >= m && i + m - 1 <= lct2 + m - 1) cnt++;
cout << cnt << "\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
struct Node {
int d;
struct Node *left, *right;
};
int p[1000][1000] = {0};
string s, t;
void dp(int i, int j) {
if (j - i + 1 == t.size()) {
int f = 1;
for (int z = 0; z < t.size(); z++) {
if (s[i + z] != t[z]) {
f = 0;
break;
}
}
if (f == 1) p[i][j] = 1;
} else if (j - i + 1 < t.size())
return;
else if (j - i + 1 > t.size()) {
int maxm = 0, l = t.size();
for (int x = i; x <= j - l + 1; x++) {
if (p[x][x + l - 1] > 0) p[i][j]++;
}
}
return;
}
int main() {
int n, m, q;
cin >> n >> m >> q;
int a[q][2];
getchar();
cin >> s;
getchar();
cin >> t;
for (int i = 0; i <= q - 1; i++) {
cin >> a[i][0] >> a[i][1];
}
int l = t.size();
for (int si = l; si <= n; si++) {
for (int j = 0; j <= n - si; j++) dp(j, j + si - 1);
}
for (int i = 0; i <= q - 1; i++) cout << p[a[i][0] - 1][a[i][1] - 1] << "\n";
}
| ### Prompt
Create a solution in CPP for the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
struct Node {
int d;
struct Node *left, *right;
};
int p[1000][1000] = {0};
string s, t;
void dp(int i, int j) {
if (j - i + 1 == t.size()) {
int f = 1;
for (int z = 0; z < t.size(); z++) {
if (s[i + z] != t[z]) {
f = 0;
break;
}
}
if (f == 1) p[i][j] = 1;
} else if (j - i + 1 < t.size())
return;
else if (j - i + 1 > t.size()) {
int maxm = 0, l = t.size();
for (int x = i; x <= j - l + 1; x++) {
if (p[x][x + l - 1] > 0) p[i][j]++;
}
}
return;
}
int main() {
int n, m, q;
cin >> n >> m >> q;
int a[q][2];
getchar();
cin >> s;
getchar();
cin >> t;
for (int i = 0; i <= q - 1; i++) {
cin >> a[i][0] >> a[i][1];
}
int l = t.size();
for (int si = l; si <= n; si++) {
for (int j = 0; j <= n - si; j++) dp(j, j + si - 1);
}
for (int i = 0; i <= q - 1; i++) cout << p[a[i][0] - 1][a[i][1] - 1] << "\n";
}
``` |
#include <bits/stdc++.h>
using namespace std;
string s, t;
vector<int> can;
int n, m, k;
int main() {
cin >> n >> m >> k;
cin >> s >> t;
for (int i = 0; i <= n - m; i++) {
if (s.substr(i, m) != t) continue;
can.push_back(i + 1);
}
while (k--) {
int l, r;
cin >> l >> r;
r = r - m + 1;
int first = lower_bound(can.begin(), can.end(), l) - can.begin();
int last = upper_bound(can.begin(), can.end(), r) - can.begin();
cout << max(0, last - first) << endl;
}
return 0;
}
| ### Prompt
Please create a solution in Cpp to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
string s, t;
vector<int> can;
int n, m, k;
int main() {
cin >> n >> m >> k;
cin >> s >> t;
for (int i = 0; i <= n - m; i++) {
if (s.substr(i, m) != t) continue;
can.push_back(i + 1);
}
while (k--) {
int l, r;
cin >> l >> r;
r = r - m + 1;
int first = lower_bound(can.begin(), can.end(), l) - can.begin();
int last = upper_bound(can.begin(), can.end(), r) - can.begin();
cout << max(0, last - first) << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, q, l, r, vf, ans, i, j, k;
const int LMAX = 1e3;
char s[LMAX + 5], t[LMAX + 5];
int st[LMAX + 5], dr[LMAX + 5], cnt = 0;
int main() {
cin >> n >> m >> q;
for (i = 1; i <= n; i++) cin >> s[i];
for (i = 1; i <= m; i++) cin >> t[i];
for (i = 1; i <= n - m + 1; i++)
if (s[i] == t[1]) {
bool ok = true;
int vf = 0;
for (j = i; j <= i + m - 1; j++)
if (s[j] != t[++vf]) {
ok = false;
break;
}
if (ok == true) {
st[++cnt] = i;
dr[cnt] = i + m - 1;
}
}
for (i = 1; i <= q; i++) {
cin >> l >> r;
ans = 0;
for (j = 1; j <= cnt; j++)
if (st[j] >= l && dr[j] <= r) ans++;
cout << ans << '\n';
}
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, q, l, r, vf, ans, i, j, k;
const int LMAX = 1e3;
char s[LMAX + 5], t[LMAX + 5];
int st[LMAX + 5], dr[LMAX + 5], cnt = 0;
int main() {
cin >> n >> m >> q;
for (i = 1; i <= n; i++) cin >> s[i];
for (i = 1; i <= m; i++) cin >> t[i];
for (i = 1; i <= n - m + 1; i++)
if (s[i] == t[1]) {
bool ok = true;
int vf = 0;
for (j = i; j <= i + m - 1; j++)
if (s[j] != t[++vf]) {
ok = false;
break;
}
if (ok == true) {
st[++cnt] = i;
dr[cnt] = i + m - 1;
}
}
for (i = 1; i <= q; i++) {
cin >> l >> r;
ans = 0;
for (j = 1; j <= cnt; j++)
if (st[j] >= l && dr[j] <= r) ans++;
cout << ans << '\n';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string s, t;
int n, m, q, ct = 0, flag, l, r;
cin >> n >> m >> q;
vector<vector<int>> ans(n + 1, vector<int>(n + 1, 0));
cin >> s;
cin >> t;
for (int i = 0; i < n; i++) {
flag = 1;
for (int j = 0; j < m; j++) {
if (s[i + j] != t[j]) {
flag = 0;
break;
}
}
if (flag) {
ct++;
for (int j = 0; j <= i; j++) ans[j + 1][i + m]++;
}
}
for (int i = 1; i <= n; i++) {
for (int j = i; j < n; j++) ans[i][j + 1] += ans[i][j];
}
for (int i = 0; i < q; i++) {
cin >> l >> r;
cout << ans[l][r] << endl;
}
return 0;
}
| ### Prompt
Please create a solution in cpp to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string s, t;
int n, m, q, ct = 0, flag, l, r;
cin >> n >> m >> q;
vector<vector<int>> ans(n + 1, vector<int>(n + 1, 0));
cin >> s;
cin >> t;
for (int i = 0; i < n; i++) {
flag = 1;
for (int j = 0; j < m; j++) {
if (s[i + j] != t[j]) {
flag = 0;
break;
}
}
if (flag) {
ct++;
for (int j = 0; j <= i; j++) ans[j + 1][i + m]++;
}
}
for (int i = 1; i <= n; i++) {
for (int j = i; j < n; j++) ans[i][j + 1] += ans[i][j];
}
for (int i = 0; i < q; i++) {
cin >> l >> r;
cout << ans[l][r] << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
string str;
vector<int> z;
int len = 0;
void zfunc() {
len = str.size();
z.resize(len);
int ind = 0;
for (int i = 1; i < len; i++) {
if (ind + z[ind] - 1 >= i) {
z[i] = min(ind + z[ind] - i, z[i - ind]);
}
for (; i + z[i] < len && str[i + z[i]] == str[z[i]]; z[i]++)
;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m, q;
cin >> n >> m >> q;
string s, t;
cin >> s >> t;
str = t + '#' + s;
zfunc();
vector<int> pref(s.size() + 1);
for (int i = 1; i <= s.size(); i++) {
pref[i] = pref[i - 1] + int(z[i + t.size()] == t.size());
}
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
if (r - l + 1 < t.size()) {
cout << 0 << '\n';
} else {
cout << pref[r - t.size() + 1] - pref[l - 1] << '\n';
}
}
return 0;
}
| ### Prompt
Please formulate a cpp solution to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
string str;
vector<int> z;
int len = 0;
void zfunc() {
len = str.size();
z.resize(len);
int ind = 0;
for (int i = 1; i < len; i++) {
if (ind + z[ind] - 1 >= i) {
z[i] = min(ind + z[ind] - i, z[i - ind]);
}
for (; i + z[i] < len && str[i + z[i]] == str[z[i]]; z[i]++)
;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m, q;
cin >> n >> m >> q;
string s, t;
cin >> s >> t;
str = t + '#' + s;
zfunc();
vector<int> pref(s.size() + 1);
for (int i = 1; i <= s.size(); i++) {
pref[i] = pref[i - 1] + int(z[i + t.size()] == t.size());
}
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
if (r - l + 1 < t.size()) {
cout << 0 << '\n';
} else {
cout << pref[r - t.size() + 1] - pref[l - 1] << '\n';
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
char s[100005], t[100005];
int Q, m, n, l1, l2;
int sum[100005], fr[100005], ma[100005];
void init() {
int i, np;
scanf("%d%d%d", &l1, &l2, &Q);
scanf("%s", s);
scanf("%s", t);
for (i = l1; i > 0; i--) s[i] = s[i - 1];
for (i = l2; i > 0; i--) t[i] = t[i - 1];
fr[1] = 0;
for (i = 2; i <= l2; i++) {
np = fr[i - 1];
while (t[i] != t[np + 1] && np) np = fr[np];
if (t[i] == t[np + 1])
fr[i] = np + 1;
else
fr[i] = 0;
}
t[l2 + 1] = '&';
ma[0] = 0;
for (i = 1; i <= l1; i++) {
np = ma[i - 1];
while (np && s[i] != t[np + 1]) np = fr[np];
if (s[i] == t[np + 1])
ma[i] = np + 1;
else
ma[i] = 0;
}
sum[0] = 0;
for (i = 1; i <= l1; i++) sum[i] = sum[i - 1] + (ma[i] == l2);
}
int main() {
int i, l, r;
init();
for (i = 1; i <= Q; i++) {
scanf("%d%d", &l, &r);
if (r - l + 1 < l2) {
printf("0\n");
continue;
}
printf("%d\n", max(0, sum[r] - sum[l + l2 - 2]));
}
return 0;
}
| ### Prompt
Create a solution in cpp for the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
char s[100005], t[100005];
int Q, m, n, l1, l2;
int sum[100005], fr[100005], ma[100005];
void init() {
int i, np;
scanf("%d%d%d", &l1, &l2, &Q);
scanf("%s", s);
scanf("%s", t);
for (i = l1; i > 0; i--) s[i] = s[i - 1];
for (i = l2; i > 0; i--) t[i] = t[i - 1];
fr[1] = 0;
for (i = 2; i <= l2; i++) {
np = fr[i - 1];
while (t[i] != t[np + 1] && np) np = fr[np];
if (t[i] == t[np + 1])
fr[i] = np + 1;
else
fr[i] = 0;
}
t[l2 + 1] = '&';
ma[0] = 0;
for (i = 1; i <= l1; i++) {
np = ma[i - 1];
while (np && s[i] != t[np + 1]) np = fr[np];
if (s[i] == t[np + 1])
ma[i] = np + 1;
else
ma[i] = 0;
}
sum[0] = 0;
for (i = 1; i <= l1; i++) sum[i] = sum[i - 1] + (ma[i] == l2);
}
int main() {
int i, l, r;
init();
for (i = 1; i <= Q; i++) {
scanf("%d%d", &l, &r);
if (r - l + 1 < l2) {
printf("0\n");
continue;
}
printf("%d\n", max(0, sum[r] - sum[l + l2 - 2]));
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long ans[1005];
int main() {
long long n, m, k;
string a, b;
cin >> n >> m >> k;
cin >> a >> b;
for (int i = 0; i <= n - m; i++) {
ans[i + 1] = ans[i] + (a.substr(i, m) == b);
}
while (k--) {
long long l, r;
cin >> l >> r;
if (r - l + 1 < m) {
cout << "0" << endl;
} else {
cout << ans[r - m + 1] - ans[l - 1] << endl;
}
}
}
| ### Prompt
Please formulate a CPP solution to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long ans[1005];
int main() {
long long n, m, k;
string a, b;
cin >> n >> m >> k;
cin >> a >> b;
for (int i = 0; i <= n - m; i++) {
ans[i + 1] = ans[i] + (a.substr(i, m) == b);
}
while (k--) {
long long l, r;
cin >> l >> r;
if (r - l + 1 < m) {
cout << "0" << endl;
} else {
cout << ans[r - m + 1] - ans[l - 1] << endl;
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 10;
int main() {
int n, q, g;
int index;
int a[1005];
int k = 0;
string s, t;
string ss[1005];
scanf("%d%d%d", &n, &q, &g);
getchar();
cin >> s >> t;
for (int i = 0; i < n; i++) {
string str = s.substr(i, q);
if (str == t) {
a[k++] = i + 1;
}
}
while (g--) {
int sum = 0;
int l, r;
scanf("%d%d", &l, &r);
for (int i = 0; i < k; i++) {
if (a[i] >= l && a[i] + q - 1 <= r) sum++;
}
printf("%d\n", sum);
}
return 0;
}
| ### Prompt
Please formulate a cpp solution to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 10;
int main() {
int n, q, g;
int index;
int a[1005];
int k = 0;
string s, t;
string ss[1005];
scanf("%d%d%d", &n, &q, &g);
getchar();
cin >> s >> t;
for (int i = 0; i < n; i++) {
string str = s.substr(i, q);
if (str == t) {
a[k++] = i + 1;
}
}
while (g--) {
int sum = 0;
int l, r;
scanf("%d%d", &l, &r);
for (int i = 0; i < k; i++) {
if (a[i] >= l && a[i] + q - 1 <= r) sum++;
}
printf("%d\n", sum);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int arr[1001] = {0};
int u[200001] = {0};
int main() {
ios::sync_with_stdio(false);
int n, m, q, i, l, r;
cin >> n >> m >> q;
string s, t, ch;
cin >> s >> t;
int pos = -1, x = 0;
while (pos + 1 < s.size() && s.find(t, pos + 1) != string::npos) {
pos = s.find(t, pos + 1);
arr[pos + t.size() - 1]++;
}
for (i = 1; i < n; i++) {
arr[i] += arr[i - 1];
}
for (i = 1; i <= q; i++) {
cin >> l >> r;
if (r - l + 1 < t.size())
cout << "0";
else if (l > 1) {
cout << arr[r - 1] - arr[l - 1 + t.size() - 2];
} else if (l == 1) {
cout << arr[r - 1];
}
if (i != q) cout << endl;
}
return 0;
}
| ### Prompt
Please formulate a cpp solution to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int arr[1001] = {0};
int u[200001] = {0};
int main() {
ios::sync_with_stdio(false);
int n, m, q, i, l, r;
cin >> n >> m >> q;
string s, t, ch;
cin >> s >> t;
int pos = -1, x = 0;
while (pos + 1 < s.size() && s.find(t, pos + 1) != string::npos) {
pos = s.find(t, pos + 1);
arr[pos + t.size() - 1]++;
}
for (i = 1; i < n; i++) {
arr[i] += arr[i - 1];
}
for (i = 1; i <= q; i++) {
cin >> l >> r;
if (r - l + 1 < t.size())
cout << "0";
else if (l > 1) {
cout << arr[r - 1] - arr[l - 1 + t.size() - 2];
} else if (l == 1) {
cout << arr[r - 1];
}
if (i != q) cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
;
const double eps = 1e-8;
const int mod = 10007;
const int maxn = 1e6 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f;
const unsigned long long p = 2333;
int n, m, q, l, r, ans;
unsigned long long hs[1007], ht, pw[1007];
char s[1007], t[1007];
void check(int l, int r) {
ans = 0;
for (int i = l; i <= r - m + 1; i++) {
if (hs[i + m - 1] - hs[i - 1] * pw[m] == ht) {
ans++;
}
}
}
int main() {
pw[0] = 1;
for (int i = 1; i < 1007; i++) {
pw[i] = pw[i - 1] * p;
}
scanf("%d%d%d", &n, &m, &q);
scanf("%s%s", s + 1, t + 1);
hs[0] = 0, ht = 0;
for (int i = 1; i <= n; i++) {
hs[i] = hs[i - 1] * p + (s[i] - 'a' + 1);
}
for (int i = 1; i <= m; i++) {
ht = ht * p + (t[i] - 'a' + 1);
}
while (q--) {
scanf("%d%d", &l, &r);
check(l, r);
printf("%d\n", ans);
}
return 0;
}
| ### Prompt
Please formulate a cpp solution to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
;
const double eps = 1e-8;
const int mod = 10007;
const int maxn = 1e6 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f;
const unsigned long long p = 2333;
int n, m, q, l, r, ans;
unsigned long long hs[1007], ht, pw[1007];
char s[1007], t[1007];
void check(int l, int r) {
ans = 0;
for (int i = l; i <= r - m + 1; i++) {
if (hs[i + m - 1] - hs[i - 1] * pw[m] == ht) {
ans++;
}
}
}
int main() {
pw[0] = 1;
for (int i = 1; i < 1007; i++) {
pw[i] = pw[i - 1] * p;
}
scanf("%d%d%d", &n, &m, &q);
scanf("%s%s", s + 1, t + 1);
hs[0] = 0, ht = 0;
for (int i = 1; i <= n; i++) {
hs[i] = hs[i - 1] * p + (s[i] - 'a' + 1);
}
for (int i = 1; i <= m; i++) {
ht = ht * p + (t[i] - 'a' + 1);
}
while (q--) {
scanf("%d%d", &l, &r);
check(l, r);
printf("%d\n", ans);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
inline int two(int n) { return 1 << n; }
inline void set_bit(int& n, int b) { n |= two(b); }
inline void unset_bit(int& n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
template <class T>
T gcd(T a, T b) {
return (b != 0 ? gcd<T>(b, a % b) : a);
}
template <class T>
T lcm(T a, T b) {
return (a / gcd<T>(a, b) * b);
}
const int maxx = 1e4;
int arr[maxx] = {0};
string s, t;
int n, m, q;
bool ok = true;
int main() {
std::ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> q;
cin >> s >> t;
for (int i = 0; i < n - m + 1; ++i) {
if (s[i] == t[0]) {
ok = true;
for (int j = 0; j < m; ++j) {
if (j + i > n - 1) break;
if (s[j + i] != t[j]) {
ok = false;
break;
}
}
if (ok) arr[i] = 1;
}
if (i > 0) arr[i] += arr[i - 1];
}
while (q--) {
int start, end;
cin >> start >> end;
start--, end--;
if (end - start + 1 < m)
cout << 0 << '\n';
else
cout << arr[end - m + 1] - (start ? arr[start - 1] : 0) << '\n';
}
}
| ### Prompt
Your challenge is to write a cpp solution to the following problem:
You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline int two(int n) { return 1 << n; }
inline void set_bit(int& n, int b) { n |= two(b); }
inline void unset_bit(int& n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
template <class T>
T gcd(T a, T b) {
return (b != 0 ? gcd<T>(b, a % b) : a);
}
template <class T>
T lcm(T a, T b) {
return (a / gcd<T>(a, b) * b);
}
const int maxx = 1e4;
int arr[maxx] = {0};
string s, t;
int n, m, q;
bool ok = true;
int main() {
std::ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> q;
cin >> s >> t;
for (int i = 0; i < n - m + 1; ++i) {
if (s[i] == t[0]) {
ok = true;
for (int j = 0; j < m; ++j) {
if (j + i > n - 1) break;
if (s[j + i] != t[j]) {
ok = false;
break;
}
}
if (ok) arr[i] = 1;
}
if (i > 0) arr[i] += arr[i - 1];
}
while (q--) {
int start, end;
cin >> start >> end;
start--, end--;
if (end - start + 1 < m)
cout << 0 << '\n';
else
cout << arr[end - m + 1] - (start ? arr[start - 1] : 0) << '\n';
}
}
``` |
End of preview. Expand
in Dataset Viewer.
No dataset card yet
New: Create and edit this dataset card directly on the website!
Contribute a Dataset Card- Downloads last month
- 17