Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
1016_B. Segment Occurrences
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.
2
8
#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; }
CPP
1016_B. Segment Occurrences
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.
2
8
#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; } }
CPP
1016_B. Segment Occurrences
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.
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; public class B { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader jin = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); jin.next(); int m = Integer.parseInt(jin.next()); int n = Integer.parseInt(jin.next()); String str1 = jin.next(); String str2 = jin.next(); int sumArr[] = new int[str1.length()]; for (int i = 0; i <= str1.length() - m; ++i) { if (str1.substring(i, i + m).equals(str2)) sumArr[i] = 1; } for (int i = 0; i < n; ++i) { int from = Integer.parseInt(jin.next()); int to = Integer.parseInt(jin.next()); int ret = 0; for (int j = from - 1; j < to; ++j) { if (to - j >= m) ret += sumArr[j]; } System.out.println(ret); } out.close(); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
JAVA
1016_B. Segment Occurrences
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.
2
8
#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; }
CPP
1016_B. Segment Occurrences
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.
2
8
#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; }
CPP
1016_B. Segment Occurrences
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.
2
8
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); BSegmentOccurrences solver = new BSegmentOccurrences(); solver.solve(1, in, out); out.close(); } static class BSegmentOccurrences { int n; int m; int q; char[] first; char[] second; int[] prefix; public void readInput(Scanner sc) { n = sc.nextInt(); m = sc.nextInt(); q = sc.nextInt(); first = sc.next().toCharArray(); second = sc.next().toCharArray(); } public void solve(int testNumber, Scanner sc, PrintWriter pw) { int tc = 1; while (tc-- > 0) { readInput(sc); prefix = new int[n]; for (int i = 0; i + m - 1 < n; i++) { boolean f = true; for (int j = 0; j < m; j++) f &= first[i + j] == second[j]; if (f) prefix[i]++; if (i != 0) prefix[i] += prefix[i - 1]; } for (int i = 0; i < q; i++) { int l = sc.nextInt() - 1, r = sc.nextInt() - 1; if (r - l + 1 < m) pw.println(0); else { int pre = prefix[r - m + 1]; if (l - 1 >= 0) pre -= prefix[l - 1]; pw.println(pre); } } } } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() { try { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } catch (Exception e) { throw new RuntimeException(e); } } public int nextInt() { return Integer.parseInt(next()); } } }
JAVA
1016_B. Segment Occurrences
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.
2
8
n, m, q= map(int,input().split()) s = str(input()) t = str(input()) ans = list() if n >= m: buf = "" for i in range(0, m-1): buf += s[i] for i in range(m-1, n): buf += s[i] ok = 1 for j in range(m): ok &= buf[i - m + 1 + j] == t[j] ans.append(ok) while len(ans) < n: ans.append(0) pref = list() for i in range(n): if i == 0: pref.append(ans[0]) else: pref.append(pref[i-1] + ans[i]) for _ in range(q): l, r = map(int, input().split()) l -= 1 r -= 1 if r - l + 1 < m: print(0) else: if l == 0: print(pref[r-m+1]) else: print(pref[r-m+1] - pref[l-1])
PYTHON3
1016_B. Segment Occurrences
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.
2
8
#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; } }
CPP
1016_B. Segment Occurrences
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.
2
8
#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; }
CPP
1016_B. Segment Occurrences
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.
2
8
import java.util.Scanner; public class SegmentOcc { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(),m=sc.nextInt(),q=sc.nextInt(); String s=new String(sc.next()); String t=new String(sc.next()); int a[]=new int[1000]; int ai=0; String x=new String(s); int ind=0; for(int i=0;i<n;i++) { int p=x.indexOf(t); if(p!=-1) a[ai++]=p+ind+1; x=x.substring(p+1); ind+=p+1; } int b[]=new int[q]; for(int i=0;i<q;i++) { int l=sc.nextInt(),r=sc.nextInt(); for(int j=0;j<ai&&a[j]<=r;j++) { if(a[j]>=l&&(a[j]+m-1)<=r) b[i]++; } } for(int i=0;i<q;i++) System.out.println(b[i]); } }
JAVA
1016_B. Segment Occurrences
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.
2
8
#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"); } }
CPP
1016_B. Segment Occurrences
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.
2
8
n,m,k=map(int,input().split()) a=list(input()) b=list(input()) l =[] j=0 while j<n-m+1: if j+m <= n: if a[j:j+m]==b: l.append(j) else: break j += 1 for i in range(k): x,y = map(int,input().split()) c=0 x-=1 y-=1 for i in range(len(l)): if l[i]>=x and l[i]+m-1<=y: c+=1 print(c)
PYTHON3
1016_B. Segment Occurrences
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.
2
8
#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; }
CPP
1016_B. Segment Occurrences
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.
2
8
n,m,q = (map(int,input().split(' '))) text = list(input()) patt = list(input()) cnt=[0 for i in range(n)] if(m>n): for i in range(q): l,r=(map(int,input().split(' '))) print(0) else: for i in range(n): if(i+m>n): break if(text[i:i+m]==patt): cnt[i]+=1 for i in range(1,n): cnt[i] = cnt[i]+cnt[i-1] for i in range(q): l,r=(map(int,input().split(' '))) l-=1 r-=1 r = (r-m+1) if(l>r): print(0) elif(l!=0): print(cnt[r]-cnt[l-1]) else: print(cnt[r])
PYTHON3
1016_B. Segment Occurrences
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.
2
8
import sys import io stream_enable = 0 inpstream = """ 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 """ if stream_enable: sys.stdin = io.StringIO(inpstream) input() def inpmap(): return list(map(int, input().split())) n, m, q = inpmap() a = input() b = input() s = [0] * n s[0] = int(a.startswith(b)) for i in range(1, n): s[i] = s[i - 1] + int(a[i:i+m] == b) for i in range(q): x, y = inpmap() r = (s[y - m] if y - m >= 0 else 0) - (s[x - 2] if x - 2 >= 0 else 0) print(max(r, 0))
PYTHON3
1016_B. Segment Occurrences
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.
2
8
#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'; } }
CPP
1016_B. Segment Occurrences
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.
2
8
N,M,Q = [int(x) for x in input().split()] S = input() #length N T = input() #length M A = [] pref = [] for i in range(N): A.append(0) pref.append(0) for i in range(N): if S[i:i+M] == T: A[i] = 1 if i > 0: pref[i] = pref[i-1] + A[i] else: pref[i] = A[i] for i in range(Q): l,r = [int(x) for x in input().split()] l -= 1 r -= 1 if r-l+1 >= M: if r-M+1 >= 0: if l-1>=0: print(pref[r-M+1]-pref[l-1]) else: print(pref[r-M+1]) else: print(0) else: print(0)
PYTHON3
1016_B. Segment Occurrences
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.
2
8
""" a=mt.floor(n/2)+mt.floor(n/3)+mt.floor(n/4) if a>n: print(int(a)) else: print(n) 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 """ n,m,k=map(int,input().split()) a=list(input()) b=list(input()) l =[] j=0 while j<n-m+1: if j+m <= n: if a[j:j+m]==b: l.append(j) else: break j += 1 for i in range(k): x,y = map(int,input().split()) c=0 x-=1 y-=1 for i in range(len(l)): if l[i]>=x and l[i]+m-1<=y: c+=1 print(c)
PYTHON3
1016_B. Segment Occurrences
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.
2
8
import java.util.Arrays; import java.util.Scanner; public class Segment_Occurrences { static boolean dp[]; static int tt[]; public static void main(String[] args) { Scanner sc = new Scanner (System.in); int n=sc.nextInt(),m=sc.nextInt(),q=sc.nextInt(); String s = sc.next(),t=sc.next(); dp = new boolean [n+1]; for (int i = 0; i < n - t.length() + 1; i++) { if (s.substring(i, i + t.length()).equals(t)) { dp[i] = true; } } for (int i = 0; i < q; i++) { int start = sc.nextInt() - 1; int end = sc.nextInt() - 1; int count = 0; for (int j = start; j <= end - t.length() + 1; j++) { if (dp[j]) { count++; } } System.out.println(count); } } }
JAVA
1016_B. Segment Occurrences
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.
2
8
try: while True: n, m, q = list(map(int, input().split())) str1 = input() str2 = input() cnt = [0] * (n+1) for l in range(n): #print(str1[l:l+m]) if str1[l:min(l+m, n)] == str2: cnt[l+1] = cnt[l] + 1 else: cnt[l+1] = cnt[l] for i in range(q): l, r = list(map(int, input().split())) print(cnt[max(l-1, r-m+1)] - cnt[l-1]) except Exception as e: pass
PYTHON3
1016_B. Segment Occurrences
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.
2
8
#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; }
CPP
1016_B. Segment Occurrences
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.
2
8
#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; }
CPP
1016_B. Segment Occurrences
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.
2
8
n,m,q=map(int,input().split()) s=input() t=input() x=0 dp1=[0 for i in range(n)] while x<n: if s[x-m+1:x+1]==t: dp1[x]=1 else: dp1[x]=0 x+=1 dp=[[0 for i in range(n)] for j in range(n)] for i in range(n): acum=0 for j in range(i,n): if dp1[j]!=0 and j-m+1>=i: acum+=dp1[j] dp[i][j]=acum ans="" for i in range(q): l,r=map(int,input().split()) ans+=str(dp[l-1][r-1])+chr(10) print(ans)
PYTHON3
1016_B. Segment Occurrences
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.
2
8
#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; }
CPP
1016_B. Segment Occurrences
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.
2
8
#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; }
CPP
1016_B. Segment Occurrences
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.
2
8
#!/usr/bin/python2 # -*- coding: utf-8 -*- import sys def rl(proc=None): if proc is not None: return proc(sys.stdin.readline()) else: return sys.stdin.readline().rstrip() def srl(proc=None): if proc is not None: return map(proc, rl().split()) else: return rl().split() def main(): _, _, q = srl(int) s = rl() t = rl() A = [int(s[i:].startswith(t)) for i in xrange(len(s))] A.extend([0] * 10) B = [0] for a in A: B.append(B[-1] + a) for _ in xrange(q): l, r = srl(int) r -= len(t) - 1 l -= 1 print B[r] - B[l] if r > l else 0 if __name__ == '__main__': main()
PYTHON
1016_B. Segment Occurrences
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.
2
8
from sys import stdin,stdout from bisect import bisect_left def main(): n,m,q=map(int,stdin.readline().split( )) s=stdin.readline(); t=stdin.readline(); start=[];end=[] for i in range(n-m+1): j=0 while j<m and s[i+j]==t[j]: j+=1 if j==m: start.append(i) end.append(i+j-1) len_start=len(start) len_end=len(end) for _ in range(q): l,r=map(int,stdin.readline().split( )) l-=1;r-=1 if not start: stdout.write("%d\n"%(0)) continue x=bisect_left(start,l) y=bisect_left(end,r) if x==len_end: stdout.write("%d\n"%(0)) continue if y==len_end: stdout.write("%d\n"%max(0,y-x)) continue if end[y]==r: stdout.write("%d\n"%max(0,y-x+1)) continue else: stdout.write("%d\n"%max(0,y-x)) main()
PYTHON3
1016_B. Segment Occurrences
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.
2
8
import java.io.*; import java.util.*; /** * @author baito */ @SuppressWarnings("unchecked") public class Main { static StringBuilder sb = new StringBuilder(); static FastScanner sc = new FastScanner(System.in); static int INF = 1234567890; static long LINF = 123456789123456789L; static long MINF = -123456789123456789L; static long MOD = 1000000007; static int[] y4 = {0, 1, 0, -1}; static int[] x4 = {1, 0, -1, 0}; static int[] y8 = {0, 1, 0, -1, -1, 1, 1, -1}; static int[] x8 = {1, 0, -1, 0, 1, -1, 1, -1}; static long[] Fa;//factorial static boolean[] isPrime; static int[] primes; static char[][] map; static int N, M, K; static int[] A; public static void main(String[] args) { //longを忘れるなオーバーフローするぞ N = sc.nextInt(); M = sc.nextInt(); int Q = sc.nextInt(); if (N < M) { for (int q = 0; q < Q; q++) { System.out.println(0); } return; } String s = sc.next(); String t = sc.next(); int[] rui = new int[s.length() + 1]; int lastIndex = -1; TreeSet<Integer> start = new TreeSet<>(); for (int i = 1; i < s.length() + 1; i++) { rui[i] += rui[i - 1]; int imd = s.substring(0, i).lastIndexOf(t); if (imd != lastIndex) { start.add(imd); rui[i]++; } lastIndex = imd; } for (int q = 0; q < Q; q++) { int l = sc.nextInt() - 1; int r = sc.nextInt() - 1; long res = rui[r + 1]; if (r - l + 1 < M) { System.out.println("0"); continue; } res -= start.headSet(l).size(); System.out.println(res); } } public static long sqrt(long v) { long res = (long) Math.sqrt(v); while (res * res > v) res--; return res; } public static int upper0(int a) { if (a < 0) return 0; return a; } public static long upper0(long a) { if (a < 0) return 0; return a; } public static Integer[] toIntegerArray(int[] ar) { Integer[] res = new Integer[ar.length]; for (int i = 0; i < ar.length; i++) { res[i] = ar[i]; } return res; } //k個の次の組み合わせをビットで返す 大きさに上限はない 110110 -> 111001 public static int nextCombSizeK(int comb, int k) { int x = comb & -comb; //最下位の1 int y = comb + x; //連続した下の1を繰り上がらせる return ((comb & ~y) / x >> 1) | y; } public static int keta(long num) { int res = 0; while (num > 0) { num /= 10; res++; } return res; } public static long getHashKey(int a, int b) { return (long) a << 32 | b; } public static boolean isOutofIndex(int x, int y) { if (x < 0 || y < 0) return true; if (map[0].length <= x || map.length <= y) return true; return false; } public static void setPrimes() { int n = 100001; isPrime = new boolean[n]; List<Integer> prs = new ArrayList<>(); Arrays.fill(isPrime, true); isPrime[0] = isPrime[1] = false; for (int i = 2; i * i <= n; i++) { if (!isPrime[i]) continue; prs.add(i); for (int j = i * 2; j < n; j += i) { isPrime[j] = false; } } primes = new int[prs.size()]; for (int i = 0; i < prs.size(); i++) primes[i] = prs.get(i); } public static void revSort(int[] a) { Arrays.sort(a); reverse(a); } public static void revSort(long[] a) { Arrays.sort(a); reverse(a); } public static int[][] copy(int[][] ar) { int[][] nr = new int[ar.length][ar[0].length]; for (int i = 0; i < ar.length; i++) for (int j = 0; j < ar[0].length; j++) nr[i][j] = ar[i][j]; return nr; } /** * <h1>指定した値以上の先頭のインデクスを返す</h1> * <p>配列要素が0のときは、0が返る。</p> * * @return<b>int</b> : 探索した値以上で、先頭になるインデクス * 値が無ければ、挿入できる最小のインデックス */ public static int lowerBound(final int[] arr, final int value) { int low = 0; int high = arr.length; int mid; while (low < high) { mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策) if (arr[mid] < value) { low = mid + 1; } else { high = mid; } } return low; } /** * <h1>指定した値より大きい先頭のインデクスを返す</h1> * <p>配列要素が0のときは、0が返る。</p> * * @return<b>int</b> : 探索した値より上で、先頭になるインデクス * 値が無ければ、挿入できる最小のインデックス */ public static int upperBound(final int[] arr, final int value) { int low = 0; int high = arr.length; int mid; while (low < high) { mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策) if (arr[mid] <= value) { low = mid + 1; } else { high = mid; } } return low; } /** * <h1>指定した値以上の先頭のインデクスを返す</h1> * <p>配列要素が0のときは、0が返る。</p> * * @return<b>int</b> : 探索した値以上で、先頭になるインデクス * 値がなければ挿入できる最小のインデックス */ public static long lowerBound(final long[] arr, final long value) { int low = 0; int high = arr.length; int mid; while (low < high) { mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策) if (arr[mid] < value) { low = mid + 1; } else { high = mid; } } return low; } /** * <h1>指定した値より大きい先頭のインデクスを返す</h1> * <p>配列要素が0のときは、0が返る。</p> * * @return<b>int</b> : 探索した値より上で、先頭になるインデクス * 値がなければ挿入できる最小のインデックス */ public static long upperBound(final long[] arr, final long value) { int low = 0; int high = arr.length; int mid; while (low < high) { mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策) if (arr[mid] <= value) { low = mid + 1; } else { high = mid; } } return low; } //次の順列に書き換える、最大値ならfalseを返す public static boolean nextPermutation(int A[]) { int len = A.length; int pos = len - 2; for (; pos >= 0; pos--) { if (A[pos] < A[pos + 1]) break; } if (pos == -1) return false; //posより大きい最小の数を二分探索 int ok = pos + 1; int ng = len; while (Math.abs(ng - ok) > 1) { int mid = (ok + ng) / 2; if (A[mid] > A[pos]) ok = mid; else ng = mid; } swap(A, pos, ok); reverse(A, pos + 1, len - 1); return true; } //次の順列に書き換える、最小値ならfalseを返す public static boolean prevPermutation(int A[]) { int len = A.length; int pos = len - 2; for (; pos >= 0; pos--) { if (A[pos] > A[pos + 1]) break; } if (pos == -1) return false; //posより小さい最大の数を二分探索 int ok = pos + 1; int ng = len; while (Math.abs(ng - ok) > 1) { int mid = (ok + ng) / 2; if (A[mid] < A[pos]) ok = mid; else ng = mid; } swap(A, pos, ok); reverse(A, pos + 1, len - 1); return true; } //↓nCrをmod計算するために必要。 ***factorial(N)を呼ぶ必要がある*** static long ncr(int n, int r) { if (n < r) return 0; else if (r == 0) return 1; factorial(n); return Fa[n] / (Fa[n - r] * Fa[r]); } static long ncr2(int a, int b) { if (b == 0) return 1; else if (a < b) return 0; long res = 1; for (int i = 0; i < b; i++) { res *= a - i; res /= i + 1; } return res; } static long ncrdp(int n, int r) { if (n < r) return 0; long[][] dp = new long[n + 1][r + 1]; for (int ni = 0; ni < n + 1; ni++) { dp[ni][0] = dp[ni][ni] = 1; for (int ri = 1; ri < ni; ri++) { dp[ni][ri] = dp[ni - 1][ri - 1] + dp[ni - 1][ri]; } } return dp[n][r]; } static long modNcr(int n, int r) { if (n < r) return 0; long result = Fa[n]; result = result * modInv(Fa[n - r]) % MOD; result = result * modInv(Fa[r]) % MOD; return result; } public static long modSum(long... lar) { long res = 0; for (long l : lar) res = (res + l % MOD) % MOD; if (res < 0) res += MOD; res %= MOD; return res; } public static long modDiff(long a, long b) { long res = a % MOD - b % MOD; if (res < 0) res += MOD; res %= MOD; return res; } public static long modMul(long... lar) { long res = 1; for (long l : lar) res = (res * l % MOD) % MOD; if (res < 0) res += MOD; res %= MOD; return res; } public static long modDiv(long a, long b) { long x = a % MOD; long y = b % MOD; long res = (x * modInv(y)) % MOD; return res; } static long modInv(long n) { return modPow(n, MOD - 2); } static void factorial(int n) { Fa = new long[n + 1]; Fa[0] = Fa[1] = 1; for (int i = 2; i <= n; i++) { Fa[i] = (Fa[i - 1] * i) % MOD; } } static long modPow(long x, long n) { long res = 1L; while (n > 0) { if ((n & 1) == 1) { res = res * x % MOD; } x = x * x % MOD; n >>= 1; } return res; } //↑nCrをmod計算するために必要 static int gcd(int n, int r) { return r == 0 ? n : gcd(r, n % r); } static long gcd(long n, long r) { return r == 0 ? n : gcd(r, n % r); } static <T> void swap(T[] x, int i, int j) { T t = x[i]; x[i] = x[j]; x[j] = t; } static void swap(int[] x, int i, int j) { int t = x[i]; x[i] = x[j]; x[j] = t; } public static void reverse(int[] x) { int l = 0; int r = x.length - 1; while (l < r) { int temp = x[l]; x[l] = x[r]; x[r] = temp; l++; r--; } } public static void reverse(long[] x) { int l = 0; int r = x.length - 1; while (l < r) { long temp = x[l]; x[l] = x[r]; x[r] = temp; l++; r--; } } public static void reverse(char[] x) { int l = 0; int r = x.length - 1; while (l < r) { char temp = x[l]; x[l] = x[r]; x[r] = temp; l++; r--; } } public static void reverse(int[] x, int s, int e) { int l = s; int r = e; while (l < r) { int temp = x[l]; x[l] = x[r]; x[r] = temp; l++; r--; } } static int length(int a) { int cou = 0; while (a != 0) { a /= 10; cou++; } return cou; } static int length(long a) { int cou = 0; while (a != 0) { a /= 10; cou++; } return cou; } static int cou(boolean[] a) { int res = 0; for (boolean b : a) { if (b) res++; } return res; } static int cou(String s, char c) { int res = 0; for (char ci : s.toCharArray()) { if (ci == c) res++; } return res; } static int countC2(char[][] a, char c) { int co = 0; for (int i = 0; i < a.length; i++) for (int j = 0; j < a[0].length; j++) if (a[i][j] == c) co++; return co; } static int countI(int[] a, int key) { int co = 0; for (int i = 0; i < a.length; i++) if (a[i] == key) co++; return co; } static int countI(int[][] a, int key) { int co = 0; for (int i = 0; i < a.length; i++) for (int j = 0; j < a[0].length; j++) if (a[i][j] == key) co++; return co; } static void fill(int[][] a, int v) { for (int i = 0; i < a.length; i++) for (int j = 0; j < a[0].length; j++) a[i][j] = v; } static void fill(long[][] a, long v) { for (int i = 0; i < a.length; i++) for (int j = 0; j < a[0].length; j++) a[i][j] = v; } static void fill(int[][][] a, int v) { for (int i = 0; i < a.length; i++) for (int j = 0; j < a[0].length; j++) for (int k = 0; k < a[0][0].length; k++) a[i][j][k] = v; } static int max(int... a) { int res = Integer.MIN_VALUE; for (int i : a) { res = Math.max(res, i); } return res; } static long min(long... a) { long res = Long.MAX_VALUE; for (long i : a) { res = Math.min(res, i); } return res; } static int max(int[][] ar) { int res = Integer.MIN_VALUE; for (int i[] : ar) res = Math.max(res, max(i)); return res; } static int min(int... a) { int res = Integer.MAX_VALUE; for (int i : a) { res = Math.min(res, i); } return res; } static int min(int[][] ar) { int res = Integer.MAX_VALUE; for (int i[] : ar) res = Math.min(res, min(i)); return res; } static int sum(int[] a) { int cou = 0; for (int i : a) cou += i; return cou; } static int abs(int a) { return Math.abs(a); } static class FastScanner { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public FastScanner(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); tokenizer = null; } public String next() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } /*public String nextChar(){ return (char)next()[0]; }*/ public String nextLine() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken("\n"); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public int[] nextIntArrayOneIndex(int n) { int[] a = new int[n + 1]; for (int i = 1; i < n + 1; i++) { a[i] = nextInt(); } return a; } public int[] nextIntArrayDec(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt() - 1; } return a; } public int[][] nextIntArray2(int h, int w) { int[][] a = new int[h][w]; for (int hi = 0; hi < h; hi++) { for (int wi = 0; wi < w; wi++) { a[hi][wi] = nextInt(); } } return a; } public int[][] nextIntArray2Dec(int h, int w) { int[][] a = new int[h][w]; for (int hi = 0; hi < h; hi++) { for (int wi = 0; wi < w; wi++) { a[hi][wi] = nextInt() - 1; } } return a; } //複数の配列を受け取る public void nextIntArrays2ar(int[] a, int[] b) { for (int i = 0; i < a.length; i++) { a[i] = sc.nextInt(); b[i] = sc.nextInt(); } } public void nextIntArrays2arDec(int[] a, int[] b) { for (int i = 0; i < a.length; i++) { a[i] = sc.nextInt() - 1; b[i] = sc.nextInt() - 1; } } //複数の配列を受け取る public void nextIntArrays3ar(int[] a, int[] b, int[] c) { for (int i = 0; i < a.length; i++) { a[i] = sc.nextInt(); b[i] = sc.nextInt(); c[i] = sc.nextInt(); } } //複数の配列を受け取る public void nextIntArrays3arDecLeft2(int[] a, int[] b, int[] c) { for (int i = 0; i < a.length; i++) { a[i] = sc.nextInt() - 1; b[i] = sc.nextInt() - 1; c[i] = sc.nextInt(); } } public Integer[] nextIntegerArray(int n) { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public char[] nextCharArray(int n) { char[] a = next().toCharArray(); return a; } public char[][] nextCharArray2(int h, int w) { char[][] a = new char[h][w]; for (int i = 0; i < h; i++) { a[i] = next().toCharArray(); } return a; } //スペースが入っている場合 public char[][] nextCharArray2s(int h, int w) { char[][] a = new char[h][w]; for (int i = 0; i < h; i++) { a[i] = nextLine().replace(" ", "").toCharArray(); } return a; } public char[][] nextWrapCharArray2(int h, int w, char c) { char[][] a = new char[h + 2][w + 2]; //char c = '*'; int i; for (i = 0; i < w + 2; i++) a[0][i] = c; for (i = 1; i < h + 1; i++) { a[i] = (c + next() + c).toCharArray(); } for (i = 0; i < w + 2; i++) a[h + 1][i] = c; return a; } //スペースが入ってる時用 public char[][] nextWrapCharArray2s(int h, int w, char c) { char[][] a = new char[h + 2][w + 2]; //char c = '*'; int i; for (i = 0; i < w + 2; i++) a[0][i] = c; for (i = 1; i < h + 1; i++) { a[i] = (c + nextLine().replace(" ", "") + c).toCharArray(); } for (i = 0; i < w + 2; i++) a[h + 1][i] = c; return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } public long[][] nextLongArray2(int h, int w) { long[][] a = new long[h][w]; for (int hi = 0; hi < h; hi++) { for (int wi = 0; wi < w; wi++) { a[hi][wi] = nextLong(); } } return a; } } }
JAVA
1016_B. Segment Occurrences
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.
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class quesries { public static void main(String[] args) { // TODO Auto-generated method stub FastReader s=new FastReader(); int n=s.nextInt(); int m=s.nextInt(); int q=s.nextInt(); String str1=s.next(); String str2=s.next(); boolean arr[]=new boolean[n]; int arr1[]=new int[q]; int arr2[]=new int[q]; int count; for(int i=0;i<=str1.length()-str2.length();i++) { String substr=str1.substring(i, i+str2.length()); if(substr.matches(str2)) { arr[i]=true; } } for(int i=0;i<q;i++) { count=0; arr1[i]=s.nextInt(); arr2[i]=s.nextInt(); for(int j=arr1[i]-1;j<=arr2[i]-str2.length();j++) { if(arr[j]) count++; } System.out.println(count); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JAVA
1016_B. Segment Occurrences
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.
2
8
n, m, q = map(int, input().split()) s = input() t = input() pat = [0] * n i = 0 c = 0 while True: f = s[i:].find(t) if f == -1: for j in range(i, n): pat[j] = c break g = i + f for j in range(i, g): pat[j] = c c += 1 pat[g] = c i = g + 1 pat = [0] + pat for _ in range(q): l, r = map(int, input().split()) if r - l + 1 >= m: print(pat[r-m+1] - pat[l-1]) else: print(0)
PYTHON3
1016_B. Segment Occurrences
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.
2
8
#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; } }
CPP
1016_B. Segment Occurrences
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.
2
8
#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; }
CPP
1016_B. Segment Occurrences
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.
2
8
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Objects; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author The Viet Nguyen */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader2 in = new InputReader2(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, InputReader2 in, PrintWriter out) { int n = in.nextInt(), m = in.nextInt(), q = in.nextInt(); int[] ss = new int[n]; String s = in.next(), t = in.next(); for (int i = 0; i <= n - m; ++i) if (Objects.equals(s.substring(i, i + m), t)) ++ss[i]; for (int i = 0; i < q; ++i) { int l = in.nextInt() - 1, r = in.nextInt(), ans = 0; for (int j = l; j <= r - m; ++j) if (ss[j] > 0) ++ans; out.println(ans); } } } static class InputReader2 { private BufferedReader reader; private StringTokenizer tokenizer; public InputReader2(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
JAVA
1016_B. Segment Occurrences
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.
2
8
import java.util.*; import java.io.*; public class Solution { static Scanner in =new Scanner(System.in); static final Random random=new Random(); static int mod=1000_000_007; public static void main(String[] args) { //int tt=in.nextInt(); // outer:while(tt-->0) { int n=in.nextInt(); int m=in.nextInt(); int q=in.nextInt();in.nextLine(); char s[]=in.nextLine().toCharArray();char[] t=in.nextLine().toCharArray(); if (m>n) { for (int i=0; i<q; i++) System.out.println(0); return; }boolean[] matches=new boolean[n-m+1]; for (int i=0; i<n-m+1; i++) { matches[i]=true; for (int j=0; j<m; j++) if (s[i+j]!=t[j]) matches[i]=false; } int[] cs=new int[matches.length]; cs[0]=matches[0]?1:0; for (int i=1; i<cs.length; i++) cs[i]=cs[i-1]+(matches[i]?1:0); for (int i=0; i<q; i++) { int from=in.nextInt()-1, to=in.nextInt()-1-m+1; if (to<from) { System.out.println(0); } else { System.out.println(from==0?cs[to]:(cs[to]-cs[from-1])); } } //} } static int nCr(int n,int r,int p){ if (r == 0) return 1; if(r==1)return n; return ((int)fact(n)*modInverse((int)fact(r),p)%p * modInverse((int)fact(n-r),p) % p)%p; } static int modInverse(int n, int p){ return power(n, p-2, p); } static int power(int x,int y,int p){ int res = 1; x= x % p; while (y>0){ if (y%2==1) res= (res*x)% p; y= y >> 1; x= (x*x)% p; } return res; } static int[]readAr(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) {a[i]=in.nextInt();} return a; } static int fact(int k){ long[]f=new long[10001];f[1]=1; for(int i=2;i<10001;i++){ f[i]=(i*f[i-1]% mod); } return (int)f[k]; } static void ruffleSort(int[] a) { int n=a.length; for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static void reverse(int []a){ int n= a.length; for(int i=0;i<n/2;i++){ int temp=a[i]; a[i]=a[n-i-1]; a[n-i-1]=temp; a[i]=a[i]; } //debug(a); } static void debug(int []a) { for (int i=0; i<a.length;i++) System.out.print(a[i]+" "); //System.out.println(); } static void print(List<Integer>s) { for(int x:s) System.out.print(x+","); System.out.println(); } static int gcd(int a, int b) { return b==0 ? a : gcd(b, a % b); } static int find_max(int []a){ int m=Integer.MIN_VALUE; for(int x:a)m=Math.max(x,m); return m; } static int find_min(int []a){ int m=Integer.MAX_VALUE; for(int x:a)m=Math.min(x,m); return m; } }
JAVA
1016_B. Segment Occurrences
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.
2
8
#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])); } }
CPP
1016_B. Segment Occurrences
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.
2
8
#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; }
CPP
1016_B. Segment Occurrences
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.
2
8
import sys def main(): n, m, q = map(int, input().strip().split()) s = input() t = input() pos = [] for i in range(len(s)): if i + len(t) <= len(s) and s[i:i+len(t)] == t: pos.append(1) else: pos.append(0) sum = [0] for i in range(len(pos)): sum.append(sum[-1] + pos[i]) for _ in range(q): l, r = map(int, input().strip().split()) r = r - len(t) + 1 l -= 1 if l < r: print(sum[r] - sum[l]) else: print(0) return 0 def test(i): with open("test_{}.txt".format(i)) as fin: sys.stdin = fin main() if __name__ == "__main__": # test(1) # test(2) sys.exit(main())
PYTHON3
1016_B. Segment Occurrences
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.
2
8
#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; }
CPP
1016_B. Segment Occurrences
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.
2
8
n, m, q = map(int, input().split()) s = input() t = input() a = [0] * (1000 + 7) b = [0] * (1000 + 7) for i in range(n - m + 1): fl = 1 for j in range(m): if s[i + j] != t[j]: fl = 0 break b[i] = fl a[i + 1] = a[i] + b[i] for i in range(max(0, n - m + 1), n): a[i + 1] = a[i] while q > 0: q -= 1 l, r = map(int, input().split()) l -= 1 r -= m - 1 if r < l: print(0) else: print(a[r] - a[l])
PYTHON3
1016_B. Segment Occurrences
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.
2
8
n,m,q = list(map(int,input().split())) s = input() t = input() def check(i): if len(t) > len(s): return 0 for k in range(i,i+len(t)): if s[k] != t[k-i]: return 0 return 1 pref = [0] for i in range(len(s)-len(t)+1): pref.append(pref[-1]+check(i)) for i in range(len(s)-len(t)+1,len(s)): pref.append(pref[-1]) for k in range(q): l,r = list(map(int, input().split())) r -= len(t) - 1 if r < l: print(0) continue print(pref[r]-pref[l-1])
PYTHON3
1016_B. Segment Occurrences
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.
2
8
import re import bisect def preprocess(s, t): starts = [] ends = [] l = len(t) for m in re.finditer('(?=%s)' % t, s): starts.append(m.start() + 1) ends.append(m.start() + l) # print(starts) # print(ends) return starts, ends def run(l, r, starts, ends): lpos = bisect.bisect_left(starts, l) rpos = bisect.bisect_right(ends, r) tt = rpos - lpos return tt if tt >= 0 else 0 def main(): n, m, q = map(int, input().split()) s = input() t = input() starts, ends = preprocess(s, t) for _ in range(q): l, r = map(int, input().split()) print(run(l, r, starts, ends)) main()
PYTHON3
1016_B. Segment Occurrences
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.
2
8
#include <bits/stdc++.h> using namespace std; int32_t main() { ios::sync_with_stdio(false); cin.tie(nullptr); long long n, m, q; cin >> n >> m >> q; string s, t; cin >> s >> t; vector<long long> matchingId; for (long long i = 0; i <= n - m; i++) { string second = s.substr(i, m); if (second == t) matchingId.push_back(i); } while (q--) { long long l, r; cin >> l >> r; l--, r--; if (r - l + 1 < m) { cout << 0 << '\n'; continue; } l = lower_bound(matchingId.begin(), matchingId.end(), l) - matchingId.begin(); r = lower_bound(matchingId.begin(), matchingId.end(), r - m + 2) - matchingId.begin(); cout << r - l << '\n'; } }
CPP
1016_B. Segment Occurrences
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.
2
8
n, m, q = map(int, input().split()) s = input() t = input() arr = [] for i in range(n - m + 1): if s[i:i + m] == t: arr.append(i) for i in range(q): l, r = map(int, input().split()) k = 0 for j in range(len(arr)): if arr[j] >= l - 1 and arr[j] + m - 1<= r - 1: k = k + 1 print(k)
PYTHON3
1016_B. Segment Occurrences
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.
2
8
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n, m, q; cin >> n >> m >> q; string s1, s2; cin >> s1 >> s2; long long int arr[n], d = 0; for (long long int i = 0; i <= n - m; i++) { string s3 = s1.substr(i, m); if (s3 == s2) { arr[d] = i; d++; } } for (long long int i = 0; i < q; i++) { long long int a, b; cin >> a >> b; long long int out = 0; for (long long int i = 0; i < d; i++) { if (arr[i] + 1 >= a && arr[i] + m <= b) { out++; } } cout << out << '\n'; } return 0; }
CPP
1016_B. Segment Occurrences
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.
2
8
from sys import stdin from math import * line = stdin.readline().rstrip().split() n = int(line[0]) m = int(line[1]) q = int(line[2]) s = stdin.readline().rstrip().split()[0] t = stdin.readline().rstrip().split()[0] bools = [0] * (n - m + 1 + 1) accum = 0 for i in range(n - m, -1, -1): if s[i:i+m] == t: accum += 1 bools[i] = accum for i in range(q): numbers = list(map(int, stdin.readline().rstrip().split())) l = numbers[0] - 1 r = numbers[1] - 1 - m + 1 + 1 if r > l: print(bools[l] - bools[r]) else: print(0) #suma = 0 # for j in range(l, r+1): # if bools[j]: # suma += 1
PYTHON3
1016_B. Segment Occurrences
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.
2
8
import re, bisect n, m, q = [int(v) for v in input().split()] s = input().strip() t = input().strip() starts = [m.start() for m in re.finditer('(?=%s)' % t, s)] for _ in range(q): l, r = [int(v) for v in input().split()] if r - l + 1 < len(t): print(0) else: print(bisect.bisect_right(starts, r - len(t)) - bisect.bisect_left(starts, l - 1))
PYTHON3
1016_B. Segment Occurrences
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.
2
8
// 10-Feb-2021 import java.util.*; import java.io.*; public class B { static class FastReader { BufferedReader br; StringTokenizer st; private FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayOne(int n) { int[] a = new int[n + 1]; for (int i = 1; i < n + 1; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader s = new FastReader(); StringBuilder str = new StringBuilder(); int n = s.nextInt(), m = s.nextInt(); String a = s.nextLine(); String b = s.nextLine(); int ans[] = new int[n + 1]; int q = s.nextInt(); for(int i = 0; i <= n - m; i++) { if(a.substring(i,i + m).equals(b)) { ans[i + 1] = 1; } } while(q-- > 0) { int l = s.nextInt(), r = s.nextInt(); int count = 0; for(int i = l ; i <= r; i++) { if(i + m - 1 <= r && ans[i]==1) { count++; } } str.append(count + "\n"); } System.out.println(str); } }
JAVA
1016_B. Segment Occurrences
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.
2
8
import java.util.*; public class B { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.next()); int m = Integer.parseInt(sc.next()); int q = Integer.parseInt(sc.next()); String s = sc.next(); String t = sc.next(); for (int i = 0; i < q; i++) { int l = Integer.parseInt(sc.next()); int r = Integer.parseInt(sc.next()); String range; range = s.substring(l-1, r); int ans = countOccurences(t, range); System.out.println(ans); } sc.close(); } public static int[] doArray(String looking) { int[] x = new int[looking.length()]; x[0] = 0; int len = 0; int go = 1; while (go < looking.length()) { if (looking.charAt(go) == looking.charAt(len)) { len++; x[go] = len; go++; } else { if (len != 0) { len = x[len-1]; } else { x[go] = len; go++; } } } return x; } public static int countOccurences(String looking, String big) { int lLen = looking.length(), bLen = big.length(), x = 0, y = 0, res = 0; int arr[] = doArray(looking); while (y < bLen) { if (looking.charAt(x) == big.charAt(y)) { x++; y++; } if (x == lLen) { x = arr[x-1]; res++; } else if (y < bLen && (big.charAt(y) != looking.charAt(x))) { if (x != 0) x = arr[x-1]; else y++; } } return res; } }
JAVA
1016_B. Segment Occurrences
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.
2
8
# Python program for KMP Algorithm def KMPSearch(pat, txt): ans = [] M = len(pat) N = len(txt) # create lps[] that will hold the longest prefix suffix # values for pattern lps = [0]*M j = 0 # index for pat[] # Preprocess the pattern (calculate lps[] array) computeLPSArray(pat, M, lps) i = 0 # index for txt[] while i < N: if pat[j] == txt[i]: i += 1 j += 1 if j == M: ans.append(i-j) j = lps[j-1] # mismatch after j matches elif i < N and pat[j] != txt[i]: # Do not match lps[0..lps[j-1]] characters, # they will match anyway if j != 0: j = lps[j-1] else: i += 1 return ans def computeLPSArray(pat, M, lps): len = 0 # length of the previous longest prefix suffix lps[0] # lps[0] is always 0 i = 1 # the loop calculates lps[i] for i = 1 to M-1 while i < M: if pat[i]== pat[len]: len += 1 lps[i] = len i += 1 else: # This is tricky. Consider the example. # AAACAAAA and i = 7. The idea is similar # to search step. if len != 0: len = lps[len-1] # Also, note that we do not increment i here else: lps[i] = 0 i += 1 n, m, q = map(int, input().split()) s = input().strip() t = input().strip() pos = KMPSearch(t, s) for i in range(q): l, r = map(int, input().split()) l -= 1 ans = 0 for a in pos: if(a >= l and a+m <=r): ans+=1 print(ans)
PYTHON3
1016_B. Segment Occurrences
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.
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; public class B { private static char T[], P[]; private static int b[], n, m; private static ArrayList<Integer> subs; private static void kmpPreprocess() { b = new int[m + 1]; int i = 0, j = -1; b[0] = -1; while (i < m) { while (j >= 0 && P[i] != P[j]) { j = b[j]; } i++; j++; b[i] = j; } } private static void kmpSearch() { subs = new ArrayList<>(); int i = 0, j = 0, k = 0; while (i < n) { while (j >= 0 && T[i] != P[j]) { j = b[j]; } i++; j++; if (j == m) { subs.add(i - j); j = b[j]; } } } private static int query(int a, int b) { b -= (m - 1); if (b < a || m > n) { return 0; } int sIdx = Collections.binarySearch(subs, a); if (sIdx < 0) { sIdx *= -1; sIdx--; } int eIdx = Collections.binarySearch(subs, b); if (eIdx < 0) { eIdx *= -1; eIdx -= 2; } return eIdx - sIdx + 1; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = br.readLine().trim(), buffer[]; buffer = line.split("\\s+"); n = Integer.parseInt(buffer[0]); m = Integer.parseInt(buffer[1]); int Q = Integer.parseInt(buffer[2]); T = br.readLine().trim().toCharArray(); P = br.readLine().trim().toCharArray(); kmpPreprocess(); kmpSearch(); for (int i = 0; i < Q; i++) { line = br.readLine().trim(); buffer = line.split("\\s+"); int a = Integer.parseInt(buffer[0]) - 1; int b = Integer.parseInt(buffer[1]) - 1; System.out.println(query(a, b)); } } }
JAVA
1016_B. Segment Occurrences
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.
2
8
#include <bits/stdc++.h> using namespace std; int main() { vector<int> left, right; int n, m, s, l, r; string kata, q; cin >> n >> m >> s; cin >> kata >> q; size_t pos = kata.find(q, 0); while (pos != string::npos) { left.push_back(pos + 1); right.push_back(pos + m); pos = kata.find(q, pos + 1); } vector<int>::iterator low, up; for (int i = 0; i < s; i++) { cin >> l >> r; int lef = lower_bound(left.begin(), left.end(), l) - left.begin(); int righ = lower_bound(right.begin(), right.end(), r + 1) - right.begin(); if (righ - lef >= 0) cout << righ - lef << endl; else cout << 0 << endl; } return 0; }
CPP
1016_B. Segment Occurrences
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.
2
8
n, m, q= map(int,input().split()) s,t = input(), input() ans = "" for i in range(n-m+1): if s[i:i+m] == t: ans += "1" else:ans += "0" for i in range(0,q): l, r = map(int,input().split()) if r-l+1 >= m: print(ans[l-1 : r-m+1].count("1")) else:print("0")
PYTHON3
1016_B. Segment Occurrences
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.
2
8
#include <bits/stdc++.h> using namespace std; int a[10000]; int inds[10000]; int main() { int n, m, q; cin >> n >> m >> q; string str, target; cin >> str >> target; for (int i = 0; i <= (n - m); i++) { if (str[i] == target[0]) { string temp = str.substr(i, m); if (temp == target) a[i] = 1; else a[i] = 0; } } for (int i = 0; i < q; i++) { int l, r; cin >> l >> r; l--; r--; int count = 0; for (int b = l; b <= r; b++) { if (a[b] == 1 && (b + m - 1) <= r) count++; } cout << count; if (i != q - 1) cout << endl; } return 0; }
CPP
1016_B. Segment Occurrences
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.
2
8
n, m, q = [int(i) for i in input().split()] S = input() T = input() A = [[int(i) for i in input().split()] for j in range(q)] s = [] for i in range(len(S)): if S[i:i+len(T)] == T: s.append(1) else: s.append(0) csum = [0] for s_ in s: csum.append(csum[-1] + s_) for a, b in A: print(csum[max(a-1, b-len(T)+1)] - csum[a-1])
PYTHON3
1016_B. Segment Occurrences
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.
2
8
######### ## ## ## #### ##### ## # ## # ## # # # # # # # # # # # # # # # # # # # # # # # ### # # # # # # # # # # # # # ##### # # # # ### # # # # # # # # ##### # # # # # # # # # # # # # # # # # # ######### # # # # ##### # ##### # ## # ## # # """ PPPPPPP RRRRRRR OOOO VV VV EEEEEEEEEE PPPPPPPP RRRRRRRR OOOOOO VV VV EE PPPPPPPPP RRRRRRRRR OOOOOOOO VV VV EE PPPPPPPP RRRRRRRR OOOOOOOO VV VV EEEEEE PPPPPPP RRRRRRR OOOOOOOO VV VV EEEEEEE PP RRRR OOOOOOOO VV VV EEEEEE PP RR RR OOOOOOOO VV VV EE PP RR RR OOOOOO VV VV EE PP RR RR OOOO VVVV EEEEEEEEEE """ """ Perfection is achieved not when there is nothing more to add, but rather when there is nothing more to take away. """ import sys input = sys.stdin.readline # from bisect import bisect_left as lower_bound; # from bisect import bisect_right as upper_bound; # from math import ceil, factorial; def ceil(x): if x != int(x): x = int(x) + 1 return x def factorial(x, m): val = 1 while x>0: val = (val * x) % m x -= 1 return val def fact(x): val = 1 while x > 0: val *= x x -= 1 return val # swap_array function def swaparr(arr, a,b): temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; ## gcd function def gcd(a,b): if b == 0: return a; return gcd(b, a % b); ## lcm function def lcm(a, b): return (a * b) // gcd(a, b) ## nCr function efficient using Binomial Cofficient def nCr(n, k): if k > n: return 0 if(k > n - k): k = n - k res = 1 for i in range(k): res = res * (n - i) res = res / (i + 1) return int(res) ## upper bound function code -- such that e in a[:i] e < x; def upper_bound(a, x, lo=0, hi = None): if hi == None: hi = len(a); while lo < hi: mid = (lo+hi)//2; if a[mid] < x: lo = mid+1; else: hi = mid; return lo; ## prime factorization def primefs(n): ## if n == 1 ## calculating primes primes = {} while(n%2 == 0 and n > 0): primes[2] = primes.get(2, 0) + 1 n = n//2 for i in range(3, int(n**0.5)+2, 2): while(n%i == 0 and n > 0): primes[i] = primes.get(i, 0) + 1 n = n//i if n > 2: primes[n] = primes.get(n, 0) + 1 ## prime factoriazation of n is stored in dictionary ## primes and can be accesed. O(sqrt n) return primes ## MODULAR EXPONENTIATION FUNCTION def power(x, y, p): res = 1 x = x % p if (x == 0) : return 0 while (y > 0) : if ((y & 1) == 1) : res = (res * x) % p y = y >> 1 x = (x * x) % p return res ## DISJOINT SET UNINON FUNCTIONS def swap(a,b): temp = a a = b b = temp return a,b; # find function with path compression included (recursive) # def find(x, link): # if link[x] == x: # return x # link[x] = find(link[x], link); # return link[x]; # find function with path compression (ITERATIVE) def find(x, link): p = x; while( p != link[p]): p = link[p]; while( x != p): nex = link[x]; link[x] = p; x = nex; return p; # the union function which makes union(x,y) # of two nodes x and y def union(x, y, link, size): x = find(x, link) y = find(y, link) if size[x] < size[y]: x,y = swap(x,y) if x != y: size[x] += size[y] link[y] = x ## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES def sieve(n): prime = [True for i in range(n+1)] prime[0], prime[1] = False, False p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * p, n+1, p): prime[i] = False p += 1 return prime # Euler's Toitent Function phi def phi(n) : result = n p = 2 while(p * p<= n) : if (n % p == 0) : while (n % p == 0) : n = n // p result = result * (1.0 - (1.0 / (float) (p))) p = p + 1 if (n > 1) : result = result * (1.0 - (1.0 / (float)(n))) return (int)(result) def is_prime(n): if n == 0: return False if n == 1: return True for i in range(2, int(n ** (1 / 2)) + 1): if not n % i: return False return True #### PRIME FACTORIZATION IN O(log n) using Sieve #### MAXN = int(1e5 + 5) def spf_sieve(): spf[1] = 1; for i in range(2, MAXN): spf[i] = i; for i in range(4, MAXN, 2): spf[i] = 2; for i in range(3, ceil(MAXN ** 0.5), 2): if spf[i] == i: for j in range(i*i, MAXN, i): if spf[j] == j: spf[j] = i; ## function for storing smallest prime factors (spf) in the array ################## un-comment below 2 lines when using factorization ################# spf = [0 for i in range(MAXN)] # spf_sieve(); def factoriazation(x): res = [] for i in range(2, int(x ** 0.5) + 1): while x % i == 0: res.append(i) x //= i if x != 1: res.append(x) return res ## this function is useful for multiple queries only, o/w use ## primefs function above. complexity O(log n) def factors(n): res = [] for i in range(1, int(n ** 0.5) + 1): if n % i == 0: res.append(i) res.append(n // i) return list(set(res)) ## taking integer array input def int_array(): return list(map(int, input().strip().split())); def float_array(): return list(map(float, input().strip().split())); ## taking string array input def str_array(): return input().strip().split(); #defining a couple constants MOD = int(1e9)+7; CMOD = 998244353; INF = float('inf'); NINF = -float('inf'); ################### ---------------- TEMPLATE ENDS HERE ---------------- ################### from itertools import permutations import math from bisect import bisect_left def solve(): n, m, q = map(int, input().split()) string = list(input().rstrip()) substring = input().rstrip() sequence = [0] * (n + 1) for i in range(n - m + 1): # print(i, "".join(string[i:i + m])) if "".join(string[i:i + m]) == substring: sequence[i + 1] = 1 # print(sequence) for i in range(1, n + 1): sequence[i] += sequence[i - 1] # print(sequence) for i in range(q): l, r = map(int, input().split()) print(sequence[max(l - 1, r - m + 1)] - sequence[l - 1]) if __name__ == '__main__': for _ in range(1): solve() # fin_time = datetime.now() # print("Execution time (for loop): ", (fin_time-init_time))
PYTHON3
1016_B. Segment Occurrences
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.
2
8
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.FilterInputStream; import java.io.BufferedInputStream; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Jenish */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; ScanReader in = new ScanReader(inputStream); PrintWriter out = new PrintWriter(outputStream); BSegmentOccurrences solver = new BSegmentOccurrences(); solver.solve(1, in, out); out.close(); } static class BSegmentOccurrences { public void solve(int testNumber, ScanReader in, PrintWriter out) { int n = in.scanInt(); int m = in.scanInt(); int q = in.scanInt(); String s = in.scanString(); String t = in.scanString(); long prefix[] = new long[s.length()]; long dp[][] = new long[n + 2][n + 2]; for (int i = 0; i < n; i++) { if (i + m <= n) { String tt = s.substring(i, i + m); if (tt.equals(t)) { prefix[i + m - 1]++; } } } for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { if (prefix[j] == 1 && (j - m + 1) >= i) { dp[i][j] = 1; } } for (int j = 1; j < n; j++) { dp[i][j] += dp[i][j - 1]; } } while (q-- > 0) { int x = in.scanInt() - 1; int y = in.scanInt() - 1; out.println(dp[x][y]); } } } static class ScanReader { private byte[] buf = new byte[4 * 1024]; private int INDEX; private BufferedInputStream in; private int TOTAL; public ScanReader(InputStream inputStream) { in = new BufferedInputStream(inputStream); } private int scan() { if (INDEX >= TOTAL) { INDEX = 0; try { TOTAL = in.read(buf); } catch (Exception e) { e.printStackTrace(); } if (TOTAL <= 0) return -1; } return buf[INDEX++]; } public int scanInt() { int I = 0; int n = scan(); while (isWhiteSpace(n)) n = scan(); int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { I *= 10; I += n - '0'; n = scan(); } } return neg * I; } public String scanString() { int c = scan(); while (isWhiteSpace(c)) c = scan(); StringBuilder RESULT = new StringBuilder(); do { RESULT.appendCodePoint(c); c = scan(); } while (!isWhiteSpace(c)); return RESULT.toString(); } private boolean isWhiteSpace(int n) { if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true; else return false; } } }
JAVA
1016_B. Segment Occurrences
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.
2
8
#include <bits/stdc++.h> using namespace std; void urmi() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } int main() { long long n, m, q, i, c = 0, d = 0, e, f; cin >> n >> m >> q; string s, t; cin >> s >> t; map<long long, long long> m1; for (i = 0; i < n; i++) { if (s.compare(i, t.size(), t) == 0) { d++; m1[i] = d; } else m1[i] = m1[i - 1]; } while (q--) { long long l, r; cin >> l >> r; r -= m - 1; l--; if (r >= l) { cout << m1[r - 1] - m1[l - 1] << endl; } else cout << "0" << endl; } }
CPP
1016_B. Segment Occurrences
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.
2
8
n, m, q = map(int, raw_input().split()) s = raw_input() t = raw_input() positions = [] for i in xrange(len(s) - len(t) + 1): if t == s[i:i + len(t)]: positions.append(i) def bsearch(positions, x): if len(positions) == 0: return 0 l = 0 r = len(positions) while l + 1 != r: m = (l + r) / 2 if positions[m] <= x: l = m else: r = m if positions[l] <= x: return l + 1 else: return l for _ in xrange(q): l, r = map(int, raw_input().split()) print max(bsearch(positions, r - 1 - len(t) + 1) - bsearch(positions, l - 2), 0)
PYTHON
1016_B. Segment Occurrences
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.
2
8
#include <bits/stdc++.h> using namespace std; int pre[1005], p, n, m, t, l, r; char a[1005], b[1005]; int main() { cin >> n >> m >> t; cin >> (a + 1) >> b; for (int i = 1; i <= n - m + 1; i++) { p = 0; for (int j = 0; j < m; j++) { if (a[i + j] != b[j]) { p = 1; break; } } if (p == 1) pre[i] = pre[i - 1]; else pre[i] = pre[i - 1] + 1; } while (t--) { cin >> l >> r; if (r - l < m - 1) cout << "0" << endl; else { cout << pre[r - m + 1] - pre[l - 1] << endl; } } return 0; }
CPP
1016_B. Segment Occurrences
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.
2
8
#include <bits/stdc++.h> using namespace std; string str, sub; vector<long long int> v; int main() { long long int n, m, q, x, y; cin >> n >> m >> q; cin >> str >> sub; size_t pos = str.find(sub, 0); while (pos != string::npos) { v.push_back(pos); pos = str.find(sub, pos + 1); } while (q--) { cin >> x >> y; x--; y--; long long int ans = 0; std::vector<long long int>::iterator low, up; low = std::lower_bound(v.begin(), v.end(), x); long long int l = low - v.begin(); for (long long int i = low - v.begin(); i < v.size(); i++) { if (v[i] > y) break; if (v[i] + m - 1 <= y) ans++; } cout << ans << endl; } }
CPP
1016_B. Segment Occurrences
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.
2
8
n, m, q = map(int, input().split()) s = input() t = input() nex = -1 i = 0 ans = [0]*n while(i<n): if s[i] == t[0]: nex = -1 start = i i += 1 j = 1 flag = False while(i<n and j<m): if not flag: if s[i] == t[0]: nex = i flag = True if s[i] != t[j]: break i += 1 j += 1 if j==m: ans[start] = 1 if nex != -1: i = nex else: i += 1 ans.insert(0, 0) for i in range(1, n+1): ans[i] = ans[i]+ans[i-1] for _ in range(q): l, r = map(int, input().split()) if r-l+1>=m: print(ans[r-m+1] - ans[l-1]) else: print("0")
PYTHON3
1016_B. Segment Occurrences
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.
2
8
(n,m,q)=input().split(' ') n=int(n) m=int(m) q=int(q) s=input() t=input() length=len(t) su=[0,] now=0 for i in range(len(s)-length+1): if s[i:i+length]==t: now+=1 su.append(now) #print(su) for i in range(q): (l,r)=input().split(' ') l=int(l) r=int(r)-length+1 if r>=l: #print(out) print(str(su[r]-su[l-1])) else: print(0)
PYTHON3
1016_B. Segment Occurrences
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.
2
8
n, m, q = map(int, input().split()) s = input() t = input() if len(t) > len(s): for _ in range(q): print(0) else: pos = s.find(t) cnt = [0] while pos != -1: while len(cnt) <= pos: cnt.append(cnt[-1]) cnt.append(cnt[-1] + 1) pos = s.find(t, pos + 1) while len(cnt) < len(s) + 1: cnt.append(cnt[-1]) for test in range(q): left, right = map(int, input().split()) ans = cnt[right - len(t) + 1] - cnt[left - 1] if ans < 0 or right - left + 1 < len(t): ans = 0 print(ans)
PYTHON3
1016_B. Segment Occurrences
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.
2
8
#include <bits/stdc++.h> using namespace std; int a[1005]; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m, q; cin >> n >> m >> q; string s, t; cin >> s >> t; for (int i = 0; i + m <= n; ++i) { if (s.substr(i, m) == t) { a[i + 1]++; } } for (int i = 1; i <= n; ++i) a[i] += a[i - 1]; int l, r; while (q--) { cin >> l >> r; r -= m - 1; cout << max(0, a[max(0, r)] - a[l - 1]) << '\n'; } return 0; }
CPP
1016_B. Segment Occurrences
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.
2
8
n,m,q = map(int, input().split()) s = input() t = input() a = [0]*n for i in range(n-m+1): if s[i:i+m]==t: a[i] = 1 for k in range(q): l, r = map(int, input().split()) l = l-1 r = r-m res = sum(a[l:r+1]) if l<=r else 0 print(res)
PYTHON3
1016_B. Segment Occurrences
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.
2
8
n,m,q = map(int,input().split()) a = input() b = input() c = [0] d = 0 for i in range(m-1,n): if (a[i-m+1:i+1] == b): d += 1 c.append(d) for i in range(q): x,y = map(int,input().split()) if (y-x+1) < m: print(0) else: print(max((c[y-m+1]-c[x-1]),0))
PYTHON3
1016_B. Segment Occurrences
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.
2
8
n, m, q = map(int,input().split()) s = input() t = input() res = [] for i in range(n-m+1): if s[i:i+m] == t: e = (i, i+m-1) res.append(e) for i in range(q): li, ri = map(int, input().split()) k = 0 for t, p in res: if t >= (li - 1) and p <= (ri - 1): k += 1 print(k)
PYTHON3
1016_B. Segment Occurrences
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.
2
8
n, m, q = map(int, input().split()) s = input() t = input() ht = hash(t) hss = [hash(s[i : i + m]) for i in range(n - m + 1)] lhss = n - m + 1 for i in range(q): l, r = map(int, input().split()) c = 0 for j in range(l - 1, r): if j + m > r: continue if j < lhss and hss[j] == ht: c += 1 print(c)
PYTHON3
1016_B. Segment Occurrences
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.
2
8
import os, sys from io import BytesIO, IOBase def main(): n, m, q = rints() s, t, cum = rstr(), rstr(), [] for i in range(n - m + 1): if s[i:i + m] == t: cum.append((i + 1, i + m)) for i in range(q): l, r = rints() ans = 0 for l1, r1 in cum: if l <= l1 and r >= r1: ans += 1 print(ans) class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") BUFSIZE = 8192 sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") rstr = lambda: input().strip() rstrs = lambda: [str(x) for x in input().split()] rstr_2d = lambda n: [rstr() for _ in range(n)] rint = lambda: int(input()) rints = lambda: [int(x) for x in input().split()] rint_2d = lambda n: [rint() for _ in range(n)] rints_2d = lambda n: [rints() for _ in range(n)] ceil1 = lambda a, b: (a + b - 1) // b if __name__ == '__main__': main()
PYTHON3
1016_B. Segment Occurrences
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.
2
8
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.Collections; import java.util.ArrayList; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); BSegmentOccurrences solver = new BSegmentOccurrences(); solver.solve(1, in, out); out.close(); } static class BSegmentOccurrences { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(), m = in.nextInt(), q = in.nextInt(); String s = in.next(), t = in.next(); ArrayList<Integer> sp = new ArrayList<>(); int c = -1; for (int i = 0; i < n; i++) { c = s.indexOf(t, i); if (c == -1) { break; } i = c; sp.add(i); } for (int i = 0; i < q; i++) { int si = in.nextInt() - 1, ei = in.nextInt(); if (ei - si >= m) { int d = Collections.binarySearch(sp, si); int p = Collections.binarySearch(sp, ei - m + 1); if (d < 0) { d = -d - 1; } if (p < 0) { p = -p - 1; } out.println(p - d); } else { out.println(0); } } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
JAVA
1016_B. Segment Occurrences
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.
2
8
#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); if (r - l + 1 < m) { printf("0\n"); continue; } check(l, r); printf("%d\n", ans); } return 0; }
CPP
1016_B. Segment Occurrences
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.
2
8
#include <bits/stdc++.h> int a, b, c, i, j, ch, x, y, S[1004]; char s[1004], w[1004]; int main() { scanf("%d%d%d", &a, &b, &c); scanf("%s%s", s, w); for (i = 0; i + b - 1 < a; i++) { ch = 0; for (j = 0; j < b; j++) { if (s[i + j] == w[j]) ch++; } if (ch == b) { S[i + 1]++; } S[i + 1] += S[i]; } while (c--) { scanf("%d%d", &x, &y); if (y - b + 1 >= x - 1) printf("%d\n", S[y - b + 1] - S[x - 1]); else printf("0\n"); } }
CPP
1016_B. Segment Occurrences
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.
2
8
import java.util.*; import java.io.*; import java.text.*; /** * * @author alanl */ public class Main{ static BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); public static void main(String[] args) throws IOException{ int n = readInt(), m = readInt(), q = readInt(); ArrayList<Integer>arr = new ArrayList(); String s = readLine(), t = readLine(); StringBuilder sb = new StringBuilder(); if(m>n){ for(int i = 0; i<q; i++){ System.out.println(0); } return; } for(int i = 0; i<=n; i++){ if(sb.length()==m){ if(sb.toString().equals(t)){ arr.add(i); } if(i==n)break; sb.deleteCharAt(0); sb.append(s.charAt(i)); } else sb.append(s.charAt(i)); } for(int i = 0; i<q; i++){ int a = readInt(), b = readInt(), ans = 0; for(int j = 0; j<arr.size(); j++){ if(b>=arr.get(j) && a<=arr.get(j)-m+1)ans++; } System.out.println(ans); } } static String next () throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(input.readLine().trim()); return st.nextToken(); } static long readLong () throws IOException { return Long.parseLong(next()); } static int readInt () throws IOException { return Integer.parseInt(next()); } static double readDouble () throws IOException { return Double.parseDouble(next()); } static char readChar () throws IOException { return next().charAt(0); } static String readLine () throws IOException { return input.readLine().trim(); } /* stuff you should look for * int overflow, array bounds * special cases (n=1?) * do smth instead of nothing and stay organized * WRITE STUFF DOWN * DON'T GET STUCK ON ONE APPROACH // Did you read the bounds? // Did you make typos? // Are there edge cases (N=1?) // Are array sizes proper (scaled by proper constant, for example 2* for koosaga tree) // Integer overflow? // DS reset properly between test cases? // Is using long longs causing TLE? // Are you using floating points? */ }
JAVA
1016_B. Segment Occurrences
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.
2
8
n, m, q = map(int, input().split()) s = input() t = input() arr = [0] * n for i in range(n - m + 1): arr[i] = arr[i-1] if s[i : i + m] == t: arr[i] += 1 # print(arr) for case in range(q): l, r = map(int, input().split()) l -= 1 r -= 1 # print(s[l:r+1]) # print(arr[]) if r - m + 1 < 0: print(0) else: if l == 0: print(arr[r-m+1]) else: if r - m + 1 > l - 1: print(arr[r-m+1] - arr[l-1]) else: print(0)
PYTHON3
1016_B. Segment Occurrences
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.
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class SegmentOccurences { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); int q = Integer.parseInt(st.nextToken()); StringTokenizer st1 = new StringTokenizer(br.readLine()); String query = st1.nextToken(); StringTokenizer st2 = new StringTokenizer(br.readLine()); String subString = st2.nextToken(); int total = 0; int[] dummy = new int[n]; for (int i = 0; i < Math.max(n - m + 1, 0); i++) { if (query.startsWith(subString, i)) { dummy[i]++; } } for (int i = 0; i < q; i++) { StringTokenizer st3 = new StringTokenizer(br.readLine()); int l = Integer.parseInt(st3.nextToken()); int r = Integer.parseInt(st3.nextToken()); int temp = 0; int start = l - 1; int end = r - m + 1; for (int j = start; j < Math.max(start,end); j++) { temp += dummy[j]; } System.out.println(temp); } } }
JAVA
1016_B. Segment Occurrences
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.
2
8
#include <bits/stdc++.h> using namespace std; char a[1000010]; char b[1000010]; int f[1000010]; int n, m, t; int main() { cin >> n >> m >> t; cin >> a + 1 >> b; for (int i = 1; i <= n - m + 1; i++) { bool mark = 1; for (int j = 0; j < m; j++) { if (a[i + j] != b[j]) { mark = 0; break; } } if (mark == 0) f[i] = f[i - 1]; else f[i] = f[i - 1] + 1; } while (t--) { int l, r; cin >> l >> r; if (r - l + 1 < m) puts("0"); else cout << f[r - m + 1] - f[l - 1] << "\n"; } return 0; }
CPP
1016_B. Segment Occurrences
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.
2
8
n,m,q=map(int,input().strip().split()) s=input() t=input() le=len(t) l1=[0 for i in range(n+2)] count=0 for i in range(0,n-le+1): if (s[i:i+le]==t): l1[i+1]=-1 count=count+1 l2=[0 for i in range(n+2)] l2[1]=count for i in range(2,n+2): l2[i]=l2[i-1]+l1[i-1] for i in range(q): r1,r2=map(int,input().strip().split()) if (r1>r2-le+2): print (0) continue print(l2[r1]-l2[r2-le+2])
PYTHON3
1016_B. Segment Occurrences
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.
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long n, m, q; scanf("%lld", &n), scanf("%lld", &m), scanf("%lld", &q); string s, t; cin >> s >> t; long long cs[n + 10]; cs[0] = 0; memset(cs, 0, sizeof cs); for (long long i = 0; i < n; i++) { bool f = true; if (n < m) { cs[i + 1] = 0; continue; } for (long long j = i, k = 0; j < n and k < m; j++, k++) { if (s[j] != t[k]) { f = false; } } if (f) { cs[i + 1] = cs[i] + 1; } else { cs[i + 1] = cs[i]; } } long long l, r; while (q--) { scanf("%lld", &l), scanf("%lld", &r); r = r - m + 1; if (l > r) { printf("0\n"); continue; } printf("%lld\n", cs[r] - cs[l - 1]); } }
CPP
1016_B. Segment Occurrences
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.
2
8
import java.util.Scanner; public class Main { public static void main(String[] args) throws Exception { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); int q = in.nextInt(); String s = in.next(); String t = in.next(); int[][] ar = new int[n][n + 1]; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n + 1; j++) { String s1 = s.substring(i, j); int k = 0; if (j - i - m > 0) { s1 = s1.substring(j - i - m); k = ar[i][j - 1]; } while (s1.contains(t)) { k++; s1 = s1.substring(s1.indexOf(t) + 1); } ar[i][j] = k; } } for (int i = 0; i < q; i++) { int l = in.nextInt(); int r = in.nextInt(); System.out.println(ar[l - 1][r]); } // System.out.println("codeforces".substring(4, 6)); } }
JAVA
1016_B. Segment Occurrences
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.
2
8
import bisect first = list(map(int, input().split())) a = input() b = input() n, m, q = first[0], first[1], first[2] starts = [] # preprocess string # brute force: determine each starting point for i in range(n - m + 1): if a[i:i + m] == b: starts.append(i + 1) for _ in range(q): endpoints = list(map(int, input().split())) print(max(0, bisect.bisect_left(starts, endpoints[1] - m + 2) - \ bisect.bisect_left(starts, endpoints[0])))
PYTHON3
1016_B. Segment Occurrences
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.
2
8
#include <bits/stdc++.h> using namespace std; int val[1005], rang[1005]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); string s, t; int n, m, q; cin >> n >> m >> q; cin >> s >> t; for (int i = 0; i < s.size(); i++) { int cnt = 0; for (int j = 0; j < t.size(); j++) { if (i + j >= s.size()) break; if (s[i + j] != t[j]) break; cnt++; } if (cnt == t.size()) val[i + 1]++; } rang[0] = 0; for (int i = 1; i < 1005; i++) { rang[i] = val[i] + rang[i - 1]; } while (q--) { int x, y; cin >> x >> y; y = y - (int)t.size() + 1; if (x > y) cout << 0 << '\n'; else cout << (rang[y] - rang[x - 1]) << '\n'; } return 0; }
CPP
1016_B. Segment Occurrences
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.
2
8
#include <bits/stdc++.h> using namespace std; vector<int> z_function(string s) { int n = (int)s.length(); vector<int> z(n); for (int i = 1, l = 0, r = 0; 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; } } return z; } int main() { int n, m, q; string s, t; while (cin >> n >> m >> q >> s >> t) { const auto z = z_function(t + '$' + s); vector<int> is_occurance(1 + n, 0), cumsum_ocuurances(1 + n); for (int i = 0; i <= n - m; ++i) { is_occurance[i + 1] = m == z[m + 1 + i]; } partial_sum(is_occurance.begin(), is_occurance.end(), cumsum_ocuurances.begin()); for (int i = 0; i < q; ++i) { int l, r; cin >> l >> r; cout << (r - l + 1 >= m ? cumsum_ocuurances[r - m + 1] - cumsum_ocuurances[l - 1] : 0) << endl; } } }
CPP
1016_B. Segment Occurrences
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.
2
8
from bisect import bisect_right as br from bisect import bisect_left as bl ar = map(int, raw_input().split()) s = raw_input() t = raw_input() lis = [] i = 0 while(1): i = s.find(t, i) if i == -1: break lis.append(i+1) i+=1 for i in range(0, ar[2]): a = map(int, raw_input().split()) e = br(lis, a[1]-ar[1]+1) s = bl(lis, a[0]) if( s < e ): print e-s else: print 0
PYTHON
1016_B. Segment Occurrences
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.
2
8
import java.io.*; import java.util.*; /* */ public class B { static FastReader sc=null; public static void main(String[] args) { sc=new FastReader(); PrintWriter out=new PrintWriter(System.out); int n=sc.nextInt(),m=sc.nextInt(),q=sc.nextInt(); char s[]=sc.next().toCharArray(),t[]=sc.next().toCharArray(); int counts[]=new int[n]; outer:for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(i+j>=n || s[i+j]!=t[j])continue outer; } counts[i]++; } //print(counts); int pre[]=new int[n]; for(int i=0;i<n;i++) { pre[i]=(i>0?pre[i-1]:0)+counts[i]; } //print(pre); while(q-->0) { int l=sc.nextInt()-1,r=sc.nextInt()-1; int rs=r-m+1; if(rs<l)out.println(0); else out.println(pre[rs]-(l>0?pre[l-1]:0)); //System.out.println(rs+" "+l); } out.close(); } static int[] reverse(int a[]) { ArrayList<Integer> al=new ArrayList<>(); for(int i:a)al.add(i); Collections.sort(al,Collections.reverseOrder()); for(int i=0;i<a.length;i++)a[i]=al.get(i); return a; } static int gcd(int a,int b) { if(b==0)return a; else return gcd(b,a%b); } static int[] ruffleSort(int a[]) { ArrayList<Integer> al=new ArrayList<>(); for(int i:a)al.add(i); Collections.sort(al); for(int i=0;i<a.length;i++)a[i]=al.get(i); return a; } static void print(int a[]) { for(int e:a) { System.out.print(e+" "); } System.out.println(); } static void print(long a[]) { for(long e:a) { System.out.print(e+" "); } System.out.println(); } static void print(char a[]) { for(char e:a) { System.out.print(e); } System.out.println(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int a[]=new int [n]; for(int i=0;i<n;i++) { a[i]=sc.nextInt(); } return a; } } }
JAVA
1016_B. Segment Occurrences
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.
2
8
n,m,q=map(int,input().split(" ")) s=input() t=input() o="" for i in range(0,n-m+1): if s[i:i+m]==t: o+="1" else: o+="0" for i in range(0,q): l,r=map(int,input().split(" ")) if r-l+1>=m: print(o[l-1:r-m+1].count("1")) else: print("0")
PYTHON3
1016_B. Segment Occurrences
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.
2
8
def preprocess(s, t): arr = [0 for i in s] for i in range(len(s)): for j in range(len(t)): if i + j >= len(s) or s[i + j] != t[j]: break else: arr[i] = 1 return arr def get_prefices(arr): prefices = [0 for i in arr] prefices[0] = arr[0] for i in range(1, len(arr)): prefices[i] = prefices[i - 1] + arr[i] prefices = [0] + prefices return prefices if __name__ == '__main__': n, m, q = [int(x) for x in raw_input().split()] s = raw_input() t = raw_input() arr = preprocess(s, t) pref = get_prefices(arr) # print pref for i in range(q): l, r = [int(x) for x in raw_input().split()] if r - l < m - 1: print 0 else: print pref[r - m + 1] - pref[l - 1]
PYTHON
1016_B. Segment Occurrences
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.
2
8
#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> amijanina; for (int i = 0; i < s.size(); i++) { bool iloveuazrin = true; for (int j = 0; j < t.size(); j++) { if (s[i + j] != t[j]) { iloveuazrin = false; break; } } if (iloveuazrin == true) { amijanina.push_back(i + 1); } } while (q--) { int x, y; cin >> x >> y; int ans = 0; for (int i = 0; i < amijanina.size(); i++) { if (amijanina[i] >= x and amijanina[i] + m - 1 <= y) { ans++; } } cout << ans << endl; } }
CPP
1016_B. Segment Occurrences
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.
2
8
#include <bits/stdc++.h> using namespace std; int n, m, q; string s, t; int a[1024]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m >> q; cin >> s >> t; for (int i = 0; i <= n - m; i++) { a[i] = 1; for (int j = 0; j < m; j++) { if (s[i + j] != t[j]) { a[i] = 0; break; } } } for (int i = 1; i < n; i++) a[i] += a[i - 1]; for (int i = 0; i < q; i++) { int l, r; cin >> l >> r; if (r - l + 1 < m) { cout << "0\n"; continue; } if (l == 1) cout << a[r - m] << '\n'; else cout << a[r - m] - a[l - 2] << '\n'; } return 0; }
CPP
1016_B. Segment Occurrences
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.
2
8
import java.util.*; import java.lang.*; public class Solution { static Scanner in = new Scanner(System.in); public static void main(String []args){ int n = in.nextInt(); int m = in.nextInt(); int q = in.nextInt(); String a = in.next(); String b = in.next(); char arra[] = a.toCharArray(); char arrb[] = b.toCharArray(); boolean flag[] = new boolean[n]; if(n >= m){ for(int i = 0; i < n-m+1; i++){ if(arrb[0] == arra[i]){ int j = 1; for(j = 1; j < m; j++){ if(arrb[j] != arra[i+j]) break; } if(j == m){ flag[i] = true; } } } } while(q-- > 0){ int count = 0; int start = in.nextInt(); int end = in.nextInt(); if(n < m){ System.out.println(0); } else{ for(int i = start-1; i < end-m+1; i++){ if(flag[i] == true) count++; } System.out.println(count); } } } }
JAVA
1016_B. Segment Occurrences
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.
2
8
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 5; int n, m, q, dp[N][N], p[N + N], used[N + N]; string s, t; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> q >> s >> t; string x = t + "#" + s; for (int i = 1; i < (int)x.size(); i++) { int j = p[i - 1]; while (j > 0 && x[i] != x[j]) { j = p[j - 1]; } if (x[i] == x[j]) { j++; } p[i] = j; if (p[i] == m) { used[i - m] = true; } } s = '#' + s; t = '%' + t; for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j++) { dp[i][j] = dp[i][j - 1] + (j - m + 1 >= i && used[j]); } } while (q--) { int l, r; cin >> l >> r; cout << dp[l][r] << "\n"; } }
CPP
1016_B. Segment Occurrences
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.
2
8
#include <bits/stdc++.h> using namespace std; const long long MX = 1e3 + 100, INF = 1e17, INF2 = -1e18, D = 1e9 + 7; long long par[MX]; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m, q; cin >> n >> m >> q; string s, t; cin >> s >> t; for (long long i = n - m; i >= 0; i--) { par[i] = par[i + 1] + (s.substr(i, m) == t); } while (q--) { long long l, r; cin >> l >> r; l--; cout << par[l] - par[max(l, r - m + 1)] << endl; } return 0; }
CPP
1016_B. Segment Occurrences
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.
2
8
n,m,q=map(int,raw_input().split()) a=raw_input();b=raw_input();k=len(b) pre=[0 for j in range(n)] pre2=[0 for j in range(n)] for i in range(n): if (i+k)<=n: if a[i:i+k]==b: pre[i]+=1 pre2[i+k-1]+=1 for i in range(1,n): pre[i]+=pre[i-1] for j in range(n-2,-1,-1): pre2[j]+=pre2[j+1] for i in range(q): a,b=map(int,raw_input().split()) a-=1;b-=1 if(b-a+1)<k: print 0;continue if a==0 and b==(n-1): print pre[-1] elif a==0: print pre2[0]- pre2[b+1] elif b==n-1: print pre[b]-pre[a-1] else: j= (pre[b]-pre[a-1])-(pre2[b+1]-pre[n-1]+pre[b]) print j
PYTHON
1016_B. Segment Occurrences
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.
2
8
from itertools import accumulate n,m,q = map(int,input().split()) a = input() b = input() acc = [0] + list(accumulate([ int(a[i:i+m] == b)for i in range(n-m+1)])) for i in range(q): x,y= map(int,input().split()) if n<m: print(0) else: print(max(0,acc[max(y-m+1,0)]-acc[min(x-1,n-m+1)]))
PYTHON3
1016_B. Segment Occurrences
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.
2
8
#include <bits/stdc++.h> using namespace std; char s[1005], t[1005]; int n, m, q; int c[1005]; int main() { scanf("%d%d%d", &n, &m, &q); scanf("%s", s + 1); scanf("%s", t + 1); for (int i = 1; i <= n; i++) { c[i] = c[i - 1]; int d = 1; for (int j = 1; j <= m; j++) { if (s[i + j - 1] != t[j]) d = 0; } c[i] += d; } for (int i = 0; i < q; i++) { int l, r; scanf("%d%d", &l, &r); if (r - m + 1 <= l - 1) { puts("0"); continue; } printf("%d\n", c[r - m + 1] - c[l - 1]); } return 0; }
CPP
1016_B. Segment Occurrences
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.
2
8
#include <bits/stdc++.h> const double PI = acos(-1.0); using namespace std; int setb(int n, int pos) { return n = n | (1 << pos); } int resb(int n, int pos) { return n = n & ~(1 << pos); } bool checkb(long long n, long long pos) { return (bool)(n & (1ll << pos)); } long long bigmod(long long b, long long p, long long m) { if (p == 0) return 1; long long ret = bigmod(b, p / 2, m); ret = (ret * ret) % m; if (p & 1) ret = (ret * b) % m; return ret; } string s, t; bool oka(int idx) { for (int i = idx, j = 0; j < t.size(); i++, j++) if (s[i] != t[j]) return false; return true; } int ara[1005]; int n, m, q; int F(int l, int r) { int ed = r - m + 1; int ret = 0; for (int i = l; i <= ed; i++) ret += ara[i]; return ret; } int main() { int x, y; scanf("%d %d %d", &n, &m, &q); cin >> s; cin >> t; for (int i = 0; i < n - m + 1; i++) ara[i] = oka(i); for (int i = 1; i <= q; i++) { scanf("%d %d", &x, &y); int ret = F(x - 1, y - 1); printf("%d", ret); printf("\n"); } return 0; }
CPP
1016_B. Segment Occurrences
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.
2
8
#include <bits/stdc++.h> using namespace std; long long n, m, f[200000], d[200000], i, j, l, r, q, edd; string s, t; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> q; cin >> s >> t; for (i = 0; i < n; i++) { if (s[i] == t[0] && i + m <= n) { edd = 1; for (j = i + 1; j < i + m; j++) if (s[j] != t[j - i]) { edd = 0; break; } f[i + 1] = edd; } d[i + 1] = d[i] + f[i + 1]; } while (q--) { cin >> l >> r; if (r - m + 1 < l) cout << 0 << "\n"; else cout << d[r - m + 1] - d[l - 1] << "\n"; } return 0; }
CPP
1016_B. Segment Occurrences
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.
2
8
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 7; const int M = 1e5 + 7; const long double pi = 3.1415926535897; int n, m, q, l, r, cnt[N]; string s, t; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> q; cin >> s; cin >> t; s = ' ' + s; t = ' ' + t; for (int i = m; i <= n; ++i) { cnt[i] = cnt[i - 1]; int flag = 1; for (int j = i - m + 1; j <= i; ++j) { if (s[j] != t[j - (i - m)]) { flag = false; break; } } if (flag) ++cnt[i]; } for (int i = 1; i <= q; ++i) { cin >> l >> r; if (r - l + 1 < m) cout << 0 << '\n'; else cout << cnt[r] - cnt[l + m - 2] << '\n'; } }
CPP
1016_B. Segment Occurrences
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.
2
8
import sys def get_array(): return list(map(int, sys.stdin.readline().split())) def get_ints(): return map(int, sys.stdin.readline().split()) def input(): return sys.stdin.readline().strip('\n') n , m , q = get_ints() s = input() p = input() ans = [] i = 0 count = 0 while True: si = s.find(p,i) if si != -1: ans += (si-i+1)*[count] count += 1 i = si+1 else: ans += [count]*(n-i+2) break out = '' for i in range(q): a , b = get_ints() a -= 1 b -= m-1 out += str(ans[b]-ans[a] if b >= a else 0 ) + '\n' sys.stdout.write(out)
PYTHON3
1016_B. Segment Occurrences
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.
2
8
#include <bits/stdc++.h> #pragma GCC optimize "Ofast" #pragma GCC optimize "unroll-loops" #pragma GCC target "sse,sse2,sse3,sse4,abm,avx,mmx,popcnt,tune=native" char _; using namespace std; string a, b; int n, m, q, c, d; int PSA[3010]; int main() { cin.sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> q >> a >> b; for (int i = 0; i + m <= n; i++) if (a.substr(i, m) == b) PSA[i + 1]++; for (int i = 2; i <= n; i++) PSA[i] += PSA[i - 1]; while (q--) { cin >> c >> d; d = d - m + 1; cout << (max(0, PSA[max(0, d)] - PSA[c - 1])) << "\n"; } return 0; }
CPP
1016_B. Segment Occurrences
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.
2
8
lena, lenb, noq = [int(x) for x in input().split()] a = input() b = input() ''' for i in range(noq): s, e = [int(x)-1 for x in input().split()] curr = a[s:e+1] #print(curr) ans = sum(1 for _ in re.finditer('(?='+b+')', curr)) print(ans) ''' cnts = [0] * lena for i in range(lena): cnts[i] += a[i:i+lenb] == b for i in range(noq): s, e = [int(x) for x in input().split()] s, e = s-1, e - lenb + 1 ans = 0 if s < e :ans = (sum(cnts[s:e])) print(ans)
PYTHON3