text
stringlengths
291
465k
### Prompt Construct a Java code solution to the problem outlined: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```java /* If you want to aim high, aim high Don't let that studying and grades consume you Just live life young ****************************** If I'm the sun, you're the moon Because when I go up, you go down ******************************* I'm working for the day I will surpass you https://www.a2oj.com/Ladder16.html */ import java.util.*; import java.io.*; import java.math.*; public class B { public static void main(String omkar[]) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); int N = Integer.parseInt(st.nextToken()); int M = Integer.parseInt(st.nextToken()); int Q = Integer.parseInt(st.nextToken()); String input = infile.readLine(); String pat = infile.readLine(); int[] arr = new int[N]; if(N >= M) for(int left=0; left+M-1 < N; left++) { int right = left+M-1; if(pat.equals(input.substring(left, right+1))) arr[left] = 1; } StringBuilder sb = new StringBuilder(); while(Q-->0) { st = new StringTokenizer(infile.readLine()); int L = Integer.parseInt(st.nextToken()); int R = Integer.parseInt(st.nextToken()); L--; R--; int res = 0; for(int i=L; i <= R; i++) if(i+M-1 <= R) res += arr[i]; sb.append(res+"\n"); } System.out.print(sb); } } ```
### Prompt Generate a JAVA solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```java import java.util.*; public class CoinsAndQueries { public static ArrayList<Integer>[] arr; public static int moves=0; static Scanner scan=new Scanner(System.in); public static void main(String[] args) { int n, m,q; n=scan.nextInt(); m=scan.nextInt(); q=scan.nextInt(); String s=scan.next(); String t=scan.next(); int len=t.length(); int[] arr=new int[n]; int[] res=new int[q]; int idx=s.indexOf(t); while(idx!=-1 && idx<=n-m){ arr[idx]=1; idx=s.indexOf(t,idx+1); } int a,b;int i=0; while(i!=q){ int count=0; a=scan.nextInt(); b=scan.nextInt(); for(int j=a-1;j<=b-m;j++){ if(arr[j]==1) count++; } res[i++]=count; } for(i=0;i<q;i++){ System.out.println(res[i]); } } } ```
### Prompt Your challenge is to write a java solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```java import java.io.*; import java.util.*; public class HelloWorld{ public static void main(String []args)throws IOException { //System.out.println("Hello World"); 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()); ArrayList<Integer> sta=new ArrayList<Integer>(); ArrayList<Integer> fin=new ArrayList<Integer>(); String s=br.readLine(); String t=br.readLine();char c=t.charAt(0); for(int i=0;i<n;i++) { if(s.charAt(i)==c) { //System.out.println(i+" "+s.charAt(i)+" "+s.substring(i,i+m)); if(i+m<=n) { if(s.substring(i,i+m).equals(t)) { sta.add(i+1); fin.add(i+m); // System.out.println(i+" "+(i+m-1)); } } } } for(int i=0;i<q;i++) { int co=0; st=new StringTokenizer(br.readLine()); int l=Integer.parseInt(st.nextToken()); int r=Integer.parseInt(st.nextToken()); for(int j=0;j<sta.size();j++) { if(sta.get(j)>=l && fin.get(j)<=r) { co++; } } System.out.println(co); } } } ```
### Prompt Please create a solution in Python3 to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python3 # from math import ceil #from sys import stdout t = 1#int(input()) for test in range(1,t+1): n,m,q = map(int, input().split()) s = input() t = input() indices = [0 for i in range(n)] for i in range(n): tmp = s.find(t, i) if tmp==-1: break else: indices[tmp] = 1 pref = [0] for i in indices: pref.append(i+pref[-1]) for i in range(q): l,r = map(int, input().split()) if r-l+1<m: print(0) continue print(pref[r-m+1]-pref[l-1]) ```
### Prompt Construct a python3 code solution to the problem outlined: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python3 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()) print(max(0,acc[max(y-m+1,0)]-acc[max(0,min(x-1,n-m+1))])) ```
### Prompt Generate a Cpp solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m, q, i, j; cin >> n >> m >> q; string a, b; cin >> a >> b; vector<long long> v(n + 1); for (i = 0; i <= n - m; i++) { string tmp = a.substr(i, m); if (tmp == b) v[i + 1] = 1; } for (i = 1; i <= n; i++) v[i] += v[i - 1]; while (q--) { long long l, r, tot = 0; cin >> l >> r; r = r - m + 1; long long ans = 0; if (r >= l) ans = v[r] - v[l - 1]; cout << ans << endl; } } ```
### Prompt Please provide a java coded solution to the problem described below: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```java import java.io.*; import java.util.*; public class CF1016_B { public static void main(String[] args)throws Throwable { MyScanner sc=new MyScanner(); PrintWriter pw=new PrintWriter(System.out); int n=sc.nextInt(); int m=sc.nextInt(); int q=sc.nextInt(); char [] s=sc.next().toCharArray(); char [] t=sc.next().toCharArray(); boolean [] ok=new boolean[n]; for(int i=0;i+m-1<n;i++){ ok[i]=true; for(int j=0;j<m;j++) if(s[i+j]!=t[j]) ok[i]=false; } int [] c=new int [n]; c[0]=ok[0]? 1 : 0; for(int i=1;i<n;i++) c[i]=c[i-1]+(ok[i]? 1 : 0); while(q-->0){ int l=sc.nextInt()-1; int r=sc.nextInt()-1-(m-1); if(r<l){ pw.println(0); continue; } int ans =c[r]; if(l>0) ans-=c[l-1]; pw.println(ans); } pw.flush(); pw.close(); } static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { 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;} } } ```
### Prompt Create a solution in Cpp for the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); ; long long int n, m, q; cin >> n >> m >> q; string s, t; cin >> s >> t; long long int l, r; long long int cnt; long long int ans[10005] = {0}; for (long long int i = 0; i < (n - m + 1); i++) { if (s.substr(i, m) == t) { ans[i] = 1; } } while (q--) { cnt = 0; cin >> l >> r; for (long long int i = l - 1; i < (r - m + 1); i++) { if (ans[i] == 1) cnt++; } cout << cnt << endl; } return 0; } ```
### Prompt Please create a solution in CPP to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); int n, m, q; cin >> n >> m >> q; int cnt[n + 1]; string s, s1; cin >> s >> s1; s = "1" + s; int k = s.find(s1); if (k == string::npos) fill(cnt, cnt + n, 0); else { fill(cnt, cnt + k, 0); cnt[k] = 1; } while (k != n + 1) { int l = k; k = s.find(s1, k + 1); if (k == string::npos) k = n + 1; fill(cnt + l + 1, cnt + k, cnt[l]); if (k != n + 1) cnt[k] = cnt[l] + 1; } int l, r; for (int i = 0; i < q; i++) { cin >> l >> r; if (r < l + m - 1) cout << 0 << endl; else cout << cnt[r - m + 1] - cnt[l - 1] << endl; } return 0; } ```
### Prompt Construct a CPP code solution to the problem outlined: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, m, q; cin >> n >> m >> q; string s1, s2; cin >> s1 >> s2; vector<pair<int, int> > p; int ans = 1; for (int i = 0; i < n; i++) { if (s1[i] == s2[0]) { for (int k = 1; k < m && i + k < n; k++) { if (s1[i + k] == s2[k]) ans++; } if (ans == m) p.push_back(make_pair(i + 1, i + m)); ans = 1; } } if (n >= m) { while (q--) { int a, b, la = 0; cin >> a >> b; for (int i = 0; i < p.size(); i++) { if (p[i].first >= a && p[i].second <= b) la++; } cout << la << endl; } } else { while (q--) { cout << "0" << endl; } } } ```
### Prompt Please create a solution in Python3 to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python3 R=lambda:map(int,input().split()) n,m,q=R() s,t=input(),input() a=[0]*1005 for i in range(n):a[i+2]=a[i+1]+(s[i:i+m]==t) for _ in[0]*q:l,r=R();print(a[max(l,r-m+2)]-a[l]) ```
### Prompt Develop a solution in Java to the problem described below: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```java import java.io.*; import java.util.*; public class Main { static int KMPSearch(String pat, String txt) { int M = pat.length(); int N = txt.length(); int count = 0; int lps[] = new int[M]; int j = 0; computeLPSArray(pat,M,lps); int i = 0; while (i < N) { if (pat.charAt(j) == txt.charAt(i)) { j++; i++; } if (j == M) { count++; // System.out.println("Found pattern " + "at index " + (i-j)); j = lps[j-1]; } else if (i < N && pat.charAt(j) != txt.charAt(i)) { if (j != 0) j = lps[j-1]; else i = i+1; } } return count; } static void computeLPSArray(String pat, int M, int lps[]) { int len = 0; int i = 1; lps[0] = 0; while (i < M) { if (pat.charAt(i) == pat.charAt(len)) { len++; lps[i] = len; i++; } else { if (len != 0) { len = lps[len-1]; } else { lps[i] = len; i++; } } } } public static void main(String[] args) throws IOException { Reader in=new Reader(); // int t = ni(); int t = 1; while(t-->0) { int n = ni(); int m = ni(); int q = ni(); String s = ns(); String st = ns(); while(q-->0) { int l = ni(); int r = ni(); int count = 0; // for(int i=l-1;i<=r-m;i++) { // if(s.substring(i,i+m).equals(st)){ // new KMP_String_Matching().KMPSearch(pat,txt); // count++; // } // System.out.println(s.substring(l-1,r)+" "+st); count = KMPSearch(st,s.substring(l-1,r)); // } ans.append(count + "\n"); } } System.out.println(ans); } static final int mod=1000000007; static StringBuilder ans=new StringBuilder(); static Reader in=new Reader(); static final double eps=1e-9; ////////////////////////////////// //input functions///////////////// static int ni(){return Integer.parseInt(in.next());} static long nl(){return Long.parseLong(in.next());} static String ns(){return in.next();} static double nd(){return Double.parseDouble(in.next());} static int[] nia(int n){int a[]=new int[n];for(int i=0; i<n; i++)a[i]=ni();return a;} static int[] pnia(int n){int a[]=new int[n+1];for(int i=1; i<=n; i++)a[i]=ni();return a;} static long[] nla(int n){long a[]=new long[n];for(int i=0; i<n; i++)a[i]=nl();return a;} static String[] nsa(int n){String a[]=new String[n];for(int i=0; i<n; i++)a[i]=ns();return a;} static double[] nda(int n){double a[]=new double[n];for(int i=0; i<n; i++)a[i]=nd();return a;} //////output functions//////////////// static void pr(Object a){ans.append(a+"\n");} static void pr(){ans.append("\n");} static void p(Object a){ans.append(a);} static void pra(int[]a){for(int i:a)ans.append(i+" ");ans.append("\n");} static void pra(long[]a){for(long i:a)ans.append(i+" ");ans.append("\n");} static void pra(String[]a){for(String i:a)ans.append(i+" ");ans.append("\n");} static void pra(double[]a){for(double i:a)ans.append(i+" ");ans.append("\n");} static void sop(Object a){System.out.println(a);} static void flush(){System.out.print(ans);ans=new StringBuilder();} static void exit(){System.out.print(ans);System.exit(0);} static int min(int... a){int min=a[0];for(int i:a)min=Math.min(min, i);return min;} static int max(int... a){int max=a[0];for(int i:a)max=Math.max(max, i);return max;} static int gcd(int... a){int gcd=a[0];for(int i:a)gcd=gcd(gcd, i);return gcd;} static long min(long... a){long min=a[0];for(long i:a)min=Math.min(min, i);return min;} static long max(long... a){long max=a[0];for(long i:a)max=Math.max(max, i);return max;} static long gcd(long... a){long gcd=a[0];for(long i:a)gcd=gcd(gcd, i);return gcd;} static String pr(String a, long b){String c="";while(b>0){if(b%2==1)c=c.concat(a);a=a.concat(a);b>>=1;}return c;} static long powm(long a, long b, long m){long an=1;long c=a;while(b>0){if(b%2==1)an=(an*c)%m;c=(c*c)%m;b>>=1;}return an;} static int gcd(int a, int b){if(b==0)return a;return gcd(b, a%b);} static long gcd(long a, long b){if(b==0)return a;return gcd(b, a%b);} static class Reader { BufferedReader reader; StringTokenizer tokenizer; public Reader() { reader = new BufferedReader(new InputStreamReader(System.in), 32768); tokenizer = null; } String next() {while (tokenizer == null || !tokenizer.hasMoreTokens()) { try {tokenizer = new StringTokenizer(reader.readLine());} catch (IOException e) {throw new RuntimeException(e);}} return tokenizer.nextToken();} int nextInt() {return Integer.parseInt(next());} long nextLong() {return Long.parseLong(next());} double nextDouble() {return Double.parseDouble(next());} } } ```
### Prompt Develop a solution in Python3 to the problem described below: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python3 n, m, q = map(int, input().split()) s = input() t = input() c = [0 for i in range(n+1)] b = 0 for i in range(n-m+1): #print (s[i:i+m]) if s[i:i+m] == t: c[i+1] = 1 for i in range(1,n+1): c[i] += c[i-1] for i in range(q): l, r = map(int, input().split()) if r-l+1 < m: print (0) else: if l == 1: print (c[r-m+1]) else: print (c[r-m+1]-c[l-1]) ```
### Prompt Develop a solution in Java to the problem described below: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```java 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.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); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.ni(); int m = in.ni(); int q = in.ni(); String s = in.next(); String t = in.next(); int[] start = new int[n]; int[] end = new int[n]; outer: for (int i = 0; i < n - m + 1; ++i) { int curr = i; for (int j = 0; j < m; ++j, ++curr) { if (s.charAt(curr) != t.charAt(j)) continue outer; } start[i]++; end[i + m - 1]++; } for (int i = 1; i < n; ++i) { start[i] += start[i - 1]; end[i] += end[i - 1]; } for (int i = 0; i < q; ++i) { int l = in.ni() - 1; int r = in.ni() - 1; if (r - m + 1 < 0) out.println("0"); else { int curr = 0; if (l > 0) curr = start[l - 1]; int ans = start[r - m + 1] - curr; if (ans < 0) out.println("0"); else out.println(ans); } } } } 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 ni() { 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 nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return nextString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } } ```
### Prompt Develop a solution in Cpp to the problem described below: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 2e3; char s[MAXN]; char t[MAXN]; int Begin[MAXN]; int main() { int n, m, q; int ans; int i, j; int l, r; cin >> n >> m >> q; cin >> s + 1; cin >> t + 1; for (i = 1; i + m - 1 <= n; i++) { for (j = 1; j <= m; j++) { if (s[i + j - 1] != t[j]) break; } if (j > m) Begin[i] = 1; } while (q--) { ans = 0; cin >> l >> r; for (i = l; i + m - 1 <= r; i++) ans += Begin[i]; printf("%d\n", ans); } return 0; } ```
### Prompt In python3, your task is to solve the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python3 def prefix_func(s): slen, k = len(s), 0 p = [0]*slen p[0] = 0 for i in range(1, slen): while k>0 and s[k] != s[i]: k = p[k-1] if s[k] == s[i]: k += 1 p[i] = k return p def kmp(s, p): q, slen, plen = 0, len(s), len(p) res = [] pi = prefix_func(p) for i in range(slen): while q>0 and p[q] != s[i]: q = pi[q-1] if p[q] == s[i]: q += 1 if q == plen: res.append(i-plen+1) q = pi[q-1] return res n,m,q = map(int, input().split()) s = input() t = input() matches = kmp(s,t) match_count = [0]*(len(s)+1) for m in matches: match_count[m] = 1 count = 0 for i in range(len(s)-1, 0, -1): match_count[i-1] += match_count[i] for i in range(q): l, r = map(int, input().split()) r += 1 ans = 0 if r-l >= len(t): ans = match_count[l-1] - match_count[r-len(t)] print(ans) ```
### Prompt Please provide a PYTHON3 coded solution to the problem described below: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python3 n,m,k=map(int,input().split()) s=input() t=input() ans=[0]*n for i in range(n-m+1): if s[i:i+m]==t: ans[i]=1 for tc in range(k): x,y=map(int,input().split()) if y-x+1<m: print(0) else: print(sum(ans[x-1:y-m+1])) ```
### Prompt Construct a cpp code solution to the problem outlined: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, m, q; int i, j; char s[1005]; char t[1005]; int nx[1005]; int ck[1005]; int main() { scanf("%d%d%d", &n, &m, &q); scanf("%s", s + 1); scanf("%s", t + 1); for (i = 2; i <= m; i++) { while (j && t[j + 1] != t[i]) j = nx[j]; if (t[j + 1] == t[i]) j++; nx[i] = j; } j = 0; for (i = 1; i <= n; i++) { while (j && t[j + 1] != s[i]) j = nx[j]; if (t[j + 1] == s[i]) j++; if (j == m) { ck[i] = 1; j = nx[j]; } } for (i = 1; i <= n; i++) ck[i] += ck[i - 1]; while (q--) { scanf("%d%d", &i, &j); if (i + m - 2 < j) printf("%d\n", ck[j] - ck[i + m - 2]); else printf("0\n"); } return 0; } ```
### Prompt Your task is to create a cpp solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; template <class T> using V = vector<T>; template <class T> using VV = V<V<T> >; const int maxn = (int)1e5 + 5; const int inf = (int)1e9 + 7; const ll linf = (ll)1e18 + 7; const ld pi = acosl(-1.0); const ld eps = 1e-9; void S() { int n, m, q; cin >> n >> m >> q; string s, t; cin >> s >> t; set<int> p; for (int i = 0; i < n; i++) if (s.substr(i, m) == t) p.insert(i); while (q--) { int l, r; cin >> l >> r; int an = 0; for_each((p).begin(), (p).end(), [&](int cur) { an += cur >= (l - 1) && cur + m <= r; }); cout << an << '\n'; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); S(); return 0; } ```
### Prompt Construct a JAVA code solution to the problem outlined: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```java import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Main { private static int[] arr; private static int[] root; private static int[] left; private static int[] right; public static void main(String[] args) { // TODO Auto-generated method stub Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int L = scanner.nextInt(); int N = scanner.nextInt(); int M = scanner.nextInt(); String s = scanner.next(); String t = scanner.next(); arr = new int [s.length()+1]; for(int i = 0; i < s.length() - t.length() + 1; i+= 0) { int index = s.substring(i).indexOf(t); if(index == -1) break; arr[index+1+i] = 1; i += index + 1; } root = new int [4*arr.length+10]; left = new int [4*arr.length+10]; right = new int [4*arr.length+10]; creat(1, 1, s.length()); while (M -- > 0) { int a = scanner.nextInt(); int b = scanner.nextInt(); b = b - t.length() + 1; if (a > b) { System.out.println(0); continue; } System.out.println(select(1, a, b)); } } } private static int select(int i, int a, int b) { // TODO Auto-generated method stub if(left[i] > b || right[i] < a) return 0; if(left[i] >= a && right[i] <= b) return root[i]; return select(i<<1, a, b)+select(i<<1|1, a, b); } private static void creat(int i, int s, int e) { // TODO Auto-generated method stub left[i] = s; right[i] = e; if(s == e) { root[i] = arr[s]; return; } int mid = (s+e)/2; creat(i<<1, s, mid); creat(i<<1|1, mid+1, e); root[i] = root[i<<1] + root[i<<1|1]; } } ```
### Prompt Please create a solution in Java to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```java import java.util.*; public class Main { static ArrayList<Integer> occ; public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(), m = scan.nextInt(),q = scan.nextInt(); char s[] = scan.next().toCharArray(); char t[] = scan.next().toCharArray(); occ = new ArrayList(); for(int i=0;i<=n-m;++i) { int cnt = 0; for(int j=0;j<m;++j) if(s[i+j]==t[j]) cnt++; else break; if(cnt==m) occ.add(i); } while(q-->0) { int l = scan.nextInt()-1,r = scan.nextInt()-m,ans = 0; for(int e:occ) if(e>=l && e<=r) ++ans; System.out.println(ans); } } } ```
### Prompt In Java, your task is to solve the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```java import java.util.*; public class MyClass { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int m=sc.nextInt(); int qn=sc.nextInt(); String s=sc.next(); String t=sc.next(); int q[][]=new int [qn][2]; for(int i=0;i<qn;i++) { q[i][0]=sc.nextInt()-1; q[i][1]=sc.nextInt()-1; } ArrayList<node> occ=new ArrayList<>(); int resd=0; while(true) { int temp=s.indexOf(t); if(temp>=0) { occ.add(new node(resd+temp,resd+temp+m-1)); resd=resd+temp+1; s=s.substring(temp+1); } else break; } //System.out.println(occ.toString()); int l=occ.size(); /* for(int i=0;i<l;i++) { node temp=occ.get(i); System.out.println(temp.x+" "+temp.y); } */ for(int i=0;i<qn;i++) { int si=q[i][0]; int ei=q[i][1]; int res=0; for(int j=0;j<l;j++) { node temp=occ.get(j); if(temp.x>=si&&temp.y<=ei) res++; } System.out.println(res); } } } class node { int x;int y; public node(int x,int y) { this.x=x; this.y=y; } } ```
### Prompt In python3, your task is to solve the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python3 from sys import stdin def main(): n, m , q = [ int(s) for s in stdin.readline().split(" ")] s = stdin.readline().strip() t = stdin.readline().strip() queries = [] for line in range(0, q) : queries.append( [ int(s) for s in stdin.readline().split(" ")]) acum = [0 for i in range(n+1)] for i in range( 1 , n+1): acum[i] = acum[i -1] if checkIndex(i, s, t, n, m): acum[i]+=1 for query in queries: if(query[1]- query[0] + 1< m): print(0) else: print(acum[query[1]-m+1] - acum[query[0]-1]) def checkIndex(fromIndex, s, t, n , m): valid = True if( n - fromIndex + 1 < m): return False for i in range(0, m): valid = valid and (t[i] == s[fromIndex + i-1]) if not valid: break return valid main() ```
### Prompt Please formulate a PYTHON3 solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python3 a=[] b=[] for i in range(3): a.append(input()) for n in range(int(a[0].split(" ")[2])): b.append(input()) c=0 # while c<len(b): # l=int(b[c].split(" ")[0]) # r=int(b[c].split(" ")[1]) # # d=a[1][l-1:r] # # size = [] # h=0 # while h < (len(d)-len(a[2])+1 ): # # # size.append(d[h:len(a[2])+h].count(a[2])) # h=h+1 # # # # print(sum(size)) # c=c+1 lent=0 d=[] sum=[] sum.append(0) while lent<=int(a[0].split(" ")[0])-int(a[0].split(" ")[1]): # print(a[1][lent:int(a[0].split(" ")[1])+lent],lent,int(a[0].split(" ")[1])+lent) # print(a[2]) if a[1][lent:int(a[0].split(" ")[1])+lent]== a[2]: d.append(1) else: d.append(0) sum.append(sum[lent]+d[lent]) lent=lent+1 # print(d,sum) for z in range(len(b)): l=int(b[z].split(" ")[0]) r=int(b[z].split(" ")[1])-int(a[0].split(" ")[1])+1 # print(l,r,d[l:r]) if int(b[z].split(" ")[1])-l>=int(a[0].split(" ")[1])-1: print(sum[r]-sum[l-1]) else: print(0) ```
### Prompt Generate a PYTHON3 solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python3 from itertools import accumulate N, M, Q = [int(x) for x in input().split()] S = input() T = input() L, R = [0]*Q, [0]*Q for i in range(Q): L[i], R[i] = [int(x) for x in input().split()] is_substr = [0]*N for i in range(N): if S[i:i+M] == T: is_substr[i] = 1 prefixes = [0]+list(accumulate(is_substr)) output = [0]*Q for i in range(Q): if R[i]-L[i]+1 < M: continue output[i] = prefixes[R[i]-M+1] - prefixes[L[i]-1] print("\n".join(map(str,output))) ```
### Prompt Please create a solution in Java to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```java import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; public class r48p2{ static void update(int bit[], int val, int pos){ if(pos >= bit.length) return; bit[pos]+=val; update(bit, val, pos+(pos&(-pos))); } static int query(int bit[], int pos){ if(pos <= 0) return 0; return bit[pos]+query(bit, pos-(pos&(-pos))); } public static void main(String args[]) { InputReader sc = new InputReader(System.in); PrintWriter pw = new PrintWriter(System.out); int n = sc.nextInt(), m = sc.nextInt(), q = sc.nextInt(), bit[] = new int[n+1]; String s = sc.readString(), t = sc.readString(); for(int i=0; i<=n-m; i++){ String k = s.substring(i, i+m); if(k.equals(t)) update(bit, 1, i+1); } /* for(int i=0; i<=n; i++) pw.print(bit[i]+" "); */ while(q-->0){ int l = sc.nextInt(), r = sc.nextInt()-m+1; if(l>r) pw.println(0); else pw.println(query(bit, r)-query(bit, l-1)); } pw.flush(); pw.close(); } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; InputReader(InputStream stream) { this.stream = stream; } int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } int nextInt() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } long nextLong() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } String nextLine() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(c)); return res.toString(); } boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { boolean isSpaceChar(int ch); } } } ```
### Prompt Your task is to create a Cpp solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; char s1[(20ll + (long long int)1e3)]; char s2[(20ll + (long long int)1e3)]; long long int xds[(20ll + (long long int)1e3) << 2]; long long int eds[(20ll + (long long int)1e3) << 2]; long long int n, m; long long int pushup(long long int cur) { return xds[cur] = xds[cur << 1] + xds[cur << 1 | 1]; } long long int init(long long int l, long long int r, long long int cur) { if (l + 1 == r) return xds[cur] = 0; long long int m = (l + r) >> 1; init(l, m, cur << 1); init(m, r, cur << 1 | 1); return pushup(cur); } long long int modify(long long int l, long long int r, long long int pos, long long int cur) { if (l + 1 == r) return xds[cur] += 1; long long int m = (l + r) >> 1; if (pos < m) modify(l, m, pos, cur << 1); else modify(m, r, pos, cur << 1 | 1); return pushup(cur); } long long int query(long long int l, long long int r, long long int L, long long int R, long long int cur) { if (l + 1 == r) return xds[cur]; if (l >= L && r <= R) return xds[cur]; long long int m = l + r >> 1; long long int acc = 0; if (m > L) acc += query(l, m, L, R, cur << 1); if (m < R) acc += query(m, r, L, R, cur << 1 | 1); return acc; } int main() { scanf( "%lld" "%lld", &n, &m); long long int q; scanf("%lld", &q); scanf("%s", s1 + 1); scanf("%s", s2 + 1); long long int le = n - m + 1; for (long long int i = 1; i <= le; i++) { if (!strncmp(&s1[i], s2 + 1, m)) { modify(1, n + 1, i, 1); } } for (long long int i = 0; i < q; i++) { long long int l, r; scanf( "%lld" "%lld", &l, &r); if (r >= m) { r += 2; r -= m; printf( "%lld" "\n", query(1, n + 1, l, r, 1)); } else { printf( "%lld" "\n", 0ll); } } return 0; } ```
### Prompt Please provide a PYTHON3 coded solution to the problem described below: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python3 from sys import stdin, stdout n,m,q=map(int,input().split()) s=input() t=input() x=0 dp1=[] while x<n: if s[x-m+1:x+1]==t: dp1.append(1) else: dp1.append(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 for i in range(q): l,r=map(int,input().split()) stdout.write(str(dp[l-1][r-1])+chr(10)) ```
### Prompt Please formulate a JAVA solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```java //package baobab; import java.io.*; import java.util.*; public class B { public static void main(String[] args) { Solver solver = new Solver(); } static class Solver { IO io; public Solver() { this.io = new IO(); try { solve(); } catch (RuntimeException e) { if (!e.getMessage().equals("Clean exit")) { throw e; } } finally { io.close(); } } /****************************** START READING HERE ********************************/ void solve() { int n = io.nextInt(); int m = io.nextInt(); int numberOfQueries = io.nextInt(); String s = io.next(); String t = io.next(); SegmentTree seg = new SegmentTree(n, true, false, false); for (int end=n-1; end>=m-1; end--) { int start = end-m+1; if (s.substring(start, end+1).equals(t)) seg.modifyRange(1, end, end); } for (int q=0; q<numberOfQueries; q++) { int left = io.nextInt() + m - 1 - 1; int right = io.nextInt() - 1; io.println(seg.getSum(left, right)); } } /************************** UTILITY CODE BELOW THIS LINE **************************/ long MOD = (long)1e9 + 7; boolean closeToZero(double v) { // Check if double is close to zero, considering precision issues. return Math.abs(v) <= 0.0000000000001; } class DrawGrid { void draw(boolean[][] d) { System.out.print(" "); for (int x=0; x<d[0].length; x++) { System.out.print(" " + x + " "); } System.out.println(""); for (int y=0; y<d.length; y++) { System.out.print(y + " "); for (int x=0; x<d[0].length; x++) { System.out.print((d[y][x] ? "[x]" : "[ ]")); } System.out.println(""); } } void draw(int[][] d) { int max = 1; for (int y=0; y<d.length; y++) { for (int x=0; x<d[0].length; x++) { max = Math.max(max, ("" + d[y][x]).length()); } } System.out.print(" "); String format = "%" + (max+2) + "s"; for (int x=0; x<d[0].length; x++) { System.out.print(String.format(format, x) + " "); } format = "%" + (max) + "s"; System.out.println(""); for (int y=0; y<d.length; y++) { System.out.print(y + " "); for (int x=0; x<d[0].length; x++) { System.out.print(" [" + String.format(format, (d[y][x])) + "]"); } System.out.println(""); } } } class IDval implements Comparable<IDval> { int id; long val; public IDval(int id, long val) { this.val = val; this.id = id; } @Override public int compareTo(IDval o) { if (this.val < o.val) return -1; if (this.val > o.val) return 1; return this.id - o.id; } } private class ElementCounter { private HashMap<Long, Integer> elements; public ElementCounter() { elements = new HashMap<>(); } public void add(long element) { int count = 1; Integer prev = elements.get(element); if (prev != null) count += prev; elements.put(element, count); } public void remove(long element) { int count = elements.remove(element); count--; if (count > 0) elements.put(element, count); } public int get(long element) { Integer val = elements.get(element); if (val == null) return 0; return val; } public int size() { return elements.size(); } } class StringCounter { HashMap<String, Integer> elements; public StringCounter() { elements = new HashMap<>(); } public void add(String identifier) { int count = 1; Integer prev = elements.get(identifier); if (prev != null) count += prev; elements.put(identifier, count); } public void remove(String identifier) { int count = elements.remove(identifier); count--; if (count > 0) elements.put(identifier, count); } public long get(String identifier) { Integer val = elements.get(identifier); if (val == null) return 0; return val; } public int size() { return elements.size(); } } class DisjointSet { /** Union Find / Disjoint Set data structure. */ int[] size; int[] parent; int componentCount; public DisjointSet(int n) { componentCount = n; size = new int[n]; parent = new int[n]; for (int i=0; i<n; i++) parent[i] = i; for (int i=0; i<n; i++) size[i] = 1; } public void join(int a, int b) { /* Find roots */ int rootA = parent[a]; int rootB = parent[b]; while (rootA != parent[rootA]) rootA = parent[rootA]; while (rootB != parent[rootB]) rootB = parent[rootB]; if (rootA == rootB) { /* Already in the same set */ return; } /* Merge smaller set into larger set. */ if (size[rootA] > size[rootB]) { size[rootA] += size[rootB]; parent[rootB] = rootA; } else { size[rootB] += size[rootA]; parent[rootA] = rootB; } componentCount--; } } class Trie { int N; int Z; int nextFreeId; int[][] pointers; boolean[] end; /** maxLenSum = maximum possible sum of length of words */ public Trie(int maxLenSum, int alphabetSize) { this.N = maxLenSum; this.Z = alphabetSize; this.nextFreeId = 1; pointers = new int[N+1][alphabetSize]; end = new boolean[N+1]; } public void addWord(String word) { int curr = 0; for (int j=0; j<word.length(); j++) { int c = word.charAt(j) - 'a'; int next = pointers[curr][c]; if (next == 0) { next = nextFreeId++; pointers[curr][c] = next; } curr = next; } end[curr] = true; } public boolean hasWord(String word) { int curr = 0; for (int j=0; j<word.length(); j++) { int c = word.charAt(j) - 'a'; int next = pointers[curr][c]; if (next == 0) return false; curr = next; } return end[curr]; } } private static class Prob { /** For heavy calculations on probabilities, this class * provides more accuracy & efficiency than doubles. * Math explained: https://en.wikipedia.org/wiki/Log_probability * Quick start: * - Instantiate probabilities, eg. Prob a = new Prob(0.75) * - add(), multiply() return new objects, can perform on nulls & NaNs. * - get() returns probability as a readable double */ /** Logarithmized probability. Note: 0% represented by logP NaN. */ private double logP; /** Construct instance with real probability. */ public Prob(double real) { if (real > 0) this.logP = Math.log(real); else this.logP = Double.NaN; } /** Construct instance with already logarithmized value. */ static boolean dontLogAgain = true; public Prob(double logP, boolean anyBooleanHereToChooseThisConstructor) { this.logP = logP; } /** Returns real probability as a double. */ public double get() { return Math.exp(logP); } @Override public String toString() { return ""+get(); } /***************** STATIC METHODS BELOW ********************/ /** Note: returns NaN only when a && b are both NaN/null. */ public static Prob add(Prob a, Prob b) { if (nullOrNaN(a) && nullOrNaN(b)) return new Prob(Double.NaN, dontLogAgain); if (nullOrNaN(a)) return copy(b); if (nullOrNaN(b)) return copy(a); double x = Math.max(a.logP, b.logP); double y = Math.min(a.logP, b.logP); double sum = x + Math.log(1 + Math.exp(y - x)); return new Prob(sum, dontLogAgain); } /** Note: multiplying by null or NaN produces NaN (repping 0% real prob). */ public static Prob multiply(Prob a, Prob b) { if (nullOrNaN(a) || nullOrNaN(b)) return new Prob(Double.NaN, dontLogAgain); return new Prob(a.logP + b.logP, dontLogAgain); } /** Returns true if p is null or NaN. */ private static boolean nullOrNaN(Prob p) { return (p == null || Double.isNaN(p.logP)); } /** Returns a new instance with the same value as original. */ private static Prob copy(Prob original) { return new Prob(original.logP, dontLogAgain); } } class Binary implements Comparable<Binary> { /** * Use example: Binary b = new Binary(Long.toBinaryString(53249834L)); * * When manipulating small binary strings, instantiate new Binary(string) * When just reading large binary strings, instantiate new Binary(string,true) * get(int i) returns a character '1' or '0', not an int. */ private boolean[] d; private int first; // Starting from left, the first (most remarkable) '1' public int length; public Binary(String binaryString) { this(binaryString, false); } public Binary(String binaryString, boolean initWithMinArraySize) { length = binaryString.length(); int size = Math.max(2*length, 1); first = length/4; if (initWithMinArraySize) { first = 0; size = Math.max(length, 1); } d = new boolean[size]; for (int i=0; i<length; i++) { if (binaryString.charAt(i) == '1') d[i+first] = true; } } public void addFirst(char c) { if (first-1 < 0) doubleArraySize(); first--; d[first] = (c == '1' ? true : false); length++; } public void addLast(char c) { if (first+length >= d.length) doubleArraySize(); d[first+length] = (c == '1' ? true : false); length++; } private void doubleArraySize() { boolean[] bigArray = new boolean[(d.length+1) * 2]; int newFirst = bigArray.length / 4; for (int i=0; i<length; i++) { bigArray[i + newFirst] = d[i + first]; } first = newFirst; d = bigArray; } public boolean flip(int i) { boolean value = (this.d[first+i] ? false : true); this.d[first+i] = value; return value; } public void set(int i, char c) { boolean value = (c == '1' ? true : false); this.d[first+i] = value; } public char get(int i) { return (this.d[first+i] ? '1' : '0'); } @Override public int compareTo(Binary o) { if (this.length != o.length) return this.length - o.length; int len = this.length; for (int i=0; i<len; i++) { int diff = this.get(i) - o.get(i); if (diff != 0) return diff; } return 0; } @Override public String toString() { StringBuilder sb = new StringBuilder(); for (int i=0; i<length; i++) { sb.append(d[i+first] ? '1' : '0'); } return sb.toString(); } } /************************** Range queries **************************/ class FenwickMin { long n; long[] original; long[] bottomUp; long[] topDown; public FenwickMin(int n) { this.n = n; original = new long[n+2]; bottomUp = new long[n+2]; topDown = new long[n+2]; } public void set(int modifiedNode, long value) { long replaced = original[modifiedNode]; original[modifiedNode] = value; // Update left tree int i = modifiedNode; long v = value; while (i <= n) { if (v > bottomUp[i]) { if (replaced == bottomUp[i]) { v = Math.min(v, original[i]); for (int r=1 ;; r++) { int x = (i&-i)>>>r; if (x == 0) break; int child = i-x; v = Math.min(v, bottomUp[child]); } } else break; } if (v == bottomUp[i]) break; bottomUp[i] = v; i += (i&-i); } // Update right tree i = modifiedNode; v = value; while (i > 0) { if (v > topDown[i]) { if (replaced == topDown[i]) { v = Math.min(v, original[i]); for (int r=1 ;; r++) { int x = (i&-i)>>>r; if (x == 0) break; int child = i+x; if (child > n+1) break; v = Math.min(v, topDown[child]); } } else break; } if (v == topDown[i]) break; topDown[i] = v; i -= (i&-i); } } public long getMin(int a, int b) { long min = original[a]; int prev = a; int curr = prev + (prev&-prev); // parent right hand side while (curr <= b) { min = Math.min(min, topDown[prev]); // value from the other tree prev = curr; curr = prev + (prev&-prev);; } min = Math.min(min, original[prev]); prev = b; curr = prev - (prev&-prev); // parent left hand side while (curr >= a) { min = Math.min(min,bottomUp[prev]); // value from the other tree prev = curr; curr = prev - (prev&-prev); } return min; } } class FenwickSum { public long[] d; public FenwickSum(int n) { d=new long[n+1]; } /** a[0] must be unused. */ public FenwickSum(long[] a) { d=new long[a.length]; for (int i=1; i<a.length; i++) { modify(i, a[i]); } } /** Do not modify i=0. */ void modify(int i, long v) { while (i<d.length) { d[i] += v; // Move to next uplink on the RIGHT side of i i += (i&-i); } } /** Returns sum from a to b, *BOTH* inclusive. */ long getSum(int a, int b) { return getSum(b) - getSum(a-1); } private long getSum(int i) { long sum = 0; while (i>0) { sum += d[i]; // Move to next uplink on the LEFT side of i i -= (i&-i); } return sum; } } class SegmentTree { /* Provides log(n) operations for: * - Range query (sum, min or max) * - Range update ("+8 to all values between indexes 4 and 94") */ int N; long[] lazy; long[] sum; long[] min; long[] max; boolean supportSum; boolean supportMin; boolean supportMax; public SegmentTree(int n) { this(n, true, true, true); } public SegmentTree(int n, boolean supportSum, boolean supportMin, boolean supportMax) { for (N=2; N<n;) N*=2; this.lazy = new long[2*N]; this.supportSum = supportSum; this.supportMin = supportMin; this.supportMax = supportMax; if (this.supportSum) this.sum = new long[2*N]; if (this.supportMin) this.min = new long[2*N]; if (this.supportMax) this.max = new long[2*N]; } void modifyRange(long x, int a, int b) { modifyRec(a, b, 1, 0, N-1, x); } void setRange() { //TODO } long getSum(int a, int b) { return querySum(a, b, 1, 0, N-1); } long getMin(int a, int b) { return queryMin(a, b, 1, 0, N-1); } long getMax(int a, int b) { return queryMax(a, b, 1, 0, N-1); } private long querySum(int wantedLeft, int wantedRight, int i, int actualLeft, int actualRight) { if (wantedLeft > actualRight || wantedRight < actualLeft) { return 0; } if (wantedLeft == actualLeft && wantedRight == actualRight) { int count = wantedRight - wantedLeft + 1; return sum[i] + count * lazy[i]; } if (lazy[i] != 0) propagate(i, actualLeft, actualRight); int d = (actualRight - actualLeft + 1) / 2; long left = querySum(wantedLeft, min(actualLeft+d-1, wantedRight), 2*i, actualLeft, actualLeft+d-1); long right = querySum(max(actualLeft+d, wantedLeft), wantedRight, 2*i+1, actualLeft+d, actualRight); return left + right; } private long queryMin(int wantedLeft, int wantedRight, int i, int actualLeft, int actualRight) { if (wantedLeft > actualRight || wantedRight < actualLeft) { return 0; } if (wantedLeft == actualLeft && wantedRight == actualRight) { return min[i] + lazy[i]; } if (lazy[i] != 0) propagate(i, actualLeft, actualRight); int d = (actualRight - actualLeft + 1) / 2; long left = queryMin(wantedLeft, min(actualLeft+d-1, wantedRight), 2*i, actualLeft, actualLeft+d-1); long right = queryMin(max(actualLeft+d, wantedLeft), wantedRight, 2*i+1, actualLeft+d, actualRight); return min(left, right); } private long queryMax(int wantedLeft, int wantedRight, int i, int actualLeft, int actualRight) { if (wantedLeft > actualRight || wantedRight < actualLeft) { return 0; } if (wantedLeft == actualLeft && wantedRight == actualRight) { return max[i] + lazy[i]; } if (lazy[i] != 0) propagate(i, actualLeft, actualRight); int d = (actualRight - actualLeft + 1) / 2; long left = queryMax(wantedLeft, min(actualLeft+d-1, wantedRight), 2*i, actualLeft, actualLeft+d-1); long right = queryMax(max(actualLeft+d, wantedLeft), wantedRight, 2*i+1, actualLeft+d, actualRight); return max(left, right); } private void modifyRec(int wantedLeft, int wantedRight, int i, int actualLeft, int actualRight, long value) { if (wantedLeft > actualRight || wantedRight < actualLeft) { return; } if (wantedLeft == actualLeft && wantedRight == actualRight) { lazy[i] += value; return; } if (lazy[i] != 0) propagate(i, actualLeft, actualRight); int d = (actualRight - actualLeft + 1) / 2; modifyRec(wantedLeft, min(actualLeft+d-1, wantedRight), 2*i, actualLeft, actualLeft+d-1, value); modifyRec(max(actualLeft+d, wantedLeft), wantedRight, 2*i+1, actualLeft+d, actualRight, value); if (supportSum) sum[i] += value * (min(actualRight, wantedRight) - max(actualLeft, wantedLeft) + 1); if (supportMin) min[i] = min(min[2*i] + lazy[2*i], min[2*i+1] + lazy[2*i+1]); if (supportMax) max[i] = max(max[2*i] + lazy[2*i], max[2*i+1] + lazy[2*i+1]); } private void propagate(int i, int actualLeft, int actualRight) { lazy[2*i] += lazy[i]; lazy[2*i+1] += lazy[i]; if (supportSum) sum[i] += lazy[i] * (actualRight - actualLeft + 1); if (supportMin) min[i] += lazy[i]; if (supportMax) max[i] += lazy[i]; lazy[i] = 0; } } /***************************** Graphs *****************************/ List<Integer>[] toGraph(IO io, int n) { List<Integer>[] g = new ArrayList[n+1]; for (int i=1; i<=n; i++) g[i] = new ArrayList<>(); for (int i=1; i<=n-1; i++) { int a = io.nextInt(); int b = io.nextInt(); g[a].add(b); g[b].add(a); } return g; } class Graph { HashMap<Long, List<Long>> edges; public Graph() { edges = new HashMap<>(); } List<Long> getSetNeighbors(Long node) { List<Long> neighbors = edges.get(node); if (neighbors == null) { neighbors = new ArrayList<>(); edges.put(node, neighbors); } return neighbors; } void addBiEdge(Long a, Long b) { addEdge(a, b); addEdge(b, a); } void addEdge(Long from, Long to) { getSetNeighbors(to); // make sure all have initialized lists List<Long> neighbors = getSetNeighbors(from); neighbors.add(to); } // topoSort variables int UNTOUCHED = 0; int FINISHED = 2; int INPROGRESS = 1; HashMap<Long, Integer> vis; List<Long> topoAns; List<Long> failDueToCycle = new ArrayList<Long>() {{ add(-1L); }}; List<Long> topoSort() { topoAns = new ArrayList<>(); vis = new HashMap<>(); for (Long a : edges.keySet()) { if (!topoDFS(a)) return failDueToCycle; } Collections.reverse(topoAns); return topoAns; } boolean topoDFS(long curr) { Integer status = vis.get(curr); if (status == null) status = UNTOUCHED; if (status == FINISHED) return true; if (status == INPROGRESS) { return false; } vis.put(curr, INPROGRESS); for (long next : edges.get(curr)) { if (!topoDFS(next)) return false; } vis.put(curr, FINISHED); topoAns.add(curr); return true; } } public class StronglyConnectedComponents { /** Kosaraju's algorithm */ ArrayList<Integer>[] forw; ArrayList<Integer>[] bacw; /** Use: getCount(2, new int[] {1,2}, new int[] {2,1}) */ public int getCount(int n, int[] mista, int[] minne) { forw = new ArrayList[n+1]; bacw = new ArrayList[n+1]; for (int i=1; i<=n; i++) { forw[i] = new ArrayList<Integer>(); bacw[i] = new ArrayList<Integer>(); } for (int i=0; i<mista.length; i++) { int a = mista[i]; int b = minne[i]; forw[a].add(b); bacw[b].add(a); } int count = 0; List<Integer> list = new ArrayList<Integer>(); boolean[] visited = new boolean[n+1]; for (int i=1; i<=n; i++) { dfsForward(i, visited, list); } visited = new boolean[n+1]; for (int i=n-1; i>=0; i--) { int node = list.get(i); if (visited[node]) continue; count++; dfsBackward(node, visited); } return count; } public void dfsForward(int i, boolean[] visited, List<Integer> list) { if (visited[i]) return; visited[i] = true; for (int neighbor : forw[i]) { dfsForward(neighbor, visited, list); } list.add(i); } public void dfsBackward(int i, boolean[] visited) { if (visited[i]) return; visited[i] = true; for (int neighbor : bacw[i]) { dfsBackward(neighbor, visited); } } } class LCAFinder { /* O(n log n) Initialize: new LCAFinder(graph) * O(log n) Queries: find(a,b) returns lowest common ancestor for nodes a and b */ int[] nodes; int[] depths; int[] entries; int pointer; FenwickMin fenwick; public LCAFinder(List<Integer>[] graph) { this.nodes = new int[(int)10e6]; this.depths = new int[(int)10e6]; this.entries = new int[graph.length]; this.pointer = 1; boolean[] visited = new boolean[graph.length+1]; dfs(1, 0, graph, visited); fenwick = new FenwickMin(pointer-1); for (int i=1; i<pointer; i++) { fenwick.set(i, depths[i] * 1000000L + i); } } private void dfs(int node, int depth, List<Integer>[] graph, boolean[] visited) { visited[node] = true; entries[node] = pointer; nodes[pointer] = node; depths[pointer] = depth; pointer++; for (int neighbor : graph[node]) { if (visited[neighbor]) continue; dfs(neighbor, depth+1, graph, visited); nodes[pointer] = node; depths[pointer] = depth; pointer++; } } public int find(int a, int b) { int left = entries[a]; int right = entries[b]; if (left > right) { int temp = left; left = right; right = temp; } long mixedBag = fenwick.getMin(left, right); int index = (int) (mixedBag % 1000000L); return nodes[index]; } } /**************************** Geometry ****************************/ class Point { int y; int x; public Point(int y, int x) { this.y = y; this.x = x; } } boolean segmentsIntersect(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { // Returns true if segment 1-2 intersects segment 3-4 if (x1 == x2 && x3 == x4) { // Both segments are vertical if (x1 != x3) return false; if (min(y1,y2) < min(y3,y4)) { return max(y1,y2) >= min(y3,y4); } else { return max(y3,y4) >= min(y1,y2); } } if (x1 == x2) { // Only segment 1-2 is vertical. Does segment 3-4 cross it? y = a*x + b double a34 = (y4-y3)/(x4-x3); double b34 = y3 - a34*x3; double y = a34 * x1 + b34; return y >= min(y1,y2) && y <= max(y1,y2) && x1 >= min(x3,x4) && x1 <= max(x3,x4); } if (x3 == x4) { // Only segment 3-4 is vertical. Does segment 1-2 cross it? y = a*x + b double a12 = (y2-y1)/(x2-x1); double b12 = y1 - a12*x1; double y = a12 * x3 + b12; return y >= min(y3,y4) && y <= max(y3,y4) && x3 >= min(x1,x2) && x3 <= max(x1,x2); } double a12 = (y2-y1)/(x2-x1); double b12 = y1 - a12*x1; double a34 = (y4-y3)/(x4-x3); double b34 = y3 - a34*x3; if (closeToZero(a12 - a34)) { // Parallel lines return closeToZero(b12 - b34); } // Non parallel non vertical lines intersect at x. Is x part of both segments? double x = -(b12-b34)/(a12-a34); return x >= min(x1,x2) && x <= max(x1,x2) && x >= min(x3,x4) && x <= max(x3,x4); } boolean pointInsideRectangle(Point p, List<Point> r) { Point a = r.get(0); Point b = r.get(1); Point c = r.get(2); Point d = r.get(3); double apd = areaOfTriangle(a, p, d); double dpc = areaOfTriangle(d, p, c); double cpb = areaOfTriangle(c, p, b); double pba = areaOfTriangle(p, b, a); double sumOfAreas = apd + dpc + cpb + pba; if (closeToZero(sumOfAreas - areaOfRectangle(r))) { if (closeToZero(apd) || closeToZero(dpc) || closeToZero(cpb) || closeToZero(pba)) { return false; } return true; } return false; } double areaOfTriangle(Point a, Point b, Point c) { return 0.5 * Math.abs((a.x-c.x)*(b.y-a.y)-(a.x-b.x)*(c.y-a.y)); } double areaOfRectangle(List<Point> r) { double side1xDiff = r.get(0).x - r.get(1).x; double side1yDiff = r.get(0).y - r.get(1).y; double side2xDiff = r.get(1).x - r.get(2).x; double side2yDiff = r.get(1).y - r.get(2).y; double side1 = Math.sqrt(side1xDiff * side1xDiff + side1yDiff * side1yDiff); double side2 = Math.sqrt(side2xDiff * side2xDiff + side2yDiff * side2yDiff); return side1 * side2; } boolean pointsOnSameLine(double x1, double y1, double x2, double y2, double x3, double y3) { double areaTimes2 = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2); return (closeToZero(areaTimes2)); } class PointToLineSegmentDistanceCalculator { // Just call this double minDistFromPointToLineSegment(double point_x, double point_y, double x1, double y1, double x2, double y2) { return Math.sqrt(distToSegmentSquared(point_x, point_y, x1, y1, x2, y2)); } private double distToSegmentSquared(double point_x, double point_y, double x1, double y1, double x2, double y2) { double l2 = dist2(x1,y1,x2,y2); if (l2 == 0) return dist2(point_x, point_y, x1, y1); double t = ((point_x - x1) * (x2 - x1) + (point_y - y1) * (y2 - y1)) / l2; if (t < 0) return dist2(point_x, point_y, x1, y1); if (t > 1) return dist2(point_x, point_y, x2, y2); double com_x = x1 + t * (x2 - x1); double com_y = y1 + t * (y2 - y1); return dist2(point_x, point_y, com_x, com_y); } private double dist2(double x1, double y1, double x2, double y2) { return Math.pow((x1 - x2), 2) + Math.pow((y1 - y2), 2); } } /****************************** Math ******************************/ long pow(long base, int exp) { if (exp == 0) return 1L; long x = pow(base, exp/2); long ans = x * x; if (exp % 2 != 0) ans *= base; return ans; } long gcd(long... v) { /** Chained calls to Euclidean algorithm. */ if (v.length == 1) return v[0]; long ans = gcd(v[1], v[0]); for (int i=2; i<v.length; i++) { ans = gcd(ans, v[i]); } return ans; } long gcd(long a, long b) { /** Euclidean algorithm. */ if (b == 0) return a; return gcd(b, a%b); } int[] generatePrimesUpTo(int last) { /* Sieve of Eratosthenes. Practically O(n). Values of 0 indicate primes. */ int[] div = new int[last+1]; for (int x=2; x<=last; x++) { if (div[x] > 0) continue; for (int u=2*x; u<=last; u+=x) { div[u] = x; } } return div; } long lcm(long a, long b) { /** Least common multiple */ return a * b / gcd(a,b); } class BaseConverter { /* Palauttaa luvun esityksen kannassa base */ public String convert(Long number, int base) { return Long.toString(number, base); } /* Palauttaa luvun esityksen kannassa baseTo, kun annetaan luku StringinΓ€ kannassa baseFrom */ public String convert(String number, int baseFrom, int baseTo) { return Long.toString(Long.parseLong(number, baseFrom), baseTo); } /* Tulkitsee kannassa base esitetyn luvun longiksi (kannassa 10) */ public long longify(String number, int baseFrom) { return Long.parseLong(number, baseFrom); } } class BinomialCoefficients { /** Total number of K sized unique combinations from pool of size N (unordered) N! / ( K! (N - K)! ) */ /** For simple queries where output fits in long. */ public long biCo(long n, long k) { long r = 1; if (k > n) return 0; for (long d = 1; d <= k; d++) { r *= n--; r /= d; } return r; } /** For multiple queries with same n, different k. */ public long[] precalcBinomialCoefficientsK(int n, int maxK) { long v[] = new long[maxK+1]; v[0] = 1; // nC0 == 1 for (int i=1; i<=n; i++) { for (int j=Math.min(i,maxK); j>0; j--) { v[j] = v[j] + v[j-1]; // Pascal's triangle } } return v; } /** When output needs % MOD. */ public long[] precalcBinomialCoefficientsK(int n, int k, long M) { long v[] = new long[k+1]; v[0] = 1; // nC0 == 1 for (int i=1; i<=n; i++) { for (int j=Math.min(i,k); j>0; j--) { v[j] = v[j] + v[j-1]; // Pascal's triangle v[j] %= M; } } return v; } } /**************************** Strings ****************************/ class Zalgo { public int pisinEsiintyma(String haku, String kohde) { char[] s = new char[haku.length() + 1 + kohde.length()]; for (int i=0; i<haku.length(); i++) { s[i] = haku.charAt(i); } int j = haku.length(); s[j++] = '#'; for (int i=0; i<kohde.length(); i++) { s[j++] = kohde.charAt(i); } int[] z = toZarray(s); int max = 0; for (int i=haku.length(); i<z.length; i++) { max = Math.max(max, z[i]); } return max; } public int[] toZarray(char[] s) { int n = s.length; int[] z = new int[n]; int a = 0, b = 0; for (int i = 1; i < n; i++) { if (i > b) { for (int j = i; j < n && s[j - i] == s[j]; j++) z[i]++; } else { z[i] = z[i - a]; if (i + z[i - a] > b) { for (int j = b + 1; j < n && s[j - i] == s[j]; j++) z[i]++; a = i; b = i + z[i] - 1; } } } return z; } public List<Integer> getStartIndexesWhereWordIsFound(String haku, String kohde) { // this is alternative use case char[] s = new char[haku.length() + 1 + kohde.length()]; for (int i=0; i<haku.length(); i++) { s[i] = haku.charAt(i); } int j = haku.length(); s[j++] = '#'; for (int i=0; i<kohde.length(); i++) { s[j++] = kohde.charAt(i); } int[] z = toZarray(s); List<Integer> indexes = new ArrayList<>(); for (int i=haku.length(); i<z.length; i++) { if (z[i] < haku.length()) continue; indexes.add(i); } return indexes; } } class StringHasher { class HashedString { long[] hashes; long[] modifiers; public HashedString(long[] hashes, long[] modifiers) { this.hashes = hashes; this.modifiers = modifiers; } } long P; long M; public StringHasher() { initializePandM(); } HashedString hashString(String s) { int n = s.length(); long[] hashes = new long[n]; long[] modifiers = new long[n]; hashes[0] = s.charAt(0); modifiers[0] = 1; for (int i=1; i<n; i++) { hashes[i] = (hashes[i-1] * P + s.charAt(i)) % M; modifiers[i] = (modifiers[i-1] * P) % M; } return new HashedString(hashes, modifiers); } /** * Indices are inclusive. */ long getHash(HashedString hashedString, int startIndex, int endIndex) { long[] hashes = hashedString.hashes; long[] modifiers = hashedString.modifiers; long result = hashes[endIndex]; if (startIndex > 0) result -= (hashes[startIndex-1] * modifiers[endIndex-startIndex+1]) % M; if (result < 0) result += M; return result; } // Less interesting methods below /** * Efficient for 2 input parameter strings in particular. */ HashedString[] hashString(String first, String second) { HashedString[] array = new HashedString[2]; int n = first.length(); long[] modifiers = new long[n]; modifiers[0] = 1; long[] firstHashes = new long[n]; firstHashes[0] = first.charAt(0); array[0] = new HashedString(firstHashes, modifiers); long[] secondHashes = new long[n]; secondHashes[0] = second.charAt(0); array[1] = new HashedString(secondHashes, modifiers); for (int i=1; i<n; i++) { modifiers[i] = (modifiers[i-1] * P) % M; firstHashes[i] = (firstHashes[i-1] * P + first.charAt(i)) % M; secondHashes[i] = (secondHashes[i-1] * P + second.charAt(i)) % M; } return array; } /** * Efficient for 3+ strings * More efficient than multiple hashString calls IF strings are same length. */ HashedString[] hashString(String... strings) { HashedString[] array = new HashedString[strings.length]; int n = strings[0].length(); long[] modifiers = new long[n]; modifiers[0] = 1; for (int j=0; j<strings.length; j++) { // if all strings are not same length, defer work to another method if (strings[j].length() != n) { for (int i=0; i<n; i++) { array[i] = hashString(strings[i]); } return array; } // otherwise initialize stuff long[] hashes = new long[n]; hashes[0] = strings[j].charAt(0); array[j] = new HashedString(hashes, modifiers); } for (int i=1; i<n; i++) { modifiers[i] = (modifiers[i-1] * P) % M; for (int j=0; j<strings.length; j++) { String s = strings[j]; long[] hashes = array[j].hashes; hashes[i] = (hashes[i-1] * P + s.charAt(i)) % M; } } return array; } void initializePandM() { ArrayList<Long> modOptions = new ArrayList<>(20); modOptions.add(353873237L); modOptions.add(353875897L); modOptions.add(353878703L); modOptions.add(353882671L); modOptions.add(353885303L); modOptions.add(353888377L); modOptions.add(353893457L); P = modOptions.get(new Random().nextInt(modOptions.size())); modOptions.clear(); modOptions.add(452940277L); modOptions.add(452947687L); modOptions.add(464478431L); modOptions.add(468098221L); modOptions.add(470374601L); modOptions.add(472879717L); modOptions.add(472881973L); M = modOptions.get(new Random().nextInt(modOptions.size())); } } /*************************** Technical ***************************/ private class IO extends PrintWriter { private InputStreamReader r; private static final int BUFSIZE = 1 << 15; private char[] buf; private int bufc; private int bufi; private StringBuilder sb; public IO() { super(new BufferedOutputStream(System.out)); r = new InputStreamReader(System.in); buf = new char[BUFSIZE]; bufc = 0; bufi = 0; sb = new StringBuilder(); } /** Print, flush, return nextInt. */ private int queryInt(String s) { io.println(s); io.flush(); return nextInt(); } /** Print, flush, return nextLong. */ private long queryLong(String s) { io.println(s); io.flush(); return nextLong(); } /** Print, flush, return next word. */ private String queryNext(String s) { io.println(s); io.flush(); return next(); } private void fillBuf() throws IOException { bufi = 0; bufc = 0; while(bufc == 0) { bufc = r.read(buf, 0, BUFSIZE); if(bufc == -1) { bufc = 0; return; } } } private boolean pumpBuf() throws IOException { if(bufi == bufc) { fillBuf(); } return bufc != 0; } private boolean isDelimiter(char c) { return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f'; } private void eatDelimiters() throws IOException { while(true) { if(bufi == bufc) { fillBuf(); if(bufc == 0) throw new RuntimeException("IO: Out of input."); } if(!isDelimiter(buf[bufi])) break; ++bufi; } } public String next() { try { sb.setLength(0); eatDelimiters(); int start = bufi; while(true) { if(bufi == bufc) { sb.append(buf, start, bufi - start); fillBuf(); start = 0; if(bufc == 0) break; } if(isDelimiter(buf[bufi])) break; ++bufi; } sb.append(buf, start, bufi - start); return sb.toString(); } catch(IOException e) { throw new RuntimeException("IO.next: Caught IOException."); } } public int nextInt() { try { int ret = 0; eatDelimiters(); boolean positive = true; if(buf[bufi] == '-') { ++bufi; if(!pumpBuf()) throw new RuntimeException("IO.nextInt: Invalid int."); positive = false; } boolean first = true; while(true) { if(!pumpBuf()) break; if(isDelimiter(buf[bufi])) { if(first) throw new RuntimeException("IO.nextInt: Invalid int."); break; } first = false; if(buf[bufi] >= '0' && buf[bufi] <= '9') { if(ret < -214748364) throw new RuntimeException("IO.nextInt: Invalid int."); ret *= 10; ret -= (int)(buf[bufi] - '0'); if(ret > 0) throw new RuntimeException("IO.nextInt: Invalid int."); } else { throw new RuntimeException("IO.nextInt: Invalid int."); } ++bufi; } if(positive) { if(ret == -2147483648) throw new RuntimeException("IO.nextInt: Invalid int."); ret = -ret; } return ret; } catch(IOException e) { throw new RuntimeException("IO.nextInt: Caught IOException."); } } public long nextLong() { try { long ret = 0; eatDelimiters(); boolean positive = true; if(buf[bufi] == '-') { ++bufi; if(!pumpBuf()) throw new RuntimeException("IO.nextLong: Invalid long."); positive = false; } boolean first = true; while(true) { if(!pumpBuf()) break; if(isDelimiter(buf[bufi])) { if(first) throw new RuntimeException("IO.nextLong: Invalid long."); break; } first = false; if(buf[bufi] >= '0' && buf[bufi] <= '9') { if(ret < -922337203685477580L) throw new RuntimeException("IO.nextLong: Invalid long."); ret *= 10; ret -= (long)(buf[bufi] - '0'); if(ret > 0) throw new RuntimeException("IO.nextLong: Invalid long."); } else { throw new RuntimeException("IO.nextLong: Invalid long."); } ++bufi; } if(positive) { if(ret == -9223372036854775808L) throw new RuntimeException("IO.nextLong: Invalid long."); ret = -ret; } return ret; } catch(IOException e) { throw new RuntimeException("IO.nextLong: Caught IOException."); } } public double nextDouble() { return Double.parseDouble(next()); } } void print(Object output) { io.println(output); } void done(Object output) { print(output); done(); } void done() { io.close(); throw new RuntimeException("Clean exit"); } long min(long... v) { long ans = v[0]; for (int i=1; i<v.length; i++) { ans = Math.min(ans, v[i]); } return ans; } double min(double... v) { double ans = v[0]; for (int i=1; i<v.length; i++) { ans = Math.min(ans, v[i]); } return ans; } int min(int... v) { int ans = v[0]; for (int i=1; i<v.length; i++) { ans = Math.min(ans, v[i]); } return ans; } long max(long... v) { long ans = v[0]; for (int i=1; i<v.length; i++) { ans = Math.max(ans, v[i]); } return ans; } double max(double... v) { double ans = v[0]; for (int i=1; i<v.length; i++) { ans = Math.max(ans, v[i]); } return ans; } int max(int... v) { int ans = v[0]; for (int i=1; i<v.length; i++) { ans = Math.max(ans, v[i]); } return ans; } } } ```
### Prompt Create a solution in PYTHON for the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python R=lambda:map(int,raw_input().split()) n,m,q=R() s=raw_input() t=raw_input() a=[0]*2000 for i in range(m,n+1): a[i]=a[i-1]+(s[i-m:i]==t) for _ in range(q): l,r=R() print max(0,a[r]-a[min(r,l+m-2)]) ```
### Prompt In cpp, your task is to solve the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; int main() { int n, m, q; scanf("%d %d %d", &n, &m, &q); string a, b; cin >> a >> b; int ps[1007] = {0}; for (int i = 1; i <= n; i++) { if (i < m) continue; bool flag = false; for (int j = i - m; j < i; j++) if (a[j] != b[j - i + m]) flag = true; if (!flag) ps[i] = ps[i - 1] + 1; else ps[i] = ps[i - 1]; } while (q--) { int x, y; scanf("%d %d", &x, &y); x += (m - 1); if (x > y) puts("0"); else printf("%d\n", ps[y] - ps[x - 1]); } return 0; } ```
### Prompt Please formulate a cpp solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, m, q, arr[1001], l, r; string s, t; int main() { cin >> n >> m >> q; cin >> s >> t; for (int i = 0; i <= n - m; i++) { arr[i + 1] = arr[i]; if (s.substr(i, m) == t) { arr[i + 1]++; } } for (int j = 1; j <= q; j++) { cin >> l >> r; if (r - l + 1 < m) cout << 0 << endl; else { cout << arr[r - m + 1] - arr[l - 1] << endl; } } } ```
### Prompt Please provide a java coded solution to the problem described below: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```java import java.util.*; import java.io.*; import java.text.*; public class Main{ long mod = (int)1e9+7, IINF = (long)1e17; final int MAX = (int)1e6+1, INF = (int)1e9, root = 3; DecimalFormat df = new DecimalFormat("0.00000000"); double PI = 3.141592653589793238462643383279502884197169399375105820974944; static boolean multipleTC = false, memory = false; FastReader in;PrintWriter out; public static void main(String[] args) throws Exception{ if(memory)new Thread(null, new Runnable() {public void run(){try{new Main().run();}catch(Exception e){e.printStackTrace();}}}, "1", 1 << 28).start(); else new Main().run(); } void run() throws Exception{ in = new FastReader(); out = new PrintWriter(System.out); for(int i = 1, T= (multipleTC)?ni():1; i<= T; i++)solve(i); out.flush(); out.close(); } void solve(int TC) throws Exception{ int n = ni(), m = ni(), q = ni(); if(n<m){ while(q-->0)pn(0); return; } String s = n(), t = n(); boolean[] val = new boolean[n]; for(int i = 0; i<= n-m; i++){ boolean match = true; for(int j = 0; j< m && match; j++){ if(s.charAt(i+j)!=t.charAt(j))match = false; } if(match)val[i] = true; } int[][] ans = new int[n][n]; for(int i = 0; i< n; i++){ ans[i][i] = 0; if(m==1 && val[i])ans[i][i]++; for(int j = i+1; j< n; j++){ if(j-m+1>=i && val[j-m+1])ans[i][j] = ans[i][j-1]+1; else ans[i][j] = ans[i][j-1]; } } while(q-->0)pn(ans[ni()-1][ni()-1]); } long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);} int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);} int bitcount(long n){return (n==0)?0:(1+bitcount(n&(n-1)));} void p(Object o){out.print(o);} void pn(Object o){out.println(o);} void pni(Object o){out.println(o);out.flush();} String n(){return in.next();} String nln(){return in.nextLine();} int ni(){return Integer.parseInt(in.next());} long nl(){return Long.parseLong(in.next());} double nd(){return Double.parseDouble(in.next());} class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws Exception{ br = new BufferedReader(new FileReader(s)); } String next(){ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); }catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } String nextLine(){ String str = ""; try{ str = br.readLine(); }catch (IOException e){ e.printStackTrace(); } return str; } } } ```
### Prompt Your task is to create a PYTHON3 solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python3 f = lambda: list(map(int, input().split())); ana1 = "" n, m, q = f() s = input() t = input() for _ in range(n - m + 1): if s[_:_ + m] == t: ana1 += "1" else: ana1 += "0" for _ in range(q): l, r = f() if r - l + 1 >= m: print(ana1[l - 1:r - m + 1].count("1")) else: print(0) ```
### Prompt Construct a PYTHON3 code solution to the problem outlined: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python3 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 i in range(q): u,v=map(int,input().split()) l=u-1 r=v-m c=sum(a[l:r+1]) if l<=r else 0 print(c) ```
### Prompt Please formulate a PYTHON3 solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python3 '''import sys,math input = sys.stdin.readline ############ ---- USER DEFINED INPUT FUNCTIONS ---- ############ def inp(): return(int(input())) def inlt(): return(list(map(int,input().split()))) def insr(): s = input() return(s[:len(s) - 1]) def invr(): return(map(int,input().split())) ################################################################ ############ ---- THE ACTUAL CODE STARTS BELOW ---- ############''' #sys.stdout=open("output.txt", 'w') #sys.stdout.write("Yes" + '\n') #from sys import stdin #input=stdin.readline #a = sorted([(n, i) for i, n in enumerate(map(int, input().split()))]) # from collections import Counter # import sys #s="abcdefghijklmnopqrstuvwxyz" #arr=sorted([(int(x),i) for i,x in enumerate(input().split())]) #n=int(input()) #n,k=map(int,input().split()) #arr=list(map(int,input().split())) #arr=list(map(int,input().split 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 i in range(q): u,v=map(int,input().split()) l=u-1 r=v-m cnt=sum(a[l:r+1]) if l<=r else 0 print(cnt) ```
### Prompt Create a solution in CPP for the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MX = 1e3 + 10, MD = 1e9 + 7; long long n, m, a[MX], ans, tmp, ps[MX], ps2[MX], q; string s, t, k; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> q; cin >> s >> t; int flag = 0; for (int i = 0; i < n; i++) { ps[i + 1] += ps[i]; ps2[i + 1] += ps2[i]; k = s.substr(i, m); if (k == t) { ps2[i + 1]++; ps[i + m]++; } } for (int i = 0; i < q; i++) { int l, r; cin >> l >> r; if (ps[r] - ps2[l - 1] < 0) { cout << 0 << "\n"; } else cout << ps[r] - ps2[l - 1] << "\n"; } } ```
### Prompt Please create a solution in CPP to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int kmp(string T, string &P) { if (P.empty()) return 0; if (T.size() < P.size()) return 0; vector<int> prefix(P.size(), 0); for (int i = 1, k = 0; i < P.size(); ++i) { while (k && P[k] != P[i]) k = prefix[k - 1]; if (P[k] == P[i]) ++k; prefix[i] = k; } int count = 0; for (int i = 0, k = 0; i < T.size(); ++i) { while (k && P[k] != T[i]) k = prefix[k - 1]; if (P[k] == T[i]) ++k; if (k == P.size()) count++; } return count; } int main() { int n, m, q; string s, t; cin >> n >> m >> q; cin >> s >> t; for (int i = 0; i < q; i++) { int l, r; cin >> l >> r; string sub = s.substr(l - 1, r - l + 1); int ret = kmp(sub, t); cout << ret << endl; } return 0; } ```
### Prompt Develop a solution in Cpp to the problem described below: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int N, M, Q; char S[1001], T[1001]; int A[1001], P[1001]; void f() { for (int i = 0; i + M <= N; i++) { int ok = 1; for (int j = 0; j < M && ok; j++) ok = (S[i + j] == T[j]); A[i] = ok; } } int main(int argc, char **argv) { ios::sync_with_stdio(false); cin >> N >> M >> Q; cin >> S >> T; f(); P[0] = A[0]; for (int i = 1; i < N; i++) P[i] = P[i - 1] + A[i]; while (Q--) { int l, r; cin >> l >> r; l--; r--; r -= M - 1; int ans = 0; if (l <= r) { ans = P[r]; if (l) ans -= P[l - 1]; } cout << ans << endl; } return 0; } ```
### Prompt Your task is to create a Python solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python def main(): n, m, q = map(int, raw_input().split()) s = raw_input() t = raw_input() counts = [0] _sum = 0 for i in xrange(len(s) - len(t) + 1): flag = True for j in xrange(len(t)): if t[j] != s[i+j]: flag = False break if flag: _sum += 1 counts.append(_sum) for i in xrange(q): l, r = map(lambda x: int(x) - 1, raw_input().split()) r -= len(t) - 2 if r <= 0 or l < 0 or r >= len(counts) or l >= len(counts): print 0 continue ans = max(0, counts[r] - counts[l]) print ans if __name__ == '__main__': main() ```
### Prompt Create a solution in CPP for the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; char a[1005]; char b[1005]; int t[1005]; int sum[1005]; int main() { memset(t, 0, sizeof(t)); int p, n, m; cin >> n >> m >> p; cin >> a >> b; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i + j] != b[j]) break; else if (j == m - 1) t[i] = 1; } } sum[0] = t[0]; for (int i = 1; i < n; i++) sum[i] = sum[i - 1] + t[i]; while (p--) { int l, r; scanf("%d%d", &l, &r); l--, r--; if (n < m || r - l + 1 < m) cout << 0 << endl; else cout << sum[r - m + 1] - sum[l - 1] << endl; } return 0; } ```
### Prompt Develop a solution in Cpp to the problem described below: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, m, q; const int maxn = 1005; char s[maxn], t[maxn]; int sum[maxn]; int main() { scanf("%d%d%d", &n, &m, &q); scanf("%s", s + 1); scanf("%s", t + 1); for (int i = 1; i <= n - m + 1; i++) { bool f = 1; for (int j = 1; j <= m; j++) if (s[i + j - 1] != t[j]) f = 0; if (f == 1) sum[i] = 1; } for (int i = 1; i <= n; i++) sum[i] += sum[i - 1]; for (int i = 1; i <= q; i++) { int l, r; scanf("%d%d", &l, &r); printf("%d\n", max(0, (r >= m ? sum[r - m + 1] : 0) - (l >= 1 ? sum[l - 1] : 0))); } return 0; } ```
### Prompt In cpp, your task is to solve the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> int n, m, q, nxt[1005], a[1005], b[1005], cnt; char s[1005], t[1005]; inline void getnxt() { nxt[0] = -1; for (register int i = 1, tmp; i < m; i++) { tmp = nxt[i - 1]; while (tmp >= 0 && t[tmp + 1] != t[i]) tmp = nxt[tmp]; if (t[tmp + 1] == t[i]) nxt[i] = tmp + 1; else nxt[i] = -1; } } int main() { scanf("%d%d%d", &n, &m, &q); if (n < m) { while (q--) puts("0"); return 0; } scanf("%s%s", s, t); getnxt(); register int i = 0, j = 0, l, r, ans = 0; while (j < n) { if (t[i] == s[j]) { i++; j++; if (i == m) a[++cnt] = j - m + 1, b[cnt] = j, i = nxt[i - 1] + 1; } else if (!i) j++; else i = nxt[i - 1] + 1; } while (q--) { scanf("%d%d", &l, &r); ans = 0; for (i = 1; i <= cnt; i++) if (l <= a[i] && r >= b[i]) ans++; printf("%d\n", ans); } return 0; } ```
### Prompt Create a solution in JAVA for the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```java import java.io.*; import java.util.*; public class Main { private static FastScanner in = new FastScanner(); private static FastOutput out = new FastOutput(); public void run() { int n = in.nextInt(), m = in.nextInt(), q = in.nextInt(); String s = in.next(), t = in.next(); char[] sc = s.toCharArray(), tc = t.toCharArray(); boolean[] ex = new boolean[n]; for (int i = 0; i < n - m + 1; i++) { ex[i] = true; for (int j = 0; j < m; j++) { if (sc[i + j] != tc[j]) { ex[i] = false; break; } } } for (int i = 0; i < q; i++) { int l = in.nextInt(), r = in.nextInt(); int count = 0; for (int j = l - 1; j < r - m + 1; j++) { if (ex[j]) count++; } out.println(count); } } public static void main(String[] args) { new Main().run(); out.flush(); out.close(); } } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner() { this(System.in); } FastScanner(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream)); } FastScanner(File file) { try { br = new BufferedReader(new FileReader(file)); } catch (FileNotFoundException e) { throw new RuntimeException(e); } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } class FastOutput { private final BufferedWriter bf; public FastOutput(OutputStream outputStream) { bf = new BufferedWriter(new OutputStreamWriter(outputStream)); } public FastOutput() { this(System.out); } private void printOne(Object s) { try { bf.write(s.toString()); } catch (IOException e) { throw new RuntimeException(e); } } public void println(Object s) { printOne(s); printOne("\n"); } public void print(Object ...os) { printd(" ", os); } public void printd(String delimiter, Object ...os) { for (Object o : os) { printOne(o); printOne(delimiter); } } public void flush() { try { bf.flush(); } catch (IOException e) { throw new RuntimeException(e); } } public void close() { try { bf.close(); } catch (IOException e) { throw new RuntimeException(e); } } } ```
### Prompt In cpp, your task is to solve the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e3 + 7; const int mod = 1e9 + 7; char str[N]; char s[N]; int a[1000 + 7]; int main() { int n, m, q; scanf("%d%d%d", &n, &m, &q); scanf("%s", str + 1); scanf("%s", s + 1); int ans = 0; for (int i = 1; i <= n; i++) { int cnt = 1; while (str[i + cnt - 1] == s[cnt] && str[i + cnt - 1] != '\0') { cnt++; } if (cnt - 1 == m) { a[ans] = i; ans++; } } while (q--) { int l, r; scanf("%d%d", &l, &r); int cc = 0; for (int i = 0; i < ans; i++) { if (a[i] >= l && a[i] + m - 1 <= r) cc++; } printf("%d\n", cc); } return 0; } ```
### Prompt In Python3, your task is to solve the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python3 n,m,q = list(map(int, input().split())) s = input() t = input() ts = [1 if s[i:i+len(t)] == t else 0 for i in range(len(s)-len(t)+1)] + [0]*(len(t)-1) sums = [ [0 for i in range(len(s))] for i in range(len(s)) ] for i in range(n): sums[i][i] = ts[i] for i in range(n): for j in range(i+1, n): sums[i][j] = sums[i][j-1] + ts[j] #print(ts) #print(sums) for i in range(q): l,r = list(map(int, input().split())) if r-m < 0: print(0) else: print(sums[l-1][r-m]) ```
### Prompt Please create a solution in cpp to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 2; const long long N1 = 1e2 + 2; const long long mod = 1e9 + 7; const int MASK = 1 << 17 + 1; const int who = 6 * N * log2(N); int a[N + 1]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, q; cin >> n >> m >> q; string s, ser; cin >> s >> ser; for (int i = 0; i < n - m + 1; i++) { if (s.substr(i, m) == ser) { a[i + 1] = 1; } } a[0] = 0; for (int i = 1; i <= n - m + 1; i++) { a[i] += a[i - 1]; } while (q--) { int l, r; cin >> l >> r; l--; r -= m - 1; if (r < l) { r = l; } cout << a[r] - a[l] << "\n"; } } ```
### Prompt Your task is to create a java solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```java import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class B { InputStream is; PrintWriter out; String INPUT = ""; int KMPSearch(String pat, String txt) { int M = pat.length(); int N = txt.length(); int lps[] = new int[M]; int j = 0; // index for pat[] computeLPSArray(pat,M,lps); int i = 0; int res = 0; int next_i = 0; while (i < N) { //out.println(pat.charAt(j)+" "+txt.charAt(i)); if (pat.charAt(j) == txt.charAt(i)) { j++; i++; } if (j == M) { j = lps[j-1]; res++; // We start i to check for more than once // appearance of pattern, we will reset i // to previous start+1 } // mismatch after j matches else if (i < N && pat.charAt(j) != txt.charAt(i)) { // Do not match lps[0..lps[j-1]] characters, // they will match anyway if (j != 0) j = lps[j-1]; else i = i+1; } } return res; } void computeLPSArray(String pat, int M, int lps[]) { // length of the previous longest prefix suffix int len = 0; int i = 1; lps[0] = 0; // lps[0] is always 0 // the loop calculates lps[i] for i = 1 to M-1 while (i < M) { if (pat.charAt(i) == pat.charAt(len)) { len++; lps[i] = len; i++; } else // (pat[i] != pat[len]) { // 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 // if (len == 0) { lps[i] = len; i++; } } } } void solve() { int n = ni(); int m = ni(); int q = ni(); String s = ns(); String t = ns(); while(q-- > 0) { int l = ni()-1; int r = ni()-1; String x = s.substring(l , r+1); out.println(KMPSearch(t, x)); } } void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new B().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } void display2D(int a[][]) { for(int i[] : a) { for(int j : i) { out.print(j+" "); } out.println(); } } private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } } ```
### Prompt Please formulate a cpp solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; char a[1005]; char b[1005]; int t[1005]; int sum[1005]; int main() { memset(t, 0, sizeof(t)); int p, n, m; cin >> n >> m >> p; cin >> a >> b; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i + j] != b[j]) break; else if (j == m - 1) t[i] = 1; } } sum[0] = t[0]; for (int i = 1; i < n; i++) sum[i] = sum[i - 1] + t[i]; while (p--) { int l, r; scanf("%d%d", &l, &r); l--, r--; if (n < m || r - l + 1 < m) cout << 0 << endl; else cout << sum[r - m + 1] - sum[l - 1] << endl; } return 0; } ```
### Prompt Construct a Python3 code solution to the problem outlined: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python3 n, m, q = map(int, input().split()) s1 = input() s2 = input() ans = [] for i in range(0, len(s1) - len(s2) + 1): if s1[i:i + len(s2)] == s2: ans.append(True) else: ans.append(False) for i in range(q): l, r = map(int, input().split()) if r - l + 1 < len(s2): print(0) continue print(sum(ans[l-1:r-len(s2) + 1])) ```
### Prompt Please create a solution in Cpp to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int base = 317; const int mod1 = 1e9 + 2277; const int mod2 = 1e9 + 5277; const int N = 1e3 + 5; int n, m, q, dp[N][N]; string s, t; pair<long long, long long> Hash_s[N], Hash_t[N], pw[N]; pair<long long, long long> get_hash_s(int l, int r) { long long fi = (Hash_s[r].first - Hash_s[l - 1].first * pw[r - l + 1].first) % mod1; long long se = (Hash_s[r].second - Hash_s[l - 1].second * pw[r - l + 1].second) % mod2; if (fi < 0) fi += mod1; if (se < 0) se += mod2; return make_pair(fi, se); } pair<long long, long long> get_hash_t(int l, int r) { long long fi = (Hash_t[r].first - Hash_t[l - 1].first * pw[r - l + 1].first) % mod1; long long se = (Hash_t[r].second - Hash_t[l - 1].second * pw[r - l + 1].second) % mod2; if (fi < 0) fi += mod1; if (se < 0) se += mod2; return make_pair(fi, se); } bool check(pair<long long, long long> p1, pair<long long, long long> p2) { return p1.first == p2.first && p1.second == p2.second; } void input() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m >> q; cin >> s >> t; s = "#" + s; t = "#" + t; } void solve() { pw[0] = make_pair(1, 1); Hash_s[0] = Hash_t[0] = make_pair(0, 0); for (int i = 1; i < N; i++) pw[i] = make_pair((pw[i - 1].first * base) % mod1, (pw[i - 1].second * base) % mod2); for (int i = 1; i <= n; i++) Hash_s[i] = make_pair((Hash_s[i - 1].first * base + s[i]) % mod1, (Hash_s[i - 1].second * base + s[i]) % mod2); for (int i = 1; i <= m; i++) Hash_t[i] = make_pair((Hash_t[i - 1].first * base + t[i]) % mod1, (Hash_t[i - 1].second * base + t[i]) % mod2); memset(dp, 0, sizeof(dp)); for (int i = 1; i <= n; i++) { int r = i + m - 1; if (r <= n) dp[i][r] = check(get_hash_s(i, r), get_hash_t(1, m)); for (int j = i + m; j <= n; j++) { dp[i][j] = dp[i][j - 1]; if (check(get_hash_s(j - m + 1, j), get_hash_t(1, m))) dp[i][j]++; } } while (q--) { int l, r; cin >> l >> r; cout << dp[l][r] << "\n"; } } int main() { input(); solve(); return 0; } ```
### Prompt Develop a solution in python3 to the problem described below: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python3 a,b,c=map(int,input().split()) str1=input() str2=input() string='' for i in range(a-b+1): if str1[i:i+b]==str2: string+='1' else: string+='0' for _ in range(c): x,y=map(int,input().split()) if y-x+1>=b: print(string[x-1:y-b+1].count('1')) else: print(0) ```
### Prompt Construct a Python3 code solution to the problem outlined: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python3 n,m,q = map(int,input().split()) s = input() t = input() l = [] r = [] for i in range(n-m+1): if s[i:i+m] == t: l.append(i) r.append(i+m-1) for i in range(q): x,y = map(int,input().split()) x-=1 y-=1 ans = 0 for j in range(len(l)): if x <= l[j] and y >= r[j]: ans+=1 print(ans) ```
### Prompt Construct a java code solution to the problem outlined: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```java 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; /** * @author khokharnikunj8 */ public class Main { public static void main(String[] args) { new Thread(null, new Runnable() { public void run() { new Main().solve(); } }, "1", 1 << 26).start(); } void solve() { 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(); if (n >= m) { int[] pref = new int[n + 2]; for (int i = 0; i <= n - m; i++) { boolean flag = true; for (int j = i; j < i + m; j++) if (s.charAt(j) != t.charAt(j - i)) flag = false; if (flag) pref[i + 1]++; } for (int i = 1; i <= n; i++) pref[i] += pref[i - 1]; for (int i = 0; i < q; i++) { int l = in.scanInt(); int r = in.scanInt(); if (r - m + 1 >= l) out.println(pref[r - m + 1] - pref[l - 1]); else out.println(0); } } else for (int i = 0; i < q; i++) out.println(0); } } 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 integer = 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') { integer *= 10; integer += n - '0'; n = scan(); } } return neg * integer; } public String scanString() { int c = scan(); if (c == -1) return null; while (isWhiteSpace(c)) c = scan(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = scan(); } while (!isWhiteSpace(c)); return res.toString(); } private boolean isWhiteSpace(int n) { if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true; else return false; } } } ```
### Prompt Your task is to create a java solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```java import java.util.*; public class test { public static void main(String[] args) { Scanner sc= new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int q = sc.nextInt(); sc.nextLine(); int arr[][] = new int[q][2]; String s = sc.nextLine(); String t = sc.nextLine(); for(int i=0;i<q;i++) { int a = sc.nextInt(); int b = sc.nextInt(); arr[i][0]=a; arr[i][1]=b; } int arr_s[] = new int[n]; int arr_s1[]= new int[n]; for(int j=0;j<n;j++) { if(m+j>n) break; String str = s.substring(j,m+j); // System.out.println("str "+str); if(str.equals(t)) { arr_s[m+j-1]=1; arr_s1[j]=1; } } for(int i=1;i<n;i++) { arr_s[i] = arr_s[i-1] + arr_s[i]; arr_s1[i] = arr_s1[i-1] + arr_s1[i]; } for(int i=0;i<q;i++) { int out=0; if(arr[i][0]>=2) out = arr_s[arr[i][1]-1]-arr_s1[arr[i][0]-2]; else out = arr_s[arr[i][1]-1]; if(out<0) System.out.println(0); else System.out.println(out); } } } ```
### Prompt Your task is to create a CPP solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int mxN = 1e3 + 3; bool cnt[mxN]; int sum[mxN]; int main() { int n, m, a, i, j; string s, ss; cin >> n >> m >> a >> s >> ss; for (i = 0; i < n - m + 1; i++) { bool f = true; for (j = 0; j < m; j++) if (s[i + j] != ss[j]) { f = false; break; } cnt[i] = f; } for (i = 0; i < s.size(); i++) sum[i + 1] = sum[i] + (cnt[i] ? 1 : 0); while (a--) { int b, c; cin >> b >> c; if (c - b + 1 < m) { cout << 0 << endl; continue; } cout << sum[c - m + 1] - sum[b - 1] << endl; } return 0; } ```
### Prompt Create a solution in Cpp for the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, q, m, i, j; int dp[1010][1010]; int dp2[1010]; for (i = 0; i < 1010; i++) { for (j = 0; j < 1010; j++) dp[i][j] = 0; } cin >> n >> m >> q; string str1, str2; cin >> str1; cin >> str2; int flag, k; for (i = 0; i < n; i++) { flag = 0; k = i; for (j = 0; j < m; j++) { if (k > n - 1 || str2[j] != str1[k]) { flag = 1; break; } k++; } if (flag == 0) dp2[i] = 1; else dp2[i] = 0; } int val1, val2; for (j = 0; j < n; j++) { for (i = j; i >= 0; i--) { val1 = 0; val2 = 0; if (i + m - 1 <= j) val1 = dp2[i]; if (i + 1 <= j) val2 = dp[i + 1][j]; dp[i][j] = val1 + val2; } } int num1, num2; while (q--) { cin >> num1 >> num2; cout << dp[num1 - 1][num2 - 1] << endl; } return 0; } ```
### Prompt Please provide a java coded solution to the problem described below: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```java import java.util.*; public class segmanet { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int m=sc.nextInt(); int q=sc.nextInt(); String s=sc.next(); String t=sc.next(); int k=0,count=0; if(s.length()>=t.length()) { count=(s.length()-t.length())+1; } else count=1; int a[]=new int[count]; if(s.length()>=t.length()) { for(int i=0;i<=(s.length()-t.length());i++) { String s1=s.substring(i,i+t.length()); if(s1.equals(t)) { a[k]=i+1; //System.out.println(s1+" "+a[k]); k++; } } } //System.out.println(count+"" +k); for(int i=0;i<q;i++) { int l=sc.nextInt(); int r=sc.nextInt(); int ans=0; for(int j=0;j<k;j++) { if(a[j]>=l && (a[j]+t.length()-1)<=r) ans++; //System.out.println(ans); } System.out.println(ans); } } } ```
### Prompt Please formulate a Java solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```java import java.util.*; public class probB{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int q = sc.nextInt(); sc.nextLine(); String s = sc.nextLine(); String t = sc.nextLine(); int[] indices = new int[1000]; int[] sum_indices = new int[1000]; for (int i =0;i<n-m+1;i++){ if(s.substring(i,i+m).equals(t)) indices[i]=1; //System.out.println(indices[i]); } sum_indices[0]=indices[0]; for (int i=1;i<n;i++){ sum_indices[i] = sum_indices[i-1]+indices[i]; //System.out.println(sum_indices[i]); } for (int i=0;i<q;i++){ int r = sc.nextInt()-1; //System.out.println(r); int l = sc.nextInt()-1; int sum=0; // if(l-m+1>r){ // sum =Math.max((sum_indices[l-m+2] - sum_indices[r]),0); // } // else if(l-m+1==r){ // if(s.substring(r,r+m).equals(t)) // sum=1; // } // else{ // sum=0; // } for (int j=r;j<l-m+2;j++){ sum+=indices[j]; } System.out.println(sum); } } } ```
### Prompt Please formulate a cpp solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, m, l, r, ss[10000001], q, a[10000001]; string second, t; int main() { cin >> n >> m >> q; cin >> second >> t; for (int i = 0; i < second.size(); i++) { string st = ""; for (int j = i; j < second.size(); j++) { st += second[j]; if (st == t) { a[j] = 1; } } } for (int i = 0; i < second.size() + 100000; i++) ss[i] = ss[i - 1] + a[i]; for (int yy = 0; yy < q; yy++) { cin >> l >> r; l--; r--; cout << max(ss[r] - ss[l + t.size() - 2], 0LL) << endl; } return 0; } ```
### Prompt Please create a solution in CPP to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, m, q, l, r; cin >> n >> m >> q; string a, b; cin >> a >> b; bool p[n + 1]; for (int i = 0; i < n + 1; i++) { bool f = true; for (int j = 0; j < m; j++) if (a[i + j] != b[j]) { f = false; break; } p[i + 1] = f; } while (q--) { cin >> l >> r; int c = 0; for (int i = l; i + m - 1 <= r; i++) if (p[i] && i + m - 1 <= r) c++; cout << c << endl; } } ```
### Prompt Construct a cpp code solution to the problem outlined: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; char s[1005], t[1005]; int pre[1005], q, n, m, a, b, prel[1005]; int main() { scanf("%d%d%d", &n, &m, &q); scanf("%s%s", &s[1], &t[1]); for (int i = 1; i <= n; ++i) { int j = 1, temp = i; while (temp <= n && j <= m && t[j] == s[temp]) { j++, temp++; } if (j == m + 1) { pre[i + m - 1] = pre[i + m - 2] + 1; prel[i] = prel[i - 1] + 1; } else { pre[i + m - 1] = pre[i + m - 2]; prel[i] = prel[i - 1]; } } for (int i = 1; i <= q; ++i) { scanf("%d%d", &a, &b); if (b - a + 1 >= m) printf("%d\n", pre[b] - prel[a - 1]); else printf("0\n"); } } ```
### Prompt Create a solution in python for the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python def ind(r,g,low,up): if(up-low==0): if(r[low]>g): return(low) else: return(low+1) elif(up-low==1): if(r[low]>g): return(low) elif(r[low+1]>g): return(low+1) else: return(low+2) else: k=((up+low)//2) if(r[k]<g): u=ind(r,g,k,up) return(u) else: u=ind(r,g,low,k) return(u) n,m,t=map(int,raw_input().split()) s1=str(raw_input()) s2=str(raw_input()) d=[] for i in range(n-m+2): try: if(s1[i:(i+m):1]==s2): d.append(i) except: pass r=len(d) for i in range(t): a,b=map(int,raw_input().split()) low=a-2 up=b-m if(up>low): try: print(ind(d,up,0,r-1)-ind(d,low,0,r-1)) except: print(0) else: print(0) ```
### Prompt Your challenge is to write a CPP solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> int occ[10000]; using namespace std; int main() { int n, m, q; int sol; cin >> n >> m >> q; string s1, s2; cin >> s1 >> s2; for (int i = 0; i < n; i++) { bool test = true; for (int j = 0; j < m; j++) { if (s1[i + j] != s2[j]) { test = false; break; } } occ[i] = test; } while (q--) { int sol = 0; int a, b; cin >> a >> b; a--; b--; for (int i = a; i <= b; i++) { sol += occ[i] && (b - i + 1) >= s2.length(); } cout << sol << endl; } } ```
### Prompt In CPP, your task is to solve the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, m, q, l, r, index = 0, c = 0; string s, t; cin >> n >> m >> q >> s >> t; bool a[n]; for (int i = 0; i < n; i++) { index = 0; for (int j = i; j < i + m; j++) { if (s[j] == t[index]) index++; else break; } if (index == m) a[i] = 1; else a[i] = 0; } for (int i = 0; i < q; i++) { cin >> l >> r; c = 0; for (int j = l - 1; j <= r - 1; j++) { if (a[j] && j + m - 1 <= (r - 1)) c++; } cout << c << endl; } return 0; } ```
### Prompt Create a solution in JAVA for the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```java import java.util.Scanner; public class B { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(), m = in.nextInt(), q = in.nextInt(); char[] s = in.next().toCharArray(); char[] t = in.next().toCharArray(); int[] a = new int[n+1]; for (int i = 0; i < s.length; i++) { boolean found = true; for (int j = 0; j < t.length && j+i < s.length; j++) { if (s[i+j] != t[j]) { found = false; break; } } int sum = 0; if (found) sum += 1; a[i+1] = a[i] + sum; } StringBuilder builder = new StringBuilder(); while (q-- > 0) { int l = in.nextInt(), r = in.nextInt(); int ans = 0; if (r-(m-1) >= l) { ans += a[r-(m-1)] - a[l-1]; } builder.append(ans).append("\n"); } System.out.print(builder); } } ```
### Prompt Construct a cpp code solution to the problem outlined: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long const MOD = 1e9 + 7; long long const N = 1e3 + 10; long long ara[N + 1]; long long bra[N + 1]; int main() { (ios_base::sync_with_stdio(false), cin.tie(NULL)); long long n, m, q; cin >> n >> m >> q; string str, s; cin >> str >> s; for (long long i = 0; i < n - m + 1; i++) { if (str.substr(i, m) == s) ara[i + 1]++; } for (long long i = 1; i <= n + 10; i++) { ara[i] += ara[i - 1]; } while (q--) { long long a, b; cin >> a >> b; b = b - m + 1; cout << max(0ll, ara[max(b, 0ll)] - ara[a - 1]) << endl; } } ```
### Prompt Your task is to create a CPP solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; char s1[1005], s2[1005]; int s1len, s2len; int Next[1005]; void kmp() { int i = 0; Next[i] = -1; int j = -1; while (i < s1len) { if (j == -1 || s1[i] == s1[j]) { i++; j++; Next[i] = j; } else { j = Next[j]; } } } int main(void) { int Q, L, R; scanf("%d%d%d", &s2len, &s1len, &Q); scanf("%s", s2); scanf("%s", s1); kmp(); while (Q--) { scanf("%d%d", &L, &R); int i = L - 1, j = 0; int ans = 0; while (i < R) { if (j == -1 || s2[i] == s1[j]) { i++; j++; } else { j = Next[j]; } if (j == s1len) { ans++; j = Next[j]; } } printf("%d\n", ans); } return 0; } ```
### Prompt Please formulate a cpp solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long int countSetBits(long long int n) { long long int count = 0; while (n) { n &= (n - 1); count++; } return count; } long long int power(long long int x, long long int y) { if (y == 0) return 1; else if (y % 2 == 0) return power(x, y / 2) * power(x, y / 2); else return x * power(x, y / 2) * power(x, y / 2); } long long int gcd(long long int a, long long int b) { long long int r; while (b) { r = a % b; a = b; b = r; } return a; } bool pr[1000007]; void sieve() { pr[0] = 1; pr[1] = 1; for (int i = 2; i < sqrt(1000007); i++) { for (int j = 2 * i; j <= 1000007; j += i) { pr[j] = 1; } } } const int MOD = 1e9 + 7; const int SIZE = 4e6 + 10; const int MAX = 1e9 + 1; int main() { int n, m, q; scanf("%d%d%d", &n, &m, &q); string s, t; cin >> s >> t; int a[n + 1]; memset((a), 0, sizeof((a))); for (int i = (0); i < (n - m + 1); ++i) { if (s.substr(i, m) == t) a[i] = 1; } while (q--) { int cnt = 0; int l, r; scanf("%d%d", &l, &r); for (int i = (l - 1); i < (r - m + 1); ++i) { if (a[i]) { cnt++; } } cout << cnt; cout << endl; } return 0; } ```
### Prompt Construct a Cpp code solution to the problem outlined: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct re { int x, y, cs; }; const long long module = 1000000000 + 7; const int maxN = 1000 + 10; int n, m, q, kq[maxN]; string s, t; void nhap() { cin >> n >> m >> q >> s >> t; } int kt(int x) { for (int i = 1; i <= m - 1; ++i) if (s[x + i] != t[i]) return (0); return (1); } void xuly() { for (int i = 0; i <= n - m; ++i) { if (i > 0) kq[i] = kq[i - 1]; if (s[i] == t[0]) kq[i] += kt(i); } } void xuat() { int x, y; for (int i = 1; i <= q; ++i) { cin >> x >> y; x--; y--; if (y - x + 1 < m) cout << "0"; else if (x == 0) cout << kq[y - m + 1]; else cout << kq[y - m + 1] - kq[x - 1]; cout << "\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); nhap(); xuly(); xuat(); return 0; } ```
### Prompt Generate a JAVA solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```java import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.charset.Charset; import java.util.Comparator; public class ECF48A { public static void main(String[] args) throws Exception { boolean local = System.getProperty("ONLINE_JUDGE") == null; boolean async = false; Charset charset = Charset.forName("ascii"); FastIO io = local ? new FastIO(new FileInputStream("E:\\DATABASE\\TESTCASE\\ECF48A.in"), System.out, charset) : new FastIO(System.in, System.out, charset); Task task = new Task(io); if (async) { Thread t = new Thread(null, task, "dalt", 1 << 27); t.setPriority(Thread.MAX_PRIORITY); t.start(); t.join(); } else { task.run(); } io.flush(); } public static class Task implements Runnable { final FastIO io; public Task(FastIO io) { this.io = io; } @Override public void run() { solve(); } public void solve() { int n = io.readInt(); int m = io.readInt(); int q = io.readInt(); String s = io.readString(); String t = io.readString(); int[] count = new int[n]; int lastIndex = 0; while (lastIndex < n && (lastIndex = s.indexOf(t, lastIndex)) != -1) { count[lastIndex]++; lastIndex++; } for (int i = 1; i < n; i++) { count[i] += count[i - 1]; } for (int i = 0; i < q; i++) { int l = io.readInt() - 1; int r = io.readInt() - m; if (r < l) { io.cache.append(0).append('\n'); continue; } if (l == 0) { io.cache.append(count[r]).append('\n'); } else { io.cache.append(count[r] - count[l - 1]).append('\n'); } } } } public static class FastIO { private final InputStream is; private final OutputStream os; private final Charset charset; private StringBuilder defaultStringBuf = new StringBuilder(1 << 8); public final StringBuilder cache = new StringBuilder(); private byte[] buf = new byte[1 << 13]; private int bufLen; private int bufOffset; private int next; public FastIO(InputStream is, OutputStream os, Charset charset) { this.is = is; this.os = os; this.charset = charset; } private int read() { while (bufLen == bufOffset) { bufOffset = 0; try { bufLen = is.read(buf); } catch (IOException e) { throw new RuntimeException(e); } if (bufLen == -1) { return -1; } } return buf[bufOffset++]; } public void skipBlank() { while (next >= 0 && next <= 32) { next = read(); } } public int readInt() { int sign = 1; skipBlank(); if (next == '+' || next == '-') { sign = next == '+' ? 1 : -1; next = read(); } int val = 0; if (sign == 1) { while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } } else { while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } } return val; } public long readLong() { int sign = 1; skipBlank(); if (next == '+' || next == '-') { sign = next == '+' ? 1 : -1; next = read(); } long val = 0; if (sign == 1) { while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } } else { while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } } return val; } public double readDouble() { long num = readLong(); if (next != '.') { return num; } next = read(); long divisor = 1; long later = 0; while (next >= '0' && next <= '9') { divisor = divisor * 10; later = later * 10 + next - '0'; next = read(); } if (num >= 0) { return num + (later / (double) divisor); } else { return num - (later / (double) divisor); } } public String readString(StringBuilder builder) { skipBlank(); while (next > 32) { builder.append((char) next); next = read(); } return builder.toString(); } public String readString() { defaultStringBuf.setLength(0); return readString(defaultStringBuf); } public int readString(char[] data, int offset) { skipBlank(); int originalOffset = offset; while (next > 32) { data[offset++] = (char) next; next = read(); } return offset - originalOffset; } public int readString(byte[] data, int offset) { skipBlank(); int originalOffset = offset; while (next > 32) { data[offset++] = (byte) next; next = read(); } return offset - originalOffset; } public void flush() { try { os.write(cache.toString().getBytes(charset)); os.flush(); cache.setLength(0); } catch (IOException e) { throw new RuntimeException(e); } } public boolean hasMore() { skipBlank(); return next != -1; } } public static class Memory { public static <T> void swap(T[] data, int i, int j) { T tmp = data[i]; data[i] = data[j]; data[j] = tmp; } public static <T> int min(T[] data, int from, int to, Comparator<T> cmp) { int m = from; for (int i = from + 1; i < to; i++) { if (cmp.compare(data[m], data[i]) > 0) { m = i; } } return m; } public static <T> void move(T[] data, int from, int to, int step) { int len = to - from; step = len - (step % len + len) % len; Object[] buf = new Object[len]; for (int i = 0; i < len; i++) { buf[i] = data[(i + step) % len + from]; } System.arraycopy(buf, 0, data, from, len); } } } ```
### Prompt Your task is to create a PYTHON solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python n,m,q = map(int, raw_input().split()) w = str(raw_input()) sw = str(raw_input()) t=[0]*n t2=[0]*n ile=0 p_ile = 0 for x in range(0, n-(m-1), +1): ile = w.count(sw, x, x+m) t[x]=ile for y in range(0,len(t),+1): if t[y]==1: p_ile += 1 t2[y] = p_ile tl=0 tp=0 for h in range(0, q, +1): a,b=map(int,raw_input().split()) if b-a<m-1: print '0' continue a -= 1 b -= 1 if a >= 1: tl = t2[a-1] tp = t2[b-(m-1)] else: tl = 0 tp = t2[b-(m-1)] print tp-tl ```
### Prompt Develop a solution in CPP to the problem described below: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, m, t; cin >> n >> m >> t; string a, b; cin >> a >> b; int begin[n]; int cumm[n]; memset(begin, 0, sizeof(begin)); for (int i = 0; i <= n - m; i++) { bool match = true; for (int j = i; j < i + m; j++) { if (b[j - i] != a[j]) { match = false; break; } } if (match) { begin[i] = 1; } } cumm[0] = begin[0]; for (int i = 1; i < n; i++) { cumm[i] = cumm[i - 1] + begin[i]; } for (int p = 0; p < t; p++) { int l, r; cin >> l >> r; l--; r--; if (l > 0) { if (r - m + 1 >= 0) { cout << max(0, cumm[r - m + 1] - cumm[l - 1]) << endl; } else { cout << 0 << endl; } } else { if (r - m + 1 >= 0) { cout << cumm[r - m + 1] << endl; } else { cout << 0 << endl; } } } return 0; } ```
### Prompt Create a solution in Cpp for the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<int> v; int main() { int n, m, q; string s, t; cin >> n >> m >> q; cin >> s >> t; for (int i = 0; i < n; i++) { if (s.substr(i, m) == t) v.push_back(1); else v.push_back(0); } while (q--) { int left, right, ans = 0; cin >> left >> right; for (int i = left - 1; i < right; i++) { if (right - i >= m) ans += v[i]; } cout << ans << endl; } return 0; } ```
### Prompt Develop a solution in python3 to the problem described below: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python3 n, m, q = map(int, input().split()) s = input() t = input() w = s ind = w.find(t) a = [] st = [0] en = [0]*(m-1) count = 0 pred = 0 while ind != -1: if len(a) > 0: el = a[-1] else: el = -1 a.append(el + ind+1) st += [count]*(el+ind+1-pred) # print(count, el+ind+1-pred) en += [count]*(el+ind+1-pred) count += 1 pred = el+ind+1 w = w[ind+1:] ind = w.find(t) st += [count]*(n-pred) en += [count]*(n-pred-m+1) #print(st) #print(en) #print(a) for i in range(q): l, r = map(int, input().split()) l -= 1 r -= 1 if en[r]-st[l] >= 0: print(en[r]-st[l]) else: print(0) ```
### Prompt Your challenge is to write a Python3 solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python3 n,m,q=map(int,input().split()) a=input() b=input() ans=[0 for i in range(n)] for i in range(n-m+1): if a[i]==b[0]: t=0 for j in range(1,m): if a[j+i]!=b[j]: t=1 break if t==0: ans[i]+=1 sub=[0] for i in ans: sub.append(sub[-1]+i) for _ in range(q): l,r=map(int,input().split()) if r-l+1<m: print(0) else: print(sub[r-m+1]-sub[l-1]) ```
### Prompt Develop a solution in CPP to the problem described below: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 1e3 + 5; char s[maxn]; char t[maxn]; int ans[maxn]; int main() { int n, m, q; scanf("%d%d%d", &n, &m, &q); int l, r; int cnt = 0; scanf("%s%s", s, t); if (n < m) { while (q--) { printf("0\n"); } return 0; } int flag; for (int i = 0; i < n - m + 1; i++) { flag = 1; for (int j = i, pp = 0; pp < m; j++, pp++) { if (s[j] != t[pp]) { flag = 0; break; } } if (flag) { ans[cnt++] = i + 1; } } int n1, n2; while (q--) { scanf("%d%d", &l, &r); n1 = 0, n2 = 0; for (int i = 0; i < cnt; i++) { if (ans[i] < l) { n1++; } if (ans[i] <= r - m + 1) { n2++; } } printf("%d\n", max(n2 - n1, 0)); } } ```
### Prompt Create a solution in PYTHON3 for the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python3 from itertools import accumulate from sys import stdin all_in = list(el.rstrip('\n') for el in stdin.readlines()) n, m, q = map(int, all_in[0].split()) s = all_in[1] t = all_in[2] l_r = [tuple(map(int, el.split())) for el in all_in[3:]] in_ = [int(t == s[i: i + m]) for i in range(n - m + 1)] acc = [0] + list(accumulate(in_)) ans = list() for a, b in l_r: if b - a + 1 < m: ans.append(0) continue ans.append(acc[max(0, b - m + 1)] - acc[a - 1]) print('\n'.join(map(str, ans))) ```
### Prompt Please formulate a python3 solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python3 n, m, q = list(map(int, input().split())) s = input() t = input() arr = [0] * n for i in range(n - m + 1): if t == s[i:i+m]: arr[i] = 1 if i: arr[i] += arr[i - 1] for i in range(q): l, r = list(map(int, input().split())) l -= 1 r -= 1 L = l - 1 R = r - m + 1 if R < L: print(0) continue if L >= 0: print(arr[R] - arr[L]) else: print(arr[R]) ```
### Prompt Construct a cpp code solution to the problem outlined: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long n, m, q; cin >> n >> m >> q; string s; cin >> s; string r; cin >> r; long long lps[m]; lps[0] = 0; long long len = 0; long long i = 1; while (i < m) { if (r[i] == r[len]) { len++; lps[i] = len; i++; } else { if (len) { len = lps[len - 1]; } else { lps[i] = 0; i++; } } } for (long long p = 0; p < q; p++) { long long x, y; cin >> x >> y; long long count = 0; string k = s.substr(x - 1, y - x + 1); long long j = 0; i = 0; while (i < k.size()) { if (r[j] == k[i]) { i++; j++; } if (j == m) { count++; j = lps[j - 1]; } else if (i < n and r[j] != k[i]) { if (j != 0) { j = lps[j - 1]; } else { i++; } } } cout << count << endl; } return 0; } ```
### Prompt Create a solution in PYTHON3 for the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python3 n,m,q = [int(s) for s in input().split()] s = input() t = input() a = [] b = [] for i in range(n): b.append(0) for i in range(n - m + 1): if(s[i:(i+m)] == t): a.append(i) for i in a: for j in range(i , n): b[j] += 1 #print(a) #print(b) l = [] r = [] for i in range(q): x,y = [int(item) for item in input().split()] l.append(x - 1) r.append(y - 1) for i in range(q): if l[i] == 0: if r[i] - m + 1 >= l[i]: print(b[r[i] - m + 1]) else: print(0) else: if r[i] - m + 1 >= l[i]: print(b[r[i] - m + 1] - b[l[i] - 1]) else: print(0) ```
### Prompt Develop a solution in JAVA to the problem described below: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```java 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.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.StringTokenizer; //http://codeforces.com/contest/1016/problem/B public class Main2 { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); XeniaAndBitOperations solver = new XeniaAndBitOperations(); // int test = in.nextInt(); // while(test-- > 0) solver.solve(1, in, out); out.close(); } static class XeniaAndBitOperations { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); int q = in.nextInt(); String s = in.next(); String t = in.next(); ArrayList<Integer> pos = new ArrayList<>(); //preprocess s to find pos of t for(int i=0; i<n; i++){ int te = i; int j=0; while(te<n && j<m && s.charAt(te)==t.charAt(j)){ te++; j++; } if(j==m) pos.add(i); } for(int i=0; i<q; i++){ int ans =0; int l = in.nextInt()-1; int r = in.nextInt()-1; for(Integer ind: pos){ if(ind>=l && (ind+m-1)<=r){ ans++; } } out.println(ans); } } } 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()); } } } ```
### Prompt Your challenge is to write a Python3 solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python3 n,m,q = map(int, input().split()) a = input() b = input() wynik = '' for i in range(0, n - m + 1): if a[i : i + m] == b: wynik += '1' else: wynik += '0' for i in range(q): x , y = map(int, input().split()) if y - x + 1 >= m: print(wynik[x - 1 : y - m + 1].count('1')) else: print(0) ```
### Prompt Please formulate a Cpp solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, q; cin >> n >> m >> q; string s, t; cin >> s >> t; vector<int> k(n, false); for (int i = 0; i < n; ++i) { bool flag = true; for (int j = 0; j < m; ++j) { if (i + j >= n || s[i + j] != t[j]) flag = false; } if (flag) { if (i == 0) { k[i] = 1; continue; } k[i] = k[i - 1] + 1; } else { if (i == 0) { k[i] = 0; continue; } k[i] = k[i - 1]; } } for (int i = 0; i < q; ++i) { int l, r; cin >> l >> r; l--; r--; if (r - l + 1 < m) { cout << 0 << "\n"; continue; } if (l - 1 < 0) { cout << k[r - m + 1] << "\n"; continue; } cout << k[r - m + 1] - k[l - 1] << "\n"; continue; } return 0; } ```
### Prompt In python3, your task is to solve the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python3 f = lambda: map(int, input().split()) n, m, q = f() s = input() sb = input() indexes = '' for i in range(n - m + 1): sec_i = i + m if s[i:sec_i] == sb: indexes += '1' else: indexes += '0' for j in range(q): l, r = f() l -= 1 print(indexes[l:max(r - m + 1, 0)].count('1')) ```
### Prompt Your challenge is to write a PYTHON3 solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python3 from sys import stdin def main(): n, m, q = map(int, input().split()) s, t = input(), input() field = [] i, j, c = -1, 0, 0 m -= 1 while True: j = s.find(t, i + 1) if j != -1: field += [c] * (j - i) i = j c += 1 else: field += [c] * (n - i + 1) break res = stdin.read().splitlines() for i, s in enumerate(res): l, r = map(int, s.split()) l -= 1 r -= m res[i] = str(field[r] - field[l] if l <= r else 0) print('\n'.join(res)) if __name__ == '__main__': main() ```
### Prompt Create a solution in JAVA for the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```java import java.util.*; import java.io.*; import java.lang.*; public class occurrences{ public static void main(String[] args) { FastReader f = new FastReader(); PrintWriter wr = new PrintWriter(System.out); int n,m,q; n=f.nextInt(); m=f.nextInt(); q=f.nextInt(); char[] s = f.nextLine().toCharArray(); char[] t = f.nextLine().toCharArray(); int[] dp = new int[n]; for(int i=0;i<n-m+1;i++) { int flag=0; for(int j=0;j<m;j++) { if(s[j+i] != t[j]) { flag=1; } } if(flag == 0) { dp[i]++; } } for(int i=1;i<n;i++) { dp[i] += dp[i-1]; } while(q>0) { int l = f.nextInt()-1; int r = f.nextInt()-1; int count =0; if(r-m+1 >= 0) { count+=dp[r-m+1]; } if(l-1 >= 0) { count -= dp[l-1]; } wr.println(Math.max(count,0)); q--; } wr.close(); } 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; } } } ```
### Prompt Please provide a JAVA coded solution to the problem described below: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```java import java.io.*; import java.util.*; public class Mainn { FastReader scn; PrintWriter out; String INPUT = ""; void solve() { int n = scn.nextInt(), m = scn.nextInt(), q = scn.nextInt(), ind = 0; String str = scn.next(), s = scn.next(); int[] arr = new int[n]; while(str.indexOf(s, ind) != -1) { ind = str.indexOf(s, ind); arr[ind++]++; } while(q-- > 0) { int l = scn.nextInt() - 1, r = scn.nextInt() - 1, ans = 0; for(int i = l; i + m - 1 <= r; i++) { ans += arr[i]; } out.println(ans); } } void run() throws Exception { long time = System.currentTimeMillis(); boolean oj = System.getProperty("ONLINE_JUDGE") != null; out = new PrintWriter(System.out); scn = new FastReader(oj); solve(); out.flush(); if (!oj) { System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" })); } } public static void main(String[] args) throws Exception { new Mainn().run(); } class FastReader { InputStream is; public FastReader(boolean onlineJudge) { is = onlineJudge ? System.in : new ByteArrayInputStream(INPUT.getBytes()); } byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } double nextDouble() { return Double.parseDouble(next()); } char nextChar() { return (char) skip(); } String next() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } char[] next(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } char[][] nextMatrix(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = next(m); return map; } int[] nextArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int nextInt() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } long nextLong() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } int[] shuffle(int[] arr) { Random r = new Random(); for (int i = 1, j; i < arr.length; i++) { j = r.nextInt(i); arr[i] = arr[i] ^ arr[j]; arr[j] = arr[i] ^ arr[j]; arr[i] = arr[i] ^ arr[j]; } return arr; } } } ```
### Prompt Generate a JAVA solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```java import java.io.*; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class Main implements Runnable { static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 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 String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } 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 long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long 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 double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } 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); } } public static void main(String args[]) throws Exception { new Thread(null, new Main(),"Main",1<<26).start(); } static void merge(int arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; int L[] = new int [n1]; int R[] = new int [n2]; for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; int i = 0, j = 0; int k = l; while (i < n1 && j < n2){ if (L[i] <= R[j]){ arr[k] = L[i]; i++; } else{ arr[k] = R[j]; j++; } k++; } while (i < n1){ arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } static void sort(int arr[], int l, int r) { if (l < r) { int m = (l+r)/2; sort(arr, l, m); sort(arr , m+1, r); merge(arr, l, m, r); } } static void merge(long arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; long L[] = new long [n1]; long R[] = new long [n2]; for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; int i = 0, j = 0; int k = l; while (i < n1 && j < n2){ if (L[i] <= R[j]){ arr[k] = L[i]; i++; } else{ arr[k] = R[j]; j++; } k++; } while (i < n1){ arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } static void sort(long arr[], int l, int r) { if (l < r) { int m = (l+r)/2; sort(arr, l, m); sort(arr , m+1, r); merge(arr, l, m, r); } } static int gcd(int a, int b){ if (b == 0) return a; return gcd(b, a % b); } static long gcd(long a, long b){ if (b == 0) return a; return gcd(b, a % b); } public long m=(long)1e9+7;; public void run() { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int n=in.nextInt(),m=in.nextInt(),q=in.nextInt(); String s=in.next(),t=in.next(); int ind[]=new int[n+1]; for(int i=0;i<n;i++){ int index=s.indexOf(t,i); if(index==-1) break; ind[index+1]++; i=index; } for(int i=1;i<n+1;i++) ind[i]+=ind[i-1]; for(int i=0;i<q;i++){ int l=in.nextInt(),r=in.nextInt(); if((r-l+1)<m) out.println(0); else out.println(ind[r-m+1]-ind[l-1]); } out.close(); } } ```
### Prompt Your challenge is to write a PYTHON3 solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python3 n,m,q=map(int,input().split()) s=input() t=input() x=[] for i in range(n): x.append([0]*n) for i in range(n-m+1): if s[i:i+m]==t: x[i][i+m-1]=1 for j in range(m,n): for i in range(n-j): x[i][i+j]=x[i+1][i+j]+x[i][i+j-1] if j>1: x[i][i+j]-=x[i+1][i+j-1] for i in range(q): a,b=map(int,input().split()) print(x[a-1][b-1]) ```
### Prompt In PYTHON3, your task is to solve the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python3 n, m, q = map(int, input().split()) s = input() t = input() if m == 0 or n == 0 or n < m: for i in range(q): print('0') exit() occur = [0]*(n-m+1) for i in range(n-m+1): if s[i:i+m] == t: occur[i] = occur[i-1] + 1 if i > 0 else 1 else: occur[i] = occur[i-1] if i > 0 else 0 occur += [occur[-1]]*(m-1) occur = [0] + occur # print(list(range(n))) # print(list(s)) # print(occur) for i in range(q): l, r = map(int, (input().split())) if r-l < m - 1: print(0) else: print(occur[(r-m+1) if r-m+1>=0 else 0] - occur[l-1]) ```
### Prompt Your challenge is to write a java solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```java import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.InputMismatchException; public class B { public static void main(String args[]) { FastReader Reader=new FastReader(System.in); int N=Reader.nextInt(); int M=Reader.nextInt(); int Q=Reader.nextInt(); String s1=Reader.nextLine(); String s2=Reader.nextLine(); ArrayList<Integer> arr=new ArrayList<>(); for(int i=0;i<N-M+1;i++) { boolean flag=true; for(int j=i;j<i+M;j++) { char ch=s1.charAt(j); char ch2=s2.charAt(j-i); if(ch==ch2) continue; else { flag=false; break; } } if (flag) { arr.add(i+1); } } for(int i=0;i<Q;i++) { int a=Reader.nextInt(); int b=Reader.nextInt(); int count=0; for(int j=0;j<arr.size();j++) { int x=arr.get(j); //System.out.println(x); if(x>b) { break; } else if(x>=a && x+M-1<=b) { count++; } } System.out.println(count); } } public static void sorts(int arr[],int N) { Integer arr_obj[]=new Integer[N]; for(int i=0; i<N; ++i){ arr_obj[i] = new Integer(arr[i]); } Arrays.sort(arr_obj); for(int i=0; i<N; ++i){ arr[i] = arr_obj[i]; } } } class FastReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int pnumChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (pnumChars == -1) { throw new InputMismatchException(); } if (curChar >= pnumChars) { curChar = 0; try { pnumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (pnumChars <= 0) { return -1; } } return buf[curChar++]; } public String next() { return nextString(); } 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 == ',') { c = read(); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long 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 nextString() { 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 String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } ```
### Prompt Construct a java code solution to the problem outlined: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```java import java.util.Scanner; public class B1016 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(), m = in.nextInt(), q = in.nextInt(), i, j, count = 0, lastIndex = -1, currIndex = -1; String s = in.next(), t = in.next(); boolean[] query = new boolean[n]; for(i = 0; i < n - m + 1; ++i) if(s.substring(i, i + m).equals(t)) query[i] = true; int l, r; for(i = 0; i < q; i++) { l = in.nextInt() - 1; r = in.nextInt() - 1; count = 0; for(j = l; j <= r - m + 1; j++) if(query[j]) count++; System.out.println(count); } } } ```
### Prompt Please provide a cpp coded solution to the problem described below: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, m, s; string a, b; int num[200005]; int t = 0; cin >> n >> m >> s; cin >> a >> b; for (int i = 0; i < n; i++) { int flag = 0; for (int j = 0; j < m; j++) { if (a[i + j] != b[j]) { flag = 1; break; } } if (flag == 0) num[t++] = i; } int q, w; for (int i = 0; i < s; i++) { int sum = 0; cin >> q >> w; for (int j = 0; j < t; j++) { if (q <= num[j] + 1 && w >= num[j] + m) sum++; } cout << sum << endl; } return 0; } ```
### Prompt Your challenge is to write a JAVA solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```java import java.io.BufferedReader; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; import static java.lang.Math.*; public class SegmentOccurrences implements Closeable { private InputReader in = new InputReader(System.in); private PrintWriter out = new PrintWriter(System.out); public void solve() { int n = in.ni(), m = in.ni(), q = in.ni(); char[] x = in.next().toCharArray(), y = in.next().toCharArray(); int[] after = new int[n + 1]; for (int begin = n - m; begin >= 0; begin--) { boolean ok = true; for (int i = begin, j = 0; i < begin + m; i++, j++) { ok &= x[i] == y[j]; } after[begin] = after[begin + 1]; if (ok) { after[begin] = after[begin + 1] + 1; } } while (q-- > 0) { int left = in.ni() - 1, right = in.ni() - 1; if (right - left + 1 < m) { out.println(0); } else { out.println(after[left] - after[right - m + 2]); } } } @Override public void close() throws IOException { in.close(); 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 ni() { return Integer.parseInt(next()); } public long nl() { return Long.parseLong(next()); } public void close() throws IOException { reader.close(); } } public static void main(String[] args) throws IOException { try (SegmentOccurrences instance = new SegmentOccurrences()) { instance.solve(); } } } ```
### Prompt Please create a solution in JAVA to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class B { static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] nextArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] nextArray(long n) { long[] a = new long[(int) n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } static class FastWriter extends PrintWriter { FastWriter(){ super(System.out); } void println(int[] array) { for(int i=0; i<array.length; i++) { print(array[i]+" "); } println(); } void println(long [] array) { for(int i=0; i<array.length; i++) { print(array[i]+" "); } println(); } } static class Comp implements Comparator<int[]> { public int compare(int[] str1, int[] str2) { if(str1[1]>str2[1]){ return -1; }else if(str1[1]<str2[1]){ return 1; }else { if(str1[0]>str2[0]){ return 1; }else { return -1; } } } } public static void main(String[] args){ FastScanner in = new FastScanner(); FastWriter out = new FastWriter(); int n=in.nextInt(); int m=in.nextInt(); int q=in.nextInt(); int[] cnt=new int[n]; String a=in.next(); String b=in.next(); int len=b.length(); for (int i = 0; i < n-m+1; i++) { boolean flag=true; for (int j = 0; j < m; j++) { if(a.charAt(i+j)!=b.charAt(j)){ flag=false; break; } } if(flag){ cnt[i]=1; } } while (q-->0){ int l=in.nextInt()-1; int r=in.nextInt()-1; if(r-l+1<len){ out.println(0); }else { int cn=0; for (int i = l; i <=r-m+1 ; i++) { cn+=cnt[i]; } out.println(cn); } } out.close(); } } ```
### Prompt Generate a JAVA solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```java //package CodeForces.EducationalRound48; import java.io.*; import java.util.Arrays; import java.util.StringTokenizer; public class B { public static void main(String[] args) { sc sc = new sc(); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int n = sc.nextInt(), m = sc.nextInt(), q = sc.nextInt(); String s = sc.nextLine(), t = sc.nextLine(); int res[] = new int[n + 1]; for(int i=0;i<s.length();i++) { int x = s.indexOf(t, i); if(x != -1) { res[x + 1]++; i = x; } else break; } for(int i=1;i<=n;i++) res[i] += res[i - 1]; while(q-- > 0) { int l = sc.nextInt(), r = sc.nextInt(); if(t.length() > (r - l + 1)) out.println(0); else out.println(res[r - t.length() + 1] - res[l - 1]); } out.close(); } public static class sc { BufferedReader br; StringTokenizer st; public sc() { 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(); } Integer 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; } } } ```
### Prompt In python, your task is to solve the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python from __future__ import division, print_function def main(): n, m, q = map(int, input().split()) s = input() t = input() ans = [] i, cnt = 0, 0 while 1: si = s.find(t, i) if si != -1: for _ in range(si - i + 1): ans.append(cnt) cnt += 1 i = si + 1 else: ans += [cnt] * (n - i + 1) break res = [] for _ in range(q): l, r = map(int, input().split()) l -= 1 r -= m - 1 res.append(str(ans[r] - ans[l] if r >= l else 0)) print('\n'.join(res)) # PyPy 2 Fast IO py2 = round(0.5) if py2: from future_builtins import ascii, filter, hex, map, oct, zip range = xrange import os, sys from io import IOBase, BytesIO BUFSIZE = 8192 class FastIO(BytesIO): newlines = 0 def __init__(self, file): self._file = file self._fd = file.fileno() self.writable = "x" in file.mode or "w" in file.mode self.write = super(FastIO, self).write if self.writable else None def _fill(self): s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0]) return s def read(self): while self._fill(): pass return super(FastIO,self).read() def readline(self): while self.newlines == 0: s = self._fill(); self.newlines = s.count(b"\n") + (not s) self.newlines -= 1 return super(FastIO, self).readline() def flush(self): if self.writable: os.write(self._fd, self.getvalue()) self.truncate(0), self.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable if py2: self.write = self.buffer.write self.read = self.buffer.read self.readline = self.buffer.readline else: 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') sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip('\r\n') # Importing Modules import math import collections from collections import Counter from math import fabs if __name__ == "__main__": main() ```
### Prompt Your challenge is to write a Cpp solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long M = 1000005; int occur[1005]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, q; string s, t; cin >> n >> m >> q; cin >> s >> t; for (int i = 0; i < n - m + 1; i++) { if (s.substr(i, m) == t) { occur[i + 1] = occur[i] + 1; } else occur[i + 1] = occur[i]; } while (q--) { int l, r; cin >> l >> r; if ((r - l + 1) >= m) { cout << occur[r - m + 1] - occur[l - 1] << '\n'; } else cout << 0 << '\n'; } return 0; } ```
### Prompt Please formulate a PYTHON3 solution to the following problem: You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≀ i ≀ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≀ n, m ≀ 10^3, 1 ≀ q ≀ 10^5) β€” the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the arguments for the i-th query. Output Print q lines β€” the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. ### Response ```python3 n,m,q=map(int,input().split()) s=input() t=input() x=0 dp1=[] while x<n: if s[x-m+1:x+1]==t: dp1.append(1) else: dp1.append(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) ```