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'; } } ```
#include <bits/stdc++.h> using namespace std; int n, m, q; char a[1005], b[1005]; struct qujian { int s, e; }; qujian qq[10005]; int num = 0; int main() { scanf("%d%d%d", &n, &m, &q); scanf("%s\n%s", a, b); if (n < m) { for (int i = 0; i < q; i++) { int x, y; scanf("%d%d", &x, &y); printf("0\n"); } } else { for (int i = 0; i <= n - m; i++) { if (strncmp((a + i), b, m) == 0) { qq[num].s = i; qq[num++].e = i + m - 1; } } for (int i = 0; i < q; i++) { int x, y; scanf("%d%d", &x, &y); if (y - x + 1 < m) printf("0\n"); else { x = x - 1; y = y - 1; int flag = 0, tnum = 0; for (int i = 0; i < num; i++) { if (qq[i].s >= x && qq[i].e <= y) { tnum++; } } printf("%d\n", tnum); } } } 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; char a[1005], b[1005]; struct qujian { int s, e; }; qujian qq[10005]; int num = 0; int main() { scanf("%d%d%d", &n, &m, &q); scanf("%s\n%s", a, b); if (n < m) { for (int i = 0; i < q; i++) { int x, y; scanf("%d%d", &x, &y); printf("0\n"); } } else { for (int i = 0; i <= n - m; i++) { if (strncmp((a + i), b, m) == 0) { qq[num].s = i; qq[num++].e = i + m - 1; } } for (int i = 0; i < q; i++) { int x, y; scanf("%d%d", &x, &y); if (y - x + 1 < m) printf("0\n"); else { x = x - 1; y = y - 1; int flag = 0, tnum = 0; for (int i = 0; i < num; i++) { if (qq[i].s >= x && qq[i].e <= y) { tnum++; } } printf("%d\n", tnum); } } } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 1e3 + 10; string s1, s2; int ans[maxn]; int main() { int n, m, q; cin >> n >> m >> q; cin >> s1 >> s2; memset(ans, 0, sizeof(ans)); for (int i = 0; i + m <= n; i++) { if (s1.substr(i, m) == s2) { ans[i + 1]++; } } for (int i = 1; i <= n; i++) { ans[i] += ans[i - 1]; } while (q--) { int a, b; cin >> a >> b; b -= m - 1; if (b < 0) b = 0; cout << max(0, ans[b] - ans[a - 1]) << 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; const int maxn = 1e3 + 10; string s1, s2; int ans[maxn]; int main() { int n, m, q; cin >> n >> m >> q; cin >> s1 >> s2; memset(ans, 0, sizeof(ans)); for (int i = 0; i + m <= n; i++) { if (s1.substr(i, m) == s2) { ans[i + 1]++; } } for (int i = 1; i <= n; i++) { ans[i] += ans[i - 1]; } while (q--) { int a, b; cin >> a >> b; b -= m - 1; if (b < 0) b = 0; cout << max(0, ans[b] - ans[a - 1]) << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int const lmt = 1e5 + 4; long long pre[lmt]; int main() { int n, m, q; cin >> m >> n >> q; string s, t; cin >> s >> t; string tt = t + '#' + s; int len = tt.length(); int f[len]; int j = 0; f[0] = 0; for (int i = 1; i < len; i++) { j = f[i - 1]; while (j > 0 && tt[i] != tt[j]) j = f[j - 1]; if (tt[i] == tt[j]) j++; f[i] = j; if (j == n) { pre[i + 1 - (n + 1)] = 1; } } for (int i = 1; i <= m; i++) { pre[i] += pre[i - 1]; } int l, r; while (q--) { cin >> l >> r; if (r - l + 1 < n) cout << "0\n"; else cout << max(1ll * 0, pre[r] - pre[l + n - 2]) << "\n"; } }
### 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 const lmt = 1e5 + 4; long long pre[lmt]; int main() { int n, m, q; cin >> m >> n >> q; string s, t; cin >> s >> t; string tt = t + '#' + s; int len = tt.length(); int f[len]; int j = 0; f[0] = 0; for (int i = 1; i < len; i++) { j = f[i - 1]; while (j > 0 && tt[i] != tt[j]) j = f[j - 1]; if (tt[i] == tt[j]) j++; f[i] = j; if (j == n) { pre[i + 1 - (n + 1)] = 1; } } for (int i = 1; i <= m; i++) { pre[i] += pre[i - 1]; } int l, r; while (q--) { cin >> l >> r; if (r - l + 1 < n) cout << "0\n"; else cout << max(1ll * 0, pre[r] - pre[l + n - 2]) << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; string s, t; int n, m, q; int v[100000] = {}; void match() { for (int i = 0; i <= n - m; i++) { v[i + 1] = v[i] + (s.substr(i, m) == t); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; cin >> n >> m >> q; cin >> s; cin >> t; match(); for (int i = 0; i < q; i++) { int a, b; cin >> a >> b; if ((b - a + 1) < m) cout << "0\n"; else { cout << v[b - m + 1] - v[a - 1] << 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; string s, t; int n, m, q; int v[100000] = {}; void match() { for (int i = 0; i <= n - m; i++) { v[i + 1] = v[i] + (s.substr(i, m) == t); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; cin >> n >> m >> q; cin >> s; cin >> t; match(); for (int i = 0; i < q; i++) { int a, b; cin >> a >> b; if ((b - a + 1) < m) cout << "0\n"; else { cout << v[b - m + 1] - v[a - 1] << endl; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 1e3 + 10; char str[maxn], s[maxn]; struct node { char ch; int val; node() {} node(char _ch, int _val) : ch(_ch), val(_val) {} }; node stk[maxn]; int top; int f[maxn]; int tr[maxn]; int len1, len2; void update(int x, int v) { while (x <= len1) { tr[x] += v; x += (x & (-x)); } } int get_sum(int x) { int ans = 0; while (x > 0) { ans += tr[x]; x -= (x & (-x)); } return ans; } int fail[maxn]; void getFail(char* P, int len) { fail[0] = fail[1] = 0; for (int i = 1; i < len; i++) { int j = fail[i]; while (j && P[i] != P[j]) j = fail[j]; fail[i + 1] = (P[i] == P[j] ? j + 1 : 0); } } int main() { int m; scanf("%d %d %d", &len1, &len2, &m); scanf(" %s", str); scanf(" %s", s); getFail(s, len2); int j = 0; for (int i = 0; i < len1; i++) { while (j && s[j] != str[i]) j = fail[j]; if (s[j] == str[i]) ++j; if (j == len2) { int x = i - len2 + 1; update(x + 1, 1); } } for (int i = 0; i < m; i++) { int l, r; scanf("%d %d", &l, &r); if (r - l + 1 < len2) { printf("0\n"); } else { printf("%d\n", get_sum(r - len2 + 1) - get_sum(l - 1)); } } 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 maxn = 1e3 + 10; char str[maxn], s[maxn]; struct node { char ch; int val; node() {} node(char _ch, int _val) : ch(_ch), val(_val) {} }; node stk[maxn]; int top; int f[maxn]; int tr[maxn]; int len1, len2; void update(int x, int v) { while (x <= len1) { tr[x] += v; x += (x & (-x)); } } int get_sum(int x) { int ans = 0; while (x > 0) { ans += tr[x]; x -= (x & (-x)); } return ans; } int fail[maxn]; void getFail(char* P, int len) { fail[0] = fail[1] = 0; for (int i = 1; i < len; i++) { int j = fail[i]; while (j && P[i] != P[j]) j = fail[j]; fail[i + 1] = (P[i] == P[j] ? j + 1 : 0); } } int main() { int m; scanf("%d %d %d", &len1, &len2, &m); scanf(" %s", str); scanf(" %s", s); getFail(s, len2); int j = 0; for (int i = 0; i < len1; i++) { while (j && s[j] != str[i]) j = fail[j]; if (s[j] == str[i]) ++j; if (j == len2) { int x = i - len2 + 1; update(x + 1, 1); } } for (int i = 0; i < m; i++) { int l, r; scanf("%d %d", &l, &r); if (r - l + 1 < len2) { printf("0\n"); } else { printf("%d\n", get_sum(r - len2 + 1) - get_sum(l - 1)); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 1005, MAXM = 1005; string T, P, tt; int Next[MAXM], N, M, C; void MakeNext(int M) { int i = 0, j = -1; Next[i] = -1; while (i < M) { if (j == -1 || P[i] == P[j]) Next[++i] = ++j; else j = Next[j]; } } int KMP(int pos, int N, int M) { int i = pos, j = 0, ans = 0; while (i < N) { if (T[i] == P[j] || j == -1) i++, j++; else j = Next[j]; if (j == M) { ans++; j = Next[j - 1]; i--; } } return ans; } int main() { int n, m, q; cin >> n >> m >> q; cin >> tt >> P; int l, r; while (q--) { cin >> l >> r; T = tt.substr(l - 1, r - l + 1); N = r - l + 1; M = m; MakeNext(M); printf("%d\n", KMP(0, N, M)); } 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 MAXN = 1005, MAXM = 1005; string T, P, tt; int Next[MAXM], N, M, C; void MakeNext(int M) { int i = 0, j = -1; Next[i] = -1; while (i < M) { if (j == -1 || P[i] == P[j]) Next[++i] = ++j; else j = Next[j]; } } int KMP(int pos, int N, int M) { int i = pos, j = 0, ans = 0; while (i < N) { if (T[i] == P[j] || j == -1) i++, j++; else j = Next[j]; if (j == M) { ans++; j = Next[j - 1]; i--; } } return ans; } int main() { int n, m, q; cin >> n >> m >> q; cin >> tt >> P; int l, r; while (q--) { cin >> l >> r; T = tt.substr(l - 1, r - l + 1); N = r - l + 1; M = m; MakeNext(M); printf("%d\n", KMP(0, N, M)); } 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; int ans[1000003] = {0}; for (int i = 0; i < n; i++) { if (s.substr(i, m) == t) { ans[i] = 1; } } while (q--) { int l, r, cnt = 0; cin >> l >> r; if (r - l + 1 < m) { cout << 0 << endl; } else { l--; r--; for (int i = l; i <= r; i++) { if (ans[i] == 1 && i + m - 1 <= r) cnt++; } cout << cnt << 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() { int n, m, q; cin >> n >> m >> q; string s, t; cin >> s >> t; int ans[1000003] = {0}; for (int i = 0; i < n; i++) { if (s.substr(i, m) == t) { ans[i] = 1; } } while (q--) { int l, r, cnt = 0; cin >> l >> r; if (r - l + 1 < m) { cout << 0 << endl; } else { l--; r--; for (int i = l; i <= r; i++) { if (ans[i] == 1 && i + m - 1 <= r) cnt++; } cout << cnt << endl; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; string a; string b; int ans[1000 + 7]; int main() { int m, n, q; cin >> m >> n >> q; cin >> a >> b; int tmp = 0; for (int i = 0; i < m; i++) { if (a[i] == b[0]) { int cnt = 0; for (int j = 0; j < n; j++) { if (a[i + j] == b[j]) cnt++; else break; } if (cnt == n) tmp += 1; } ans[i + 1] = tmp; } for (int i = 0; i < q; i++) { int x, y; cin >> x >> y; y = y - n + 1; if (x > y) cout << "0" << endl; else cout << ans[y] - ans[x - 1] << endl; } }
### 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; string a; string b; int ans[1000 + 7]; int main() { int m, n, q; cin >> m >> n >> q; cin >> a >> b; int tmp = 0; for (int i = 0; i < m; i++) { if (a[i] == b[0]) { int cnt = 0; for (int j = 0; j < n; j++) { if (a[i + j] == b[j]) cnt++; else break; } if (cnt == n) tmp += 1; } ans[i + 1] = tmp; } for (int i = 0; i < q; i++) { int x, y; cin >> x >> y; y = y - n + 1; if (x > y) cout << "0" << endl; else cout << ans[y] - ans[x - 1] << endl; } } ```
#include <bits/stdc++.h> #pragma GCC optimize("O3", "unroll-loops") using namespace std; template <class T1, class T2> inline void checkmin(T1 &x, T2 y) { if (x > y) x = y; } template <class T1, class T2> inline void checkmax(T1 &x, T2 y) { if (x < y) x = y; } template <class T1> inline void sort(T1 &arr) { sort(arr.begin(), arr.end()); } template <class T1> inline void rsort(T1 &arr) { sort(arr.rbegin(), arr.rend()); } template <class T1> inline void reverse(T1 &arr) { reverse(arr.begin(), arr.end()); } template <class T1> inline void shuffle(T1 &arr) { for (int i = -int(arr.size()); i < int(arr.size()); ++i) swap(arr[rand() % arr.size()], arr[rand() % arr.size()]); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << fixed << setprecision(20); srand(time(NULL)); long long n, m, q; cin >> n >> m >> q; string second, t; cin >> second >> t; vector<int> cnt(second.size()); for (int i = 0; i <= int(second.size()) - int(t.size()); ++i) { string news = ""; for (int j = i; j < i + t.size(); ++j) news += second[j]; if (news == t) ++cnt[i]; } for (int i = 1; i < cnt.size(); ++i) cnt[i] += cnt[i - 1]; for (int i = 0; i < q; ++i) { int u, v; cin >> u >> v; --u; v -= int(t.size()); if (v < u) cout << 0 << '\n'; else { if (u == 0) cout << cnt[v] << '\n'; else cout << cnt[v] - cnt[u - 1] << '\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> #pragma GCC optimize("O3", "unroll-loops") using namespace std; template <class T1, class T2> inline void checkmin(T1 &x, T2 y) { if (x > y) x = y; } template <class T1, class T2> inline void checkmax(T1 &x, T2 y) { if (x < y) x = y; } template <class T1> inline void sort(T1 &arr) { sort(arr.begin(), arr.end()); } template <class T1> inline void rsort(T1 &arr) { sort(arr.rbegin(), arr.rend()); } template <class T1> inline void reverse(T1 &arr) { reverse(arr.begin(), arr.end()); } template <class T1> inline void shuffle(T1 &arr) { for (int i = -int(arr.size()); i < int(arr.size()); ++i) swap(arr[rand() % arr.size()], arr[rand() % arr.size()]); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << fixed << setprecision(20); srand(time(NULL)); long long n, m, q; cin >> n >> m >> q; string second, t; cin >> second >> t; vector<int> cnt(second.size()); for (int i = 0; i <= int(second.size()) - int(t.size()); ++i) { string news = ""; for (int j = i; j < i + t.size(); ++j) news += second[j]; if (news == t) ++cnt[i]; } for (int i = 1; i < cnt.size(); ++i) cnt[i] += cnt[i - 1]; for (int i = 0; i < q; ++i) { int u, v; cin >> u >> v; --u; v -= int(t.size()); if (v < u) cout << 0 << '\n'; else { if (u == 0) cout << cnt[v] << '\n'; else cout << cnt[v] - cnt[u - 1] << '\n'; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MaxN = 100005; int BIT[MaxN]; void update(int pos) { int i, j, p, q; for (i = pos; i < MaxN; i += i & (-i)) { BIT[i] += 1; } } int query(int pos) { int i, j, p = 0, q; for (i = pos; i > 0; i -= i & (-i)) { p += BIT[i]; } return p; } int main() { cin.tie(0), ios_base::sync_with_stdio(0); int N, M, K, i, j, p, q; bool proof; cin >> N >> M >> K; string a, b; cin >> a >> b; for (i = 0; i <= N - M; i++) { proof = 1; for (j = i; j < i + M; j++) { if (a[j] != b[j - i]) { proof = 0; continue; } } if (proof) { update(i + 1); } } for (i = 0; i < K; i++) { cin >> p >> q; if (q - M - p + 1 < 0) cout << "0\n"; else cout << query(q - M + 1) - query(p - 1) << "\n"; } }
### 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 MaxN = 100005; int BIT[MaxN]; void update(int pos) { int i, j, p, q; for (i = pos; i < MaxN; i += i & (-i)) { BIT[i] += 1; } } int query(int pos) { int i, j, p = 0, q; for (i = pos; i > 0; i -= i & (-i)) { p += BIT[i]; } return p; } int main() { cin.tie(0), ios_base::sync_with_stdio(0); int N, M, K, i, j, p, q; bool proof; cin >> N >> M >> K; string a, b; cin >> a >> b; for (i = 0; i <= N - M; i++) { proof = 1; for (j = i; j < i + M; j++) { if (a[j] != b[j - i]) { proof = 0; continue; } } if (proof) { update(i + 1); } } for (i = 0; i < K; i++) { cin >> p >> q; if (q - M - p + 1 < 0) cout << "0\n"; else cout << query(q - M + 1) - query(p - 1) << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; char s[2000], t[2000]; int f[2000]; int main() { int n, m, q; scanf("%d%d%d", &n, &m, &q); scanf("%s", s + 1); scanf("%s", t + 1); for (int i = 1; i <= n - m + 1; i++) { int flag = 1; for (int j = 1; j <= m; j++) if (s[i + j - 1] != t[j]) { flag = 0; break; } f[i] = f[i - 1] + flag; } for (int i = 1; i <= q; i++) { int l, r; scanf("%d%d", &l, &r); if (r - l + 1 < m) printf("0\n"); else printf("%d\n", f[r - m + 1] - f[l - 1]); } }
### 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; char s[2000], t[2000]; int f[2000]; int main() { int n, m, q; scanf("%d%d%d", &n, &m, &q); scanf("%s", s + 1); scanf("%s", t + 1); for (int i = 1; i <= n - m + 1; i++) { int flag = 1; for (int j = 1; j <= m; j++) if (s[i + j - 1] != t[j]) { flag = 0; break; } f[i] = f[i - 1] + flag; } for (int i = 1; i <= q; i++) { int l, r; scanf("%d%d", &l, &r); if (r - l + 1 < m) printf("0\n"); else printf("%d\n", f[r - m + 1] - f[l - 1]); } } ```
#include <bits/stdc++.h> using namespace std; int main() { int i, j, n, m, l, r, q, nr; string s; string t; cin >> n >> m >> q; cin >> s >> t; int cnt[1001] = {0}; for (int i = 0; i <= n - m; i++) { if (s.substr(i, m) == t) { cnt[i] = 1; } } l = 0; r = 0; for (i = 1; i <= q; i++) { cin >> l >> r; nr = 0; for (j = l - 1; j <= r - m; j++) { if (cnt[j]) nr++; } cout << nr << '\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; int main() { int i, j, n, m, l, r, q, nr; string s; string t; cin >> n >> m >> q; cin >> s >> t; int cnt[1001] = {0}; for (int i = 0; i <= n - m; i++) { if (s.substr(i, m) == t) { cnt[i] = 1; } } l = 0; r = 0; for (i = 1; i <= q; i++) { cin >> l >> r; nr = 0; for (j = l - 1; j <= r - m; j++) { if (cnt[j]) nr++; } cout << nr << '\n'; } 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) { bra[pre + 1]++; i--; bra[i]--; ara[pre + 1]++; } i = pre; } for (long long i = 1; i <= n + 10; i++) { ara[i] += ara[i - 1]; bra[i] += bra[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 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; 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) { bra[pre + 1]++; i--; bra[i]--; ara[pre + 1]++; } i = pre; } for (long long i = 1; i <= n + 10; i++) { ara[i] += ara[i - 1]; bra[i] += bra[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> #pragma GCC optimize("O3") using namespace std; const long long MAXN = 1e3 + 10; long long n, m, q; string a, b; long long pref[MAXN]; inline void precalc() { for (long long i = 0; i < n - m + 1; ++i) { bool flag = true; for (long long j = i; j < i + m; ++j) { if (a[j] != b[j - i]) { flag = false; break; } } if (i == 0) { pref[i] = flag; continue; } pref[i] = pref[i - 1] + flag; } } inline long long get(long long i) { if (i < 0) return 0; return pref[i]; } inline long long get_ans(long long L, long long R) { if (R - m + 1 < 0 || L + m - 1 > R) return 0; return pref[R - m + 1] - get(L - 1); } signed main() { ios::sync_with_stdio(0); cin >> n >> m >> q; cin >> a >> b; precalc(); for (long long i = 0; i < q; ++i) { long long L, R; cin >> L >> R; --L, --R; cout << get_ans(L, R) << "\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> #pragma GCC optimize("O3") using namespace std; const long long MAXN = 1e3 + 10; long long n, m, q; string a, b; long long pref[MAXN]; inline void precalc() { for (long long i = 0; i < n - m + 1; ++i) { bool flag = true; for (long long j = i; j < i + m; ++j) { if (a[j] != b[j - i]) { flag = false; break; } } if (i == 0) { pref[i] = flag; continue; } pref[i] = pref[i - 1] + flag; } } inline long long get(long long i) { if (i < 0) return 0; return pref[i]; } inline long long get_ans(long long L, long long R) { if (R - m + 1 < 0 || L + m - 1 > R) return 0; return pref[R - m + 1] - get(L - 1); } signed main() { ios::sync_with_stdio(0); cin >> n >> m >> q; cin >> a >> b; precalc(); for (long long i = 0; i < q; ++i) { long long L, R; cin >> L >> R; --L, --R; cout << get_ans(L, R) << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; void add_self(int& a, int b) { a += b; if (a >= (int)1e9 + 7) a -= (int)1e9 + 7; } void solve() { int n, m, q; cin >> n >> m >> q; string s, t; cin >> s >> t; vector<int> a(n + 1, 0); int cnt = 0; for (int i = 0; i <= n - m; i++) { bool ok = true; int k = 0; for (int j = i; j < i + m; j++) { if (s[j] != t[k++]) { ok = false; break; } } if (ok) { cnt++; } a[i + 1] = cnt; } for (int i = n - m + 1; i <= n; i++) a[i] = cnt; while (q--) { int l, r; cin >> l >> r; int t = r - m + 1; cout << (a[t] - a[l - 1]) * (r - l + 1 >= m) << endl; } } int main() { ios::sync_with_stdio(false); int q = 1; for (int i = 1; i <= q; i++) { solve(); } }
### 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; void add_self(int& a, int b) { a += b; if (a >= (int)1e9 + 7) a -= (int)1e9 + 7; } void solve() { int n, m, q; cin >> n >> m >> q; string s, t; cin >> s >> t; vector<int> a(n + 1, 0); int cnt = 0; for (int i = 0; i <= n - m; i++) { bool ok = true; int k = 0; for (int j = i; j < i + m; j++) { if (s[j] != t[k++]) { ok = false; break; } } if (ok) { cnt++; } a[i + 1] = cnt; } for (int i = n - m + 1; i <= n; i++) a[i] = cnt; while (q--) { int l, r; cin >> l >> r; int t = r - m + 1; cout << (a[t] - a[l - 1]) * (r - l + 1 >= m) << endl; } } int main() { ios::sync_with_stdio(false); int q = 1; for (int i = 1; i <= q; i++) { solve(); } } ```
#include <bits/stdc++.h> using namespace std; const int mxn = 1e3 + 5, seed = 131, seed2 = 31, MOD = 1e9 + 9; int n, m, q; string s, t; long long p[mxn], pp[mxn], a[mxn], aa[mxn], b[mxn], bb[mxn]; inline long long strHash(int l, int r) { return (long long)((1LL * a[r] - 1LL * a[l - 1] * p[r - l + 1] % MOD) + MOD) % MOD; } int main() { cin >> n >> m >> q >> s >> t; p[0] = 1; for (int i = 1; i <= max(n, m); i++) p[i] = ((p[i - 1] * seed % MOD) + MOD) % MOD; for (int i = 1; i <= n; i++) a[i] = ((a[i - 1] * seed + s[i - 1]) % MOD + MOD) % MOD; for (int i = 1; i <= m; i++) b[i] = ((b[i - 1] * seed + t[i - 1]) % MOD + MOD) % MOD; for (int i = 1, L, R, ans; i <= q; i++) { cin >> L >> R; ans = 0; for (int j = L; j + m - 1 <= R; j++) { if (strHash(j, j + m - 1) == b[m]) ans++; } cout << ans << "\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; const int mxn = 1e3 + 5, seed = 131, seed2 = 31, MOD = 1e9 + 9; int n, m, q; string s, t; long long p[mxn], pp[mxn], a[mxn], aa[mxn], b[mxn], bb[mxn]; inline long long strHash(int l, int r) { return (long long)((1LL * a[r] - 1LL * a[l - 1] * p[r - l + 1] % MOD) + MOD) % MOD; } int main() { cin >> n >> m >> q >> s >> t; p[0] = 1; for (int i = 1; i <= max(n, m); i++) p[i] = ((p[i - 1] * seed % MOD) + MOD) % MOD; for (int i = 1; i <= n; i++) a[i] = ((a[i - 1] * seed + s[i - 1]) % MOD + MOD) % MOD; for (int i = 1; i <= m; i++) b[i] = ((b[i - 1] * seed + t[i - 1]) % MOD + MOD) % MOD; for (int i = 1, L, R, ans; i <= q; i++) { cin >> L >> R; ans = 0; for (int j = L; j + m - 1 <= R; j++) { if (strHash(j, j + m - 1) == b[m]) ans++; } cout << ans << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; int match[1009]; int prefix[1009]; string s, t; bool isMatch(int a) { for (int i = 0; i < t.size(); i++) { if (i + a >= s.size() || s[i + a] != t[i]) { return false; } } return true; } int main() { ios_base::sync_with_stdio(0); int n, m; cin >> n >> m; int q; cin >> q; cin >> s >> t; for (int i = 0; i < s.size(); i++) { match[i] = isMatch(i); } prefix[0] = match[0]; for (int i = 1; i < s.size(); i++) { prefix[i] = prefix[i - 1] + match[i]; } cout << endl; for (int i = 0; i < q; i++) { int p, q; cin >> p >> q; q -= t.size() - 2; q--; p--; q--; p--; int ans; if (q < 0 || q <= p) { ans = 0; } else if (p == -1) { ans = prefix[q]; } else { ans = prefix[q] - prefix[p]; } cout << ans << endl; } }
### 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 match[1009]; int prefix[1009]; string s, t; bool isMatch(int a) { for (int i = 0; i < t.size(); i++) { if (i + a >= s.size() || s[i + a] != t[i]) { return false; } } return true; } int main() { ios_base::sync_with_stdio(0); int n, m; cin >> n >> m; int q; cin >> q; cin >> s >> t; for (int i = 0; i < s.size(); i++) { match[i] = isMatch(i); } prefix[0] = match[0]; for (int i = 1; i < s.size(); i++) { prefix[i] = prefix[i - 1] + match[i]; } cout << endl; for (int i = 0; i < q; i++) { int p, q; cin >> p >> q; q -= t.size() - 2; q--; p--; q--; p--; int ans; if (q < 0 || q <= p) { ans = 0; } else if (p == -1) { ans = prefix[q]; } else { ans = prefix[q] - prefix[p]; } cout << ans << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, q, t = 0; string a, b, s = ""; cin >> n >> m >> q >> a >> b; vector<pair<int, int> > v; while (t + b.size() - 1 < a.size()) { if (a.substr(t, b.size()) == b) { v.push_back({t + 1, t + b.size()}); } t++; } while (q--) { int l, r; cin >> l >> r; int c = 0; for (int i = 0; i < v.size(); i++) { if (l <= v[i].first && r >= v[i].second) c++; } cout << c << '\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; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, q, t = 0; string a, b, s = ""; cin >> n >> m >> q >> a >> b; vector<pair<int, int> > v; while (t + b.size() - 1 < a.size()) { if (a.substr(t, b.size()) == b) { v.push_back({t + 1, t + b.size()}); } t++; } while (q--) { int l, r; cin >> l >> r; int c = 0; for (int i = 0; i < v.size(); i++) { if (l <= v[i].first && r >= v[i].second) c++; } cout << c << '\n'; } } ```
#include <bits/stdc++.h> using namespace std; string s, t; int ans[100009]; int lps[100009]; int main() { int n, m, q; cin >> n >> m >> q; cin >> s >> t; int sz = t.size(); int sz1 = s.size(); for (int i = 1; i <= (sz1 + 1 - sz); i++) { int flag = 0; string sb = s.substr(i - 1, sz); if (sb == t) ans[i]++; } for (int i = 1; i <= sz1; i++) ans[i] += ans[i - 1]; while (q--) { int x, y; cin >> x >> y; if (y - x + 1 < sz) cout << 0 << endl; else { int mx = y + 1 - sz; int val; cout << ans[mx] - ans[x - 1] << 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; string s, t; int ans[100009]; int lps[100009]; int main() { int n, m, q; cin >> n >> m >> q; cin >> s >> t; int sz = t.size(); int sz1 = s.size(); for (int i = 1; i <= (sz1 + 1 - sz); i++) { int flag = 0; string sb = s.substr(i - 1, sz); if (sb == t) ans[i]++; } for (int i = 1; i <= sz1; i++) ans[i] += ans[i - 1]; while (q--) { int x, y; cin >> x >> y; if (y - x + 1 < sz) cout << 0 << endl; else { int mx = y + 1 - sz; int val; cout << ans[mx] - ans[x - 1] << endl; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long n, m, q, i, j, l, r, a[5000]; string s, t; cin >> n >> m >> q; cin >> s >> t; if (m > n) goto label; if (s.compare(0, m, t) == 0) { a[0] = 1; } else a[0] = 0; for (j = 1; j <= n - m + 1; j++) { if (s.compare(j, m, t) == 0) { a[j] = a[j - 1] + 1; } else { a[j] = a[j - 1]; } } for (j = n - m + 2; j < n; j++) { a[j] = a[n - m + 1]; } label: for (i = 0; i < q; i++) { cin >> l >> r; l--; r--; if (r - l < m - 1 || m > n) cout << l - l << endl; else if (l == 0) cout << a[r - m + 1] << endl; else cout << a[r - m + 1] - a[l - 1] << 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; int main() { long long n, m, q, i, j, l, r, a[5000]; string s, t; cin >> n >> m >> q; cin >> s >> t; if (m > n) goto label; if (s.compare(0, m, t) == 0) { a[0] = 1; } else a[0] = 0; for (j = 1; j <= n - m + 1; j++) { if (s.compare(j, m, t) == 0) { a[j] = a[j - 1] + 1; } else { a[j] = a[j - 1]; } } for (j = n - m + 2; j < n; j++) { a[j] = a[n - m + 1]; } label: for (i = 0; i < q; i++) { cin >> l >> r; l--; r--; if (r - l < m - 1 || m > n) cout << l - l << endl; else if (l == 0) cout << a[r - m + 1] << endl; else cout << a[r - m + 1] - a[l - 1] << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int max(int a, int b) { return (a > b) ? a : b; } int min(int a, int b) { return (a < b) ? a : b; } int main() { int n, m, k; cin >> n >> m >> k; string s, t; cin >> s; cin >> t; unordered_map<char, int> umap; vector<int> skips; for (int i = 0; i < t.size(); i++) { if (umap.find(t[i]) == umap.end()) { umap[t[i]] = i; skips.push_back(i + 1); } else { skips.push_back(i - umap[t[i]]); umap[t[i]] = i; } } vector<int> counter(n); int count = 0, x = 0; for (int j = 0; j < n && m + j - 1 < n;) { bool ans = 0; for (int x = 0; x < m && m + j - 1 < n; x++) { if (t[x] != s[j + x]) { ans = 0; if (x != 0) j += skips[x - 1]; else j += 1; break; } else { ans = 1; } } if (ans == 1) { count++; counter[j + m - 1] = count; j += skips[m - 1]; } } for (int i = 1; i < n; i++) { if (counter[i - 1] != 0 && counter[i] == 0) counter[i] = counter[i - 1]; } for (int i = 0; i < k; i++) { int l, r; cin >> l >> r; if (l - 1 + m - 2 >= 0) cout << counter[r - 1] - counter[min(r - 1, l - 1 + m - 2)] << endl; else cout << counter[r - 1] << 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> using namespace std; int max(int a, int b) { return (a > b) ? a : b; } int min(int a, int b) { return (a < b) ? a : b; } int main() { int n, m, k; cin >> n >> m >> k; string s, t; cin >> s; cin >> t; unordered_map<char, int> umap; vector<int> skips; for (int i = 0; i < t.size(); i++) { if (umap.find(t[i]) == umap.end()) { umap[t[i]] = i; skips.push_back(i + 1); } else { skips.push_back(i - umap[t[i]]); umap[t[i]] = i; } } vector<int> counter(n); int count = 0, x = 0; for (int j = 0; j < n && m + j - 1 < n;) { bool ans = 0; for (int x = 0; x < m && m + j - 1 < n; x++) { if (t[x] != s[j + x]) { ans = 0; if (x != 0) j += skips[x - 1]; else j += 1; break; } else { ans = 1; } } if (ans == 1) { count++; counter[j + m - 1] = count; j += skips[m - 1]; } } for (int i = 1; i < n; i++) { if (counter[i - 1] != 0 && counter[i] == 0) counter[i] = counter[i - 1]; } for (int i = 0; i < k; i++) { int l, r; cin >> l >> r; if (l - 1 + m - 2 >= 0) cout << counter[r - 1] - counter[min(r - 1, l - 1 + m - 2)] << endl; else cout << counter[r - 1] << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long mod[] = {(long long)1e9 + 7, (long long)1e9 + 9, (long long)1e9 + 21, (long long)1e9 + 33}; const int maxn = 2e3 + 5; const int Nmod = 3; string a, b; int i, j, n, m, q; long long Pow[Nmod][maxn]; static long long Hash[Nmod][maxn], Hash1[Nmod][maxn], Key[Nmod]; static int dp[maxn][maxn]; long long gethash(int i, int j, int type) { return (Hash[type][j] - Hash[type][i - 1] * Pow[type][j - i + 1] + (long long)mod[type] * (long long)mod[type]) % mod[type]; } void init() { cin >> n >> m >> q; cin >> a; a = " " + a; for (int i = 0; i < Nmod; ++i) { Pow[i][0] = 1; for (int j = 1; j <= n; ++j) Pow[i][j] = (Pow[i][j - 1] * 31) % mod[i]; } for (int i = 0; i < Nmod; ++i) { Hash[i][0] = 0; for (int j = 1; j <= n; ++j) Hash[i][j] = (Hash[i][j - 1] * 31 + a[j] - 'a') % mod[i]; } cin >> b; b = " " + b; for (int i = 0; i < Nmod; ++i) { for (int j = 1; j <= m; ++j) { Key[i] = (Key[i] * 31 + b[j] - 'a') % mod[i]; } } for (int t = (1); t <= (n); ++t) { for (int i = t; i <= n - m + 1; ++i) { int ok = 1; for (int j = 0; j < Nmod; ++j) { ok = min(ok, (int)(gethash(i, i + m - 1, j) == Key[j])); } dp[t][i + m - 1] = dp[t][i + m - 2] + ok; } } for (int i = (1); i <= (q); ++i) { int x, y; cin >> x >> y; cout << dp[x][y] << '\n'; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); init(); 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; const long long mod[] = {(long long)1e9 + 7, (long long)1e9 + 9, (long long)1e9 + 21, (long long)1e9 + 33}; const int maxn = 2e3 + 5; const int Nmod = 3; string a, b; int i, j, n, m, q; long long Pow[Nmod][maxn]; static long long Hash[Nmod][maxn], Hash1[Nmod][maxn], Key[Nmod]; static int dp[maxn][maxn]; long long gethash(int i, int j, int type) { return (Hash[type][j] - Hash[type][i - 1] * Pow[type][j - i + 1] + (long long)mod[type] * (long long)mod[type]) % mod[type]; } void init() { cin >> n >> m >> q; cin >> a; a = " " + a; for (int i = 0; i < Nmod; ++i) { Pow[i][0] = 1; for (int j = 1; j <= n; ++j) Pow[i][j] = (Pow[i][j - 1] * 31) % mod[i]; } for (int i = 0; i < Nmod; ++i) { Hash[i][0] = 0; for (int j = 1; j <= n; ++j) Hash[i][j] = (Hash[i][j - 1] * 31 + a[j] - 'a') % mod[i]; } cin >> b; b = " " + b; for (int i = 0; i < Nmod; ++i) { for (int j = 1; j <= m; ++j) { Key[i] = (Key[i] * 31 + b[j] - 'a') % mod[i]; } } for (int t = (1); t <= (n); ++t) { for (int i = t; i <= n - m + 1; ++i) { int ok = 1; for (int j = 0; j < Nmod; ++j) { ok = min(ok, (int)(gethash(i, i + m - 1, j) == Key[j])); } dp[t][i + m - 1] = dp[t][i + m - 2] + ok; } } for (int i = (1); i <= (q); ++i) { int x, y; cin >> x >> y; cout << dp[x][y] << '\n'; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); init(); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 10; int main() { ios_base::sync_with_stdio(false); int n, m, q, l, r, tlen, cnt; char s[N], t[N]; bool match[N] = {0}; cin >> n >> m >> q >> s >> t; tlen = strlen(t); for (int i = 0; s[i]; ++i) { match[i] = !strncmp(s + i, t, tlen); } while (q-- > 0) { cin >> l >> r; --l; --r; cnt = 0; for (int i = l; i <= r - tlen + 1; ++i) { cnt += match[i]; } 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; const int N = 1e3 + 10; int main() { ios_base::sync_with_stdio(false); int n, m, q, l, r, tlen, cnt; char s[N], t[N]; bool match[N] = {0}; cin >> n >> m >> q >> s >> t; tlen = strlen(t); for (int i = 0; s[i]; ++i) { match[i] = !strncmp(s + i, t, tlen); } while (q-- > 0) { cin >> l >> r; --l; --r; cnt = 0; for (int i = l; i <= r - tlen + 1; ++i) { cnt += match[i]; } cout << cnt << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; char a[1005], b[1005]; int sum[1005]; int main() { ios::sync_with_stdio(false); int n, m, t; cin >> n >> m >> t; cin >> a + 1 >> b + 1; for (int i = m; i <= n; i++) { for (int j = 1; j <= m; j++) { if (a[i - m + j] != b[j]) break; if (j == m) sum[i] = 1; } } for (int i = 1; i <= n; i++) sum[i] += sum[i - 1]; while (t--) { int l, r; cin >> l >> r; if (r - l + 1 < m) cout << 0 << endl; else cout << sum[r] - sum[l + m - 2] << 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; char a[1005], b[1005]; int sum[1005]; int main() { ios::sync_with_stdio(false); int n, m, t; cin >> n >> m >> t; cin >> a + 1 >> b + 1; for (int i = m; i <= n; i++) { for (int j = 1; j <= m; j++) { if (a[i - m + j] != b[j]) break; if (j == m) sum[i] = 1; } } for (int i = 1; i <= n; i++) sum[i] += sum[i - 1]; while (t--) { int l, r; cin >> l >> r; if (r - l + 1 < m) cout << 0 << endl; else cout << sum[r] - sum[l + m - 2] << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long pw[(int)1e6 + 5]; long long base = 1331; long long Hash[(int)1e6 + 5]; void preCal() { pw[0] = 1; for (int i = 1; i < (int)1e6 + 5; i++) pw[i] = pw[i - 1] * base; } void setHash(string s) { Hash[0] = 0; for (int i = 1; i < s.size(); i++) Hash[i] = Hash[i - 1] * base + s[i]; } long long getHash(int l, int r) { return Hash[r] - (Hash[l - 1] * pw[r - l + 1]); } long long Hasher(string s) { long long hashValue = 0; for (int i = 0; i < s.size(); i++) hashValue = hashValue * base + s[i]; return hashValue; } int main() { preCal(); long long n, m, q; cin >> n >> m >> q; string s, ss; cin >> s >> ss; while (q--) { long long le, ri; cin >> le >> ri; string temp = "\0"; for (int i = le - 1; i < ri; i++) temp += s[i]; temp = "$" + temp; setHash(temp); int l1 = temp.size(), l2 = ss.size(); long long hashValue = Hasher(ss); int c = 0; for (int i = 1; i + l2 <= l1; i++) { int l = i, r = i + l2 - 1; if (getHash(l, r) == hashValue) c++; } cout << c << 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; long long pw[(int)1e6 + 5]; long long base = 1331; long long Hash[(int)1e6 + 5]; void preCal() { pw[0] = 1; for (int i = 1; i < (int)1e6 + 5; i++) pw[i] = pw[i - 1] * base; } void setHash(string s) { Hash[0] = 0; for (int i = 1; i < s.size(); i++) Hash[i] = Hash[i - 1] * base + s[i]; } long long getHash(int l, int r) { return Hash[r] - (Hash[l - 1] * pw[r - l + 1]); } long long Hasher(string s) { long long hashValue = 0; for (int i = 0; i < s.size(); i++) hashValue = hashValue * base + s[i]; return hashValue; } int main() { preCal(); long long n, m, q; cin >> n >> m >> q; string s, ss; cin >> s >> ss; while (q--) { long long le, ri; cin >> le >> ri; string temp = "\0"; for (int i = le - 1; i < ri; i++) temp += s[i]; temp = "$" + temp; setHash(temp); int l1 = temp.size(), l2 = ss.size(); long long hashValue = Hasher(ss); int c = 0; for (int i = 1; i + l2 <= l1; i++) { int l = i, r = i + l2 - 1; if (getHash(l, r) == hashValue) c++; } cout << c << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m, q; string s, t; cin >> n >> m >> q >> s >> t; vector<int> ans; for (int i = 0; i < n; i++) { bool flag = 1; for (int j = 0; j < m; j++) { if (t[j] != s[j + i]) { flag = 0; break; } } if (flag) { ans.push_back(i + 1); } } while (q--) { int l, r; cin >> l >> r; r = (r - m + 1); if (r < l) { cout << 0 << '\n'; } else cout << (upper_bound(ans.begin(), ans.end(), r) - lower_bound(ans.begin(), ans.end(), l)) << '\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> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m, q; string s, t; cin >> n >> m >> q >> s >> t; vector<int> ans; for (int i = 0; i < n; i++) { bool flag = 1; for (int j = 0; j < m; j++) { if (t[j] != s[j + i]) { flag = 0; break; } } if (flag) { ans.push_back(i + 1); } } while (q--) { int l, r; cin >> l >> r; r = (r - m + 1); if (r < l) { cout << 0 << '\n'; } else cout << (upper_bound(ans.begin(), ans.end(), r) - lower_bound(ans.begin(), ans.end(), l)) << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; char str[1011]; char ptr[1011]; int Next[1011]; int plen, slen; int n, m, q, x, y; inline int in() { int f = 1, ans, ch; while ((ch = getchar()) < '0' || ch > '9') if ('-' == ch) { ch = getchar(), f = -1; break; } ans = ch - '0'; while ((ch = getchar()) >= '0' && ch <= '9') ans = (ans << 3) + (ans << 1) + ch - '0'; return f * ans; } void getNext() { int i = 0, j = -1; memset(Next, 0, sizeof(Next)); Next[0] = -1; while (i < plen) { if (j == -1 || ptr[i] == ptr[j]) { i++, j++; Next[i] = j; } else j = Next[j]; } } int KMP() { int i = x - 1, j = 0; int ans = 0; while (i < y) { if (j == -1 || str[i] == ptr[j]) { i++, j++; } else j = Next[j]; if (j == plen) { j = Next[j]; ans++; } } return ans; } int main() { scanf("%d%d%d", &n, &m, &q); scanf("%s", str); scanf("%s", ptr); plen = strlen(ptr); slen = strlen(str); getNext(); while (q--) { x = in(); y = in(); int ans = KMP(); 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; char str[1011]; char ptr[1011]; int Next[1011]; int plen, slen; int n, m, q, x, y; inline int in() { int f = 1, ans, ch; while ((ch = getchar()) < '0' || ch > '9') if ('-' == ch) { ch = getchar(), f = -1; break; } ans = ch - '0'; while ((ch = getchar()) >= '0' && ch <= '9') ans = (ans << 3) + (ans << 1) + ch - '0'; return f * ans; } void getNext() { int i = 0, j = -1; memset(Next, 0, sizeof(Next)); Next[0] = -1; while (i < plen) { if (j == -1 || ptr[i] == ptr[j]) { i++, j++; Next[i] = j; } else j = Next[j]; } } int KMP() { int i = x - 1, j = 0; int ans = 0; while (i < y) { if (j == -1 || str[i] == ptr[j]) { i++, j++; } else j = Next[j]; if (j == plen) { j = Next[j]; ans++; } } return ans; } int main() { scanf("%d%d%d", &n, &m, &q); scanf("%s", str); scanf("%s", ptr); plen = strlen(ptr); slen = strlen(str); getNext(); while (q--) { x = in(); y = in(); int ans = KMP(); printf("%d\n", ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n, m, q; string a, b; cin >> n >> m >> q >> a >> b; vector<int> v(n); for (int i = 0; i < n; i++) { bool good = true; if (i) v[i] = v[i - 1]; for (int j = 0; j < m; j++) { if (a[i + j] != b[j]) { good = false; break; } } if (good) v[i]++; } for (int i = 0, l, r; i < q; i++) { cin >> l >> r; l--, r--; if (r - l + 1 < m) { cout << 0 << endl; continue; } r -= m - 1; cout << (r >= 0 ? v[r] : 0) - (l > 0 ? v[l - 1] : 0) << 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; int main() { int n, m, q; string a, b; cin >> n >> m >> q >> a >> b; vector<int> v(n); for (int i = 0; i < n; i++) { bool good = true; if (i) v[i] = v[i - 1]; for (int j = 0; j < m; j++) { if (a[i + j] != b[j]) { good = false; break; } } if (good) v[i]++; } for (int i = 0, l, r; i < q; i++) { cin >> l >> r; l--, r--; if (r - l + 1 < m) { cout << 0 << endl; continue; } r -= m - 1; cout << (r >= 0 ? v[r] : 0) - (l > 0 ? v[l - 1] : 0) << endl; } } ```
#include <bits/stdc++.h> using namespace std; char a[1005], b[1005], c[1005]; int p[1005]; int n, m, q; void pipei() { int l, r, j = 0; scanf("%d%d", &l, &r); int lb = m; int la = r - l + 1; for (int i = l; i <= r; i++) c[i - l + 1] = a[i - 1]; for (int i = 2; i <= lb; i++) { while (j > 0 && b[i] != b[j + 1]) j = p[j]; if (b[j + 1] == b[i]) j++; p[i] = j; } j = 0; int ans = 0; for (int i = 1; i <= la; i++) { while (j > 0 && b[j + 1] != c[i]) j = p[j]; if (b[j + 1] == c[i]) j++; if (j == lb) ans++; } printf("%d\n", ans); } int main() { scanf("%d%d%d", &n, &m, &q); scanf("%s%s", a, b + 1); memset(p, 0, sizeof(p)); while (q--) { pipei(); } 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; char a[1005], b[1005], c[1005]; int p[1005]; int n, m, q; void pipei() { int l, r, j = 0; scanf("%d%d", &l, &r); int lb = m; int la = r - l + 1; for (int i = l; i <= r; i++) c[i - l + 1] = a[i - 1]; for (int i = 2; i <= lb; i++) { while (j > 0 && b[i] != b[j + 1]) j = p[j]; if (b[j + 1] == b[i]) j++; p[i] = j; } j = 0; int ans = 0; for (int i = 1; i <= la; i++) { while (j > 0 && b[j + 1] != c[i]) j = p[j]; if (b[j + 1] == c[i]) j++; if (j == lb) ans++; } printf("%d\n", ans); } int main() { scanf("%d%d%d", &n, &m, &q); scanf("%s%s", a, b + 1); memset(p, 0, sizeof(p)); while (q--) { pipei(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, m, q, l, r; char s[1005], t[1005]; int sum[1005]; int flag[1005]; int main() { scanf("%d%d%d", &n, &m, &q); scanf("%s", &s); scanf("%s", &t); sum[0] = 0; for (int i = 0; i < n - m + 1; i++) { int now = 1; for (int j = 0; j < m; j++) { if (s[i + j] != t[j]) { now = 0; break; } } flag[i] = now; sum[i + 1] = sum[i] + flag[i]; } for (int i = max(0, n - m + 1); i < n; i++) { sum[i + 1] = sum[i]; } for (int i = 0; i < q; i++) { scanf("%d%d", &l, &r); if (r - m + 1 >= l - 1) printf("%d\n", sum[r - m + 1] - sum[l - 1]); else printf("0\n"); } }
### 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; char s[1005], t[1005]; int sum[1005]; int flag[1005]; int main() { scanf("%d%d%d", &n, &m, &q); scanf("%s", &s); scanf("%s", &t); sum[0] = 0; for (int i = 0; i < n - m + 1; i++) { int now = 1; for (int j = 0; j < m; j++) { if (s[i + j] != t[j]) { now = 0; break; } } flag[i] = now; sum[i + 1] = sum[i] + flag[i]; } for (int i = max(0, n - m + 1); i < n; i++) { sum[i + 1] = sum[i]; } for (int i = 0; i < q; i++) { scanf("%d%d", &l, &r); if (r - m + 1 >= l - 1) printf("%d\n", sum[r - m + 1] - sum[l - 1]); else printf("0\n"); } } ```
#include <bits/stdc++.h> using namespace std; long long n, m, q, l, r; long long arr[10000]; char s[10000], t[10000]; int main() { cin >> n >> m >> q; cin >> s + 1 >> t + 1; for (long long i = 1; i <= n - m + 1; i++) { int acc = 1; for (int j = 1; j <= m; j++) if (acc == 1 && s[i + j - 1] != t[j]) acc = 0; arr[i] = arr[i - 1] + acc; } while (q--) { cin >> l >> r; if (r >= l + m - 1) cout << arr[r - m + 1] - arr[l - 1] << "\n"; else cout << 0 << "\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; long long n, m, q, l, r; long long arr[10000]; char s[10000], t[10000]; int main() { cin >> n >> m >> q; cin >> s + 1 >> t + 1; for (long long i = 1; i <= n - m + 1; i++) { int acc = 1; for (int j = 1; j <= m; j++) if (acc == 1 && s[i + j - 1] != t[j]) acc = 0; arr[i] = arr[i - 1] + acc; } while (q--) { cin >> l >> r; if (r >= l + m - 1) cout << arr[r - m + 1] - arr[l - 1] << "\n"; else cout << 0 << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, m, q; string s, t; int st[1010], ed[1010]; int main() { ios_base::sync_with_stdio(false); cin >> n >> m >> q; cin >> s >> t; for (int i = 0; i < n - m + 1; i++) { bool ok = true; for (int j = 0; j < m; j++) { if (t[j] != s[i + j]) { ok = false; break; } } if (ok) { ed[i + m - 1]++; } } for (int i = 1; i < n; i++) { st[i] += st[i - 1]; ed[i] += ed[i - 1]; } for (int i = 0; i < q; i++) { int l, r; cin >> l >> r; l--, r--; int R = r; int L = l + m - 1; if (L <= R) { int res = ed[R]; if (L) res -= ed[L - 1]; cout << res << '\n'; } else cout << 0 << '\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; string s, t; int st[1010], ed[1010]; int main() { ios_base::sync_with_stdio(false); cin >> n >> m >> q; cin >> s >> t; for (int i = 0; i < n - m + 1; i++) { bool ok = true; for (int j = 0; j < m; j++) { if (t[j] != s[i + j]) { ok = false; break; } } if (ok) { ed[i + m - 1]++; } } for (int i = 1; i < n; i++) { st[i] += st[i - 1]; ed[i] += ed[i - 1]; } for (int i = 0; i < q; i++) { int l, r; cin >> l >> r; l--, r--; int R = r; int L = l + m - 1; if (L <= R) { int res = ed[R]; if (L) res -= ed[L - 1]; cout << res << '\n'; } else cout << 0 << '\n'; } } ```
#include <bits/stdc++.h> const long long N = 1e6 + 10; const long long mod = 998244353; using namespace std; long long w[1111]; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m, q; cin >> n >> m >> q; string a, b; cin >> a >> b; for (long long i = 0; i < n - m + 1; i++) { long long ff = 0; if (a[i] == b[0]) { for (long long j = i, k = 0; j < a.size() && k < b.size(); j++, k++) { if (a[j] != b[k]) { ff = 1; break; } } if (ff == 0) w[i + 1] = 1; } } for (long long i = 1; i <= 1010; i++) w[i] += w[i - 1]; while (q--) { long long l, r; cin >> l >> r; r -= m; r++; if (r < l) cout << 0 << endl; else cout << w[r] - w[l - 1] << endl; } 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> const long long N = 1e6 + 10; const long long mod = 998244353; using namespace std; long long w[1111]; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m, q; cin >> n >> m >> q; string a, b; cin >> a >> b; for (long long i = 0; i < n - m + 1; i++) { long long ff = 0; if (a[i] == b[0]) { for (long long j = i, k = 0; j < a.size() && k < b.size(); j++, k++) { if (a[j] != b[k]) { ff = 1; break; } } if (ff == 0) w[i + 1] = 1; } } for (long long i = 1; i <= 1010; i++) w[i] += w[i - 1]; while (q--) { long long l, r; cin >> l >> r; r -= m; r++; if (r < l) cout << 0 << endl; else cout << w[r] - w[l - 1] << endl; } return 0; } ```
#include <bits/stdc++.h> int a[1000]; using namespace std; int main() { int n, m, q, l, r, i, x; scanf("%d %d %d", &n, &m, &q); string s1, t; cin >> s1 >> t; for (i = 0; i <= n - m; i++) { if (s1.substr(i, m) == t) a[i] = 1; } while (q--) { x = 0; scanf("%d %d", &l, &r); for (i = l - 1; i <= r - m; i++) { if (a[i]) x++; } printf("%d\n", x); } 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 a[1000]; using namespace std; int main() { int n, m, q, l, r, i, x; scanf("%d %d %d", &n, &m, &q); string s1, t; cin >> s1 >> t; for (i = 0; i <= n - m; i++) { if (s1.substr(i, m) == t) a[i] = 1; } while (q--) { x = 0; scanf("%d %d", &l, &r); for (i = l - 1; i <= r - m; i++) { if (a[i]) x++; } printf("%d\n", x); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long INF = (1LL << 45LL); const long long MAXLL = 9223372036854775807LL; const unsigned long long MAXULL = 18446744073709551615LLU; const long long MOD = 1000000007; const long double DELTA = 0.000000001L; inline long long fmm(long long a, long long b, long long m = MOD) { long long r = 0; a %= m; b %= m; while (b > 0) { if (b & 1) { r += a; r %= m; } a += a; a %= m; b >>= 1; } return r % m; } inline long long fme(long long a, long long b, long long m = MOD) { long long r = 1; a %= m; while (b > 0) { if (b & 1) { r *= a; r %= m; } a *= a; a %= m; b >>= 1; } return r % m; } inline long long sfme(long long a, long long b, long long m = MOD) { long long r = 1; a %= m; while (b > 0) { if (b & 1) r = fmm(r, a, m); a = fmm(a, a, m); b >>= 1; } return r % m; } std::vector<long long> primes; long long primsiz; std::vector<long long> fact; std::vector<long long> invfact; inline void sieve(long long n) { long long i, j; std::vector<bool> a(n); a[0] = true; a[1] = true; for (i = 2; i * i < n; ++i) { if (!a[i]) { for (j = i * i; j < n; j += i) { a[j] = true; } } } for (i = 2; i < n; ++i) if (!a[i]) primes.push_back(i); primsiz = primes.size(); } inline void sieve() { long long n = 1010000, i, j, k = 0; std::vector<bool> a(n); primes.resize(79252); a[0] = a[1] = true; for (i = 2; (j = (i << 1)) < n; ++i) a[j] = true; for (i = 3; i * i < n; i += 2) { if (!a[i]) { k = (i << 1); for (j = i * i; j < n; j += k) a[j] = true; } } k = 0; for (i = 2; i < n; ++i) if (!a[i]) primes[k++] = i; primsiz = k; } inline bool isPrimeSmall(unsigned long long n) { if (((!(n & 1)) && n != 2) || (n < 2) || (n % 3 == 0 && n != 3)) return false; for (unsigned long long k = 1; 36 * k * k - 12 * k < n; ++k) if ((n % (6 * k + 1) == 0) || (n % (6 * k - 1) == 0)) return false; return true; } bool _p(unsigned long long a, unsigned long long n) { unsigned long long t, u, i, p, c = 0; u = n / 2, t = 1; while (!(u & 1)) { u /= 2; ++t; } p = fme(a, u, n); for (i = 1; i <= t; ++i) { c = (p * p) % n; if ((c == 1) && (p != 1) && (p != n - 1)) return 1; p = c; } if (c != 1) return 1; return 0; } inline bool isPrime(unsigned long long n) { if (((!(n & 1)) && n != 2) || (n < 2) || (n % 3 == 0 && n != 3)) return 0; if (n < 1373653) { for (unsigned long long k = 1; (((36 * k * k) - (12 * k)) < n); ++k) if ((n % (6 * k + 1) == 0) || (n % (6 * k - 1) == 0)) return 0; return 1; } if (n < 9080191) { if (_p(31, n) || _p(73, n)) return 0; return 1; } if (_p(2, n) || _p(7, n) || _p(61, n)) return 0; return 1; } unsigned long long nCk(long long n, long long k, unsigned long long m = MOD) { if (k < 0 || k > n || n < 0) return 0; if (k == 0 || k == n) return 1; if (fact.size() >= (unsigned long long)n && isPrime(m)) { return (((fact[n] * invfact[k]) % m) * invfact[n - k]) % m; } unsigned long long i = 0, j = 0, a = 1; k = ((k) < (n - k) ? (k) : (n - k)); for (; i < (unsigned long long)k; ++i) { a = (a * (n - i)) % m; while (j < (unsigned long long)k && (a % (j + 1) == 0)) { a = a / (j + 1); ++j; } } while (j < (unsigned long long)k) { a = a / (j + 1); ++j; } return a % m; } void nCkInit(unsigned long long m = MOD) { long long i, mx = 1010000; fact.resize(mx + 1); invfact.resize(mx + 1); fact[0] = 1; for (i = 1; i <= mx; ++i) { fact[i] = (i * fact[i - 1]) % m; } invfact[mx] = fme(fact[mx], m - 2, m); for (i = mx - 1; i >= 0; --i) { invfact[i] = (invfact[i + 1] * (i + 1)) % m; } } template <class T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } void extGCD(long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1, y = 0; return; } long long x1, y1; extGCD(b, a % b, x1, y1); x = y1; y = x1 - (a / b) * y1; } inline void get(long long &x) { int n = 0; x = 0; char c = getchar_unlocked(); if (c == '-') n = 1; while (c < '0' || c > '9') { c = getchar_unlocked(); if (c == '-') n = 1; } while (c >= '0' && c <= '9') { x = (x << 3) + (x << 1) + c - '0'; c = getchar_unlocked(); } if (n) x = -x; } inline int get(char *p) { char c = getchar_unlocked(); int i = 0; while (c != '\n' && c != '\0' && c != ' ' && c != '\r' && c != EOF) { p[i++] = c; c = getchar_unlocked(); } p[i] = '\0'; return i; } inline void put(long long a) { int n = (a < 0 ? 1 : 0); if (n) a = -a; char b[20]; int i = 0; do { b[i++] = a % 10 + '0'; a /= 10; } while (a); if (n) putchar_unlocked('-'); i--; while (i >= 0) putchar_unlocked(b[i--]); putchar_unlocked(' '); } inline void putln(long long a) { int n = (a < 0 ? 1 : 0); if (n) a = -a; char b[20]; int i = 0; do { b[i++] = a % 10 + '0'; a /= 10; } while (a); if (n) putchar_unlocked('-'); i--; while (i >= 0) putchar_unlocked(b[i--]); putchar_unlocked('\n'); } const int K = 3; std::vector<std::vector<long long> > mul(std::vector<std::vector<long long> > a, std::vector<std::vector<long long> > b, unsigned long long m = MOD) { std::vector<std::vector<long long> > c(K, std::vector<long long>(K)); for (long long ii = 0; ii < (K); ++ii) for (long long jj = 0; jj < (K); ++jj) for (long long kk = 0; kk < (K); ++kk) c[ii][jj] = (c[ii][jj] + a[ii][kk] * b[kk][jj]) % m; return c; } std::vector<std::vector<long long> > fme(std::vector<std::vector<long long> > a, unsigned long long n, unsigned long long m = MOD) { if (n == 1) return a; if (n & 1) return mul(a, fme(a, n - 1, m), m); std::vector<std::vector<long long> > x = fme(a, n / 2, m); return mul(x, x, m); } long long a[1010]; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL); long long n = 0, m = 0, maxx = 0, minn = 0, curr = 0, k = 0, num = 0, siz = 0, n1 = 0, n2 = 0, n3 = 0, n4 = 0, ind = 0; long long root = 0, sum = 0, diff = 0, q = 0, choice = 0, d = 0, len = 0, begg = 0, endd = 0, pos = 0, cnt = 0, lo = 0, hi = 0, mid = 0, ans = 0; bool flag = false; std::string s, t, s1, s2, s3, str; char ch, ch1, ch2, ch3, *ptr; double dub = 0; cin >> n >> m >> q; cin >> s; cin >> t; for (long long i = 0; i < (n); ++i) { a[i + 1] = a[i]; if (i < n - m + 1) { flag = true; for (long long j = 0; j < (m); ++j) { if (s[i + j] != t[j]) flag = false; } if (flag) a[i + 1]++; } } for (long long i = 0; i < (q); ++i) { cin >> n1 >> n2; cout << (a[max(n1 - 1, n2 - m + 1)] - a[n1 - 1]) << '\n'; } 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; const long long INF = (1LL << 45LL); const long long MAXLL = 9223372036854775807LL; const unsigned long long MAXULL = 18446744073709551615LLU; const long long MOD = 1000000007; const long double DELTA = 0.000000001L; inline long long fmm(long long a, long long b, long long m = MOD) { long long r = 0; a %= m; b %= m; while (b > 0) { if (b & 1) { r += a; r %= m; } a += a; a %= m; b >>= 1; } return r % m; } inline long long fme(long long a, long long b, long long m = MOD) { long long r = 1; a %= m; while (b > 0) { if (b & 1) { r *= a; r %= m; } a *= a; a %= m; b >>= 1; } return r % m; } inline long long sfme(long long a, long long b, long long m = MOD) { long long r = 1; a %= m; while (b > 0) { if (b & 1) r = fmm(r, a, m); a = fmm(a, a, m); b >>= 1; } return r % m; } std::vector<long long> primes; long long primsiz; std::vector<long long> fact; std::vector<long long> invfact; inline void sieve(long long n) { long long i, j; std::vector<bool> a(n); a[0] = true; a[1] = true; for (i = 2; i * i < n; ++i) { if (!a[i]) { for (j = i * i; j < n; j += i) { a[j] = true; } } } for (i = 2; i < n; ++i) if (!a[i]) primes.push_back(i); primsiz = primes.size(); } inline void sieve() { long long n = 1010000, i, j, k = 0; std::vector<bool> a(n); primes.resize(79252); a[0] = a[1] = true; for (i = 2; (j = (i << 1)) < n; ++i) a[j] = true; for (i = 3; i * i < n; i += 2) { if (!a[i]) { k = (i << 1); for (j = i * i; j < n; j += k) a[j] = true; } } k = 0; for (i = 2; i < n; ++i) if (!a[i]) primes[k++] = i; primsiz = k; } inline bool isPrimeSmall(unsigned long long n) { if (((!(n & 1)) && n != 2) || (n < 2) || (n % 3 == 0 && n != 3)) return false; for (unsigned long long k = 1; 36 * k * k - 12 * k < n; ++k) if ((n % (6 * k + 1) == 0) || (n % (6 * k - 1) == 0)) return false; return true; } bool _p(unsigned long long a, unsigned long long n) { unsigned long long t, u, i, p, c = 0; u = n / 2, t = 1; while (!(u & 1)) { u /= 2; ++t; } p = fme(a, u, n); for (i = 1; i <= t; ++i) { c = (p * p) % n; if ((c == 1) && (p != 1) && (p != n - 1)) return 1; p = c; } if (c != 1) return 1; return 0; } inline bool isPrime(unsigned long long n) { if (((!(n & 1)) && n != 2) || (n < 2) || (n % 3 == 0 && n != 3)) return 0; if (n < 1373653) { for (unsigned long long k = 1; (((36 * k * k) - (12 * k)) < n); ++k) if ((n % (6 * k + 1) == 0) || (n % (6 * k - 1) == 0)) return 0; return 1; } if (n < 9080191) { if (_p(31, n) || _p(73, n)) return 0; return 1; } if (_p(2, n) || _p(7, n) || _p(61, n)) return 0; return 1; } unsigned long long nCk(long long n, long long k, unsigned long long m = MOD) { if (k < 0 || k > n || n < 0) return 0; if (k == 0 || k == n) return 1; if (fact.size() >= (unsigned long long)n && isPrime(m)) { return (((fact[n] * invfact[k]) % m) * invfact[n - k]) % m; } unsigned long long i = 0, j = 0, a = 1; k = ((k) < (n - k) ? (k) : (n - k)); for (; i < (unsigned long long)k; ++i) { a = (a * (n - i)) % m; while (j < (unsigned long long)k && (a % (j + 1) == 0)) { a = a / (j + 1); ++j; } } while (j < (unsigned long long)k) { a = a / (j + 1); ++j; } return a % m; } void nCkInit(unsigned long long m = MOD) { long long i, mx = 1010000; fact.resize(mx + 1); invfact.resize(mx + 1); fact[0] = 1; for (i = 1; i <= mx; ++i) { fact[i] = (i * fact[i - 1]) % m; } invfact[mx] = fme(fact[mx], m - 2, m); for (i = mx - 1; i >= 0; --i) { invfact[i] = (invfact[i + 1] * (i + 1)) % m; } } template <class T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } void extGCD(long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1, y = 0; return; } long long x1, y1; extGCD(b, a % b, x1, y1); x = y1; y = x1 - (a / b) * y1; } inline void get(long long &x) { int n = 0; x = 0; char c = getchar_unlocked(); if (c == '-') n = 1; while (c < '0' || c > '9') { c = getchar_unlocked(); if (c == '-') n = 1; } while (c >= '0' && c <= '9') { x = (x << 3) + (x << 1) + c - '0'; c = getchar_unlocked(); } if (n) x = -x; } inline int get(char *p) { char c = getchar_unlocked(); int i = 0; while (c != '\n' && c != '\0' && c != ' ' && c != '\r' && c != EOF) { p[i++] = c; c = getchar_unlocked(); } p[i] = '\0'; return i; } inline void put(long long a) { int n = (a < 0 ? 1 : 0); if (n) a = -a; char b[20]; int i = 0; do { b[i++] = a % 10 + '0'; a /= 10; } while (a); if (n) putchar_unlocked('-'); i--; while (i >= 0) putchar_unlocked(b[i--]); putchar_unlocked(' '); } inline void putln(long long a) { int n = (a < 0 ? 1 : 0); if (n) a = -a; char b[20]; int i = 0; do { b[i++] = a % 10 + '0'; a /= 10; } while (a); if (n) putchar_unlocked('-'); i--; while (i >= 0) putchar_unlocked(b[i--]); putchar_unlocked('\n'); } const int K = 3; std::vector<std::vector<long long> > mul(std::vector<std::vector<long long> > a, std::vector<std::vector<long long> > b, unsigned long long m = MOD) { std::vector<std::vector<long long> > c(K, std::vector<long long>(K)); for (long long ii = 0; ii < (K); ++ii) for (long long jj = 0; jj < (K); ++jj) for (long long kk = 0; kk < (K); ++kk) c[ii][jj] = (c[ii][jj] + a[ii][kk] * b[kk][jj]) % m; return c; } std::vector<std::vector<long long> > fme(std::vector<std::vector<long long> > a, unsigned long long n, unsigned long long m = MOD) { if (n == 1) return a; if (n & 1) return mul(a, fme(a, n - 1, m), m); std::vector<std::vector<long long> > x = fme(a, n / 2, m); return mul(x, x, m); } long long a[1010]; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL); long long n = 0, m = 0, maxx = 0, minn = 0, curr = 0, k = 0, num = 0, siz = 0, n1 = 0, n2 = 0, n3 = 0, n4 = 0, ind = 0; long long root = 0, sum = 0, diff = 0, q = 0, choice = 0, d = 0, len = 0, begg = 0, endd = 0, pos = 0, cnt = 0, lo = 0, hi = 0, mid = 0, ans = 0; bool flag = false; std::string s, t, s1, s2, s3, str; char ch, ch1, ch2, ch3, *ptr; double dub = 0; cin >> n >> m >> q; cin >> s; cin >> t; for (long long i = 0; i < (n); ++i) { a[i + 1] = a[i]; if (i < n - m + 1) { flag = true; for (long long j = 0; j < (m); ++j) { if (s[i + j] != t[j]) flag = false; } if (flag) a[i + 1]++; } } for (long long i = 0; i < (q); ++i) { cin >> n1 >> n2; cout << (a[max(n1 - 1, n2 - m + 1)] - a[n1 - 1]) << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long N = 2e5 + 5, MOD = 1000000007; long long n, m, q; string s, t; vector<long long> v; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m >> q >> s >> t; for (long long i = 0; i < n; i++) { if ((m + i) > n) break; string temp; temp.clear(); for (long long j = i; j < (i + m); j++) { temp += s[j]; } if (temp == t) v.push_back(i); } v.push_back(1e7); while (q--) { long long l, r; cin >> l >> r; l--; r--; if ((r - l) < (m - 1)) { cout << 0 << endl; continue; } auto it1 = lower_bound(v.begin(), v.end(), l); auto it2 = lower_bound(v.begin(), v.end(), r - m + 1); if (it1 == v.end()) { cout << 0; continue; } if ((it2 == v.end()) || (*it2 != (r - m + 1))) it2--; cout << it2 - it1 + 1 << 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; const long long N = 2e5 + 5, MOD = 1000000007; long long n, m, q; string s, t; vector<long long> v; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m >> q >> s >> t; for (long long i = 0; i < n; i++) { if ((m + i) > n) break; string temp; temp.clear(); for (long long j = i; j < (i + m); j++) { temp += s[j]; } if (temp == t) v.push_back(i); } v.push_back(1e7); while (q--) { long long l, r; cin >> l >> r; l--; r--; if ((r - l) < (m - 1)) { cout << 0 << endl; continue; } auto it1 = lower_bound(v.begin(), v.end(), l); auto it2 = lower_bound(v.begin(), v.end(), r - m + 1); if (it1 == v.end()) { cout << 0; continue; } if ((it2 == v.end()) || (*it2 != (r - m + 1))) it2--; cout << it2 - it1 + 1 << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, m, q; int nxt[100001]; char s[100001], t[100001]; int temp[100001]; int l, r; bool check(int u) { for (int i = 0; i < m; ++i) { if (s[u + i] != t[i]) return 0; } return 1; } void pre_do() { for (int i = 1; i <= n - m + 1; i++) { temp[i] += check(i); } } void putin() { cin >> n >> m >> q; scanf("%s%s", s + 1, t); } int main() { putin(); pre_do(); for (int i = 1; i <= n; i++) { temp[i] += temp[i - 1]; } for (int i = 1; i <= q; i++) { cin >> l >> r; if (r - l + 1 < m) cout << 0 << endl; else cout << temp[r - m + 1] - temp[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; int n, m, q; int nxt[100001]; char s[100001], t[100001]; int temp[100001]; int l, r; bool check(int u) { for (int i = 0; i < m; ++i) { if (s[u + i] != t[i]) return 0; } return 1; } void pre_do() { for (int i = 1; i <= n - m + 1; i++) { temp[i] += check(i); } } void putin() { cin >> n >> m >> q; scanf("%s%s", s + 1, t); } int main() { putin(); pre_do(); for (int i = 1; i <= n; i++) { temp[i] += temp[i - 1]; } for (int i = 1; i <= q; i++) { cin >> l >> r; if (r - l + 1 < m) cout << 0 << endl; else cout << temp[r - m + 1] - temp[l - 1] << endl; } } ```
#include <bits/stdc++.h> using namespace std; const int maxx = 1000004; struct XXX { long long x, y; } xx[maxx]; bool my(XXX a, XXX b) { return a.x < b.x; } bool mt(long long a, long long b) { return a > b; } long long a[maxx], b[maxx]; long long read() { long long k; scanf("%lld", &k); return k; } long long n, m; int f[maxx]; int ans; char z[maxx], c[maxx]; void getfail(char* s) { f[0] = f[1] = 0; ; int len = strlen(s); for (int i = 1; i < len; i++) { int j = f[i]; while (j && s[j] != s[i]) j = f[j]; if (s[j] == s[i]) f[i + 1] = j + 1; else f[i + 1] = 0; } } int Ans[maxx]; void Kmp(char* T, char* S) { int n = strlen(T), m = strlen(S); getfail(S); int j = 0; for (int i = 0; i <= n; i++) { while (j && S[j] != T[i]) j = f[j]; if (S[j] == T[i]) j++; if (j == m) { ans++; j = f[j]; } Ans[i] = ans; } } int main() { int q; n = read(); m = read(); q = read(); scanf("%s", z); scanf("%s", c); Kmp(z, c); for (int i = 1; i <= q; i++) { int x = read() - 1; int y = read() - 1; x = x + m - 1; if (x > y) cout << 0 << endl; else cout << max(Ans[y] - Ans[x - 1], 0) << 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; const int maxx = 1000004; struct XXX { long long x, y; } xx[maxx]; bool my(XXX a, XXX b) { return a.x < b.x; } bool mt(long long a, long long b) { return a > b; } long long a[maxx], b[maxx]; long long read() { long long k; scanf("%lld", &k); return k; } long long n, m; int f[maxx]; int ans; char z[maxx], c[maxx]; void getfail(char* s) { f[0] = f[1] = 0; ; int len = strlen(s); for (int i = 1; i < len; i++) { int j = f[i]; while (j && s[j] != s[i]) j = f[j]; if (s[j] == s[i]) f[i + 1] = j + 1; else f[i + 1] = 0; } } int Ans[maxx]; void Kmp(char* T, char* S) { int n = strlen(T), m = strlen(S); getfail(S); int j = 0; for (int i = 0; i <= n; i++) { while (j && S[j] != T[i]) j = f[j]; if (S[j] == T[i]) j++; if (j == m) { ans++; j = f[j]; } Ans[i] = ans; } } int main() { int q; n = read(); m = read(); q = read(); scanf("%s", z); scanf("%s", c); Kmp(z, c); for (int i = 1; i <= q; i++) { int x = read() - 1; int y = read() - 1; x = x + m - 1; if (x > y) cout << 0 << endl; else cout << max(Ans[y] - Ans[x - 1], 0) << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1021; int n, m, q, ans[N]; string s, t; int main() { cin >> n >> m >> q; cin >> s >> t; for (int i = 0; i + m <= n; i++) if (s.substr(i, m) == t) ans[i + 1]++; for (int i = 2; i <= n; i++) ans[i] += ans[i - 1]; while (q--) { int a, b; cin >> a >> b; b = b - m + 1; printf("%d\n", max(0, ans[max(0, b)] - ans[a - 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; const int N = 1021; int n, m, q, ans[N]; string s, t; int main() { cin >> n >> m >> q; cin >> s >> t; for (int i = 0; i + m <= n; i++) if (s.substr(i, m) == t) ans[i + 1]++; for (int i = 2; i <= n; i++) ans[i] += ans[i - 1]; while (q--) { int a, b; cin >> a >> b; b = b - m + 1; printf("%d\n", max(0, ans[max(0, b)] - ans[a - 1])); } } ```
#include <bits/stdc++.h> using namespace std; int n, m, q, a[2010], sum[2010]; string s, t; vector<pair<int, int> > v; int main() { cin >> n >> m >> q; cin >> s; cin >> t; for (int i = 0; i < n && i + m - 1 < n; i++) { bool found = true; for (int j = 0; j < m && j + i < n; j++) { if (s[i + j] != t[j]) { found = false; break; } } if (found) { v.push_back({i + 1, i + m}); } } for (int i = 1; i <= q; i++) { int l, r; cin >> l >> r; int ans = 0; for (auto interval : v) { if (interval.first >= l && interval.second <= r) ans++; } cout << ans << 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> using namespace std; int n, m, q, a[2010], sum[2010]; string s, t; vector<pair<int, int> > v; int main() { cin >> n >> m >> q; cin >> s; cin >> t; for (int i = 0; i < n && i + m - 1 < n; i++) { bool found = true; for (int j = 0; j < m && j + i < n; j++) { if (s[i + j] != t[j]) { found = false; break; } } if (found) { v.push_back({i + 1, i + m}); } } for (int i = 1; i <= q; i++) { int l, r; cin >> l >> r; int ans = 0; for (auto interval : v) { if (interval.first >= l && interval.second <= r) ans++; } cout << ans << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; char s1[1005]; char s2[1005]; int ans[1005]; int main() { int n, m, q, r, l, len1, len2, an, count = 0; bool flag; scanf("%d %d %d", &n, &m, &q); scanf("%s", s1); scanf("%s", s2); len1 = strlen(s1); len2 = strlen(s2); for (int i = 0; i < len1 - len2 + 1; i++) { flag = 1; for (int j = 0; j < len2; j++) { if (s1[i + j] != s2[j]) { flag = 0; break; } } if (flag) { count++; } ans[i + 1] = count; } for (int i = 0; i < 10; i++) { } while (q--) { scanf("%d %d", &l, &r); if (r - l + 1 < len2) { cout << 0 << endl; } else { cout << ans[r - len2 + 1] - ans[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; char s1[1005]; char s2[1005]; int ans[1005]; int main() { int n, m, q, r, l, len1, len2, an, count = 0; bool flag; scanf("%d %d %d", &n, &m, &q); scanf("%s", s1); scanf("%s", s2); len1 = strlen(s1); len2 = strlen(s2); for (int i = 0; i < len1 - len2 + 1; i++) { flag = 1; for (int j = 0; j < len2; j++) { if (s1[i + j] != s2[j]) { flag = 0; break; } } if (flag) { count++; } ans[i + 1] = count; } for (int i = 0; i < 10; i++) { } while (q--) { scanf("%d %d", &l, &r); if (r - l + 1 < len2) { cout << 0 << endl; } else { cout << ans[r - len2 + 1] - ans[l - 1] << endl; } } return 0; } ```