Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
cb01
This is a very simple problem. Given the value of N and K, you need to tell us the value of the binomial coefficient C(N,K). You may rest assured that K ≤ N and the maximum value of N is 1,000,000,000,000,000. Since the value may be very large, you need to compute the result modulo 1009. Input The first line of the input contains the number of test cases T, at most 1000. Each of the next T lines consists of two space separated integers N and K, where 0 ≤ K ≤ N and 1 ≤ N ≤ 1,000,000,000,000,000. Output For each test case, print on a new line, the value of the binomial coefficient C(N,K) modulo 1009. Example Input: 3 3 1 5 2 10 3 Output: 3 10 120
1
6
memo = {} def C(n, r, p): ''' {n choose r} modulo p (p is prime) ''' if r < 0 or r > n: return 0 if r == 0 or r == n: return 1 if n >= p: # Lucas theorem return C(n/p, r/p, p) * C(n%p, r%p, p) % p if (n, r, p) not in memo: memo[n, r, p] = (C(n-1, r-1, p) + C(n-1, r, p)) % p return memo[n, r, p] t=int(raw_input()) p=int(1009) for i in range(t): n,r=map(int,raw_input().split()) print C(n,r,p)
PYTHON
crancrd
Little johny was working on the co-ordinate plane.As he is still a novice he calculates the distance between two points P1 and P2 in the wrong way.He calculates the distance between P1(x1,y1) and P2(x2,y2) as : D(P1,P2) = |x1 - x2| + |y1 - y2| His teacher gave him a homework to calculate the area of a circle of radius R. Johny is aware of the definitions of all the figures just like any smart kid is. He knows that a circle is the locus of the point at a fixed radius from the center of the circle in a plane..   Input The first line of input will contain T- number of test cases.It will be followed by T lines each containing the radius R Output For every test case output in a separate line the largest integer less than or equal to the area of the circle as calculated by johny.. Constraints 1 ≤ T ≤ 10000 1 ≤ R ≤ 100000000   Example Input: 1 1 Output: 2.
1
6
t=int(raw_input()) for asd in range(t): n=int(raw_input()) print 2*n*n
PYTHON
crancrd
Little johny was working on the co-ordinate plane.As he is still a novice he calculates the distance between two points P1 and P2 in the wrong way.He calculates the distance between P1(x1,y1) and P2(x2,y2) as : D(P1,P2) = |x1 - x2| + |y1 - y2| His teacher gave him a homework to calculate the area of a circle of radius R. Johny is aware of the definitions of all the figures just like any smart kid is. He knows that a circle is the locus of the point at a fixed radius from the center of the circle in a plane..   Input The first line of input will contain T- number of test cases.It will be followed by T lines each containing the radius R Output For every test case output in a separate line the largest integer less than or equal to the area of the circle as calculated by johny.. Constraints 1 ≤ T ≤ 10000 1 ≤ R ≤ 100000000   Example Input: 1 1 Output: 2.
1
6
# your code goes here t=int(raw_input()) for _ in range(0,t): r=int(raw_input()) print 2*r**2
PYTHON
crancrd
Little johny was working on the co-ordinate plane.As he is still a novice he calculates the distance between two points P1 and P2 in the wrong way.He calculates the distance between P1(x1,y1) and P2(x2,y2) as : D(P1,P2) = |x1 - x2| + |y1 - y2| His teacher gave him a homework to calculate the area of a circle of radius R. Johny is aware of the definitions of all the figures just like any smart kid is. He knows that a circle is the locus of the point at a fixed radius from the center of the circle in a plane..   Input The first line of input will contain T- number of test cases.It will be followed by T lines each containing the radius R Output For every test case output in a separate line the largest integer less than or equal to the area of the circle as calculated by johny.. Constraints 1 ≤ T ≤ 10000 1 ≤ R ≤ 100000000   Example Input: 1 1 Output: 2.
1
6
t=int(input()) while(t>0): n=int(input()) print 2*n*n t-=1
PYTHON
gshuff
Presti Digitator, the world-famous magician, is highly dissatisfied with his way of shuffling cards. Just yesterday, a perky guy in the audience demanded that he shuffle the cards once again, and he was aghast to see that the bottom card had not changed. Even though Mr. Digitator pacified the crowd with a few excellent tricks, the whole experience left him sad. He thinks that his shuffling leaves too many cards unchanged. He has decided to return to the drawing board, and retrain himself. He thinks that a "good" shuffle should leave no card in its old position. Assume that cards are numbered sequentially starting from 1. For example, if there are 4 cards initially arranged in the order 1,2,3,4, a shuffle of 2,3,4,1 would be considered good, while 3,2,1,4 would be bad since 2 and 4 are unchanged in the shuffled order. Digitator wonders whether good shuffles are rare - he would like to know, how many good shuffles of a given deck of cards there are. Input For this question, you are given a series of numbers on distinct lines. The first line contains a number (let’s call it n) denotes how many decks there are to shuffle. This is followed by n lines, each containing a positive number less than or equal to 20. Each such number denotes the number of cards in that deck. Output The output should have several lines. The i-th line is the number of good shuffles for the number of cards described in the i-th line. The output will fit inside a 64-bit integer. Example Input: 2 2 4 Output: 1 9
1
6
shuffle_list=[0,0,1] for i in xrange(3,21): shuffle_list.append((i-1)*(shuffle_list[-1]+shuffle_list[-2])) for i in xrange(input()): print shuffle_list[input()]
PYTHON
mdosa
Harish has decided to go to Arya's hotel this morning. We all know he is crazy for masala dosas. And as usual he is always hungry. He decided to order all the masala dosas at once. But then he realised that he did not have enough money to buy all of them. So he decided to share the amount with his friend Ozil. But both of them are fans of even numbers. Both of them says they want to eat even number of dosas. Ozil is ready to put the share if and only if , he is sure that he can get even number of dosas. So given N number of dosas can you please help Harish to decide, if he will be able to get all the dosas at once from the hotel. Input The first line of input contains an integer T which denotes the number of test files. Next T lines contains an integer N where N is the total number of dosas. Output Print "YES" if both can get even number of dosas. If it is not possible print "NO". Constraints 1 ≤ T ≤ 10^6 1 ≤ N ≤ 10^18 Example Input: 2 16 27 Output: YES NO
1
6
# python3 if __name__ == "__main__": t = int(raw_input()) l = [] i = 0 while(i < t and t <= 10**6): ch = int(raw_input()) if(ch <= 10**18): l.append(ch) i = i + 1 for i in l: if(i % 2 == 0): print "YES" else: print "NO"
PYTHON
reciibks
Shrija's brother Vaibhav is a very mischievous boy . He has messed a lot of books of Shrija on floor . Now Shrija is very angry . Seeing her sister angry he promises to arrange them on N empty stacks numbered from 1 to N . Shrija also felt that she should help her brother in arranging the books. So she gives Vaibhav a sequence of M instructions each of the form "A B", meaning that Vaibhav should add one new book to the top of each stack in the range A..B. For example, if Vaibhav is told "10 13", then he should add a book to each of the stacks 10, 11, 12, and 13. After Vaibhav finishes stacking books according to the instructions, Shrija would like to know the median height of the N stacks i.e. the height of the middle stack if the stacks were to be arranged in sorted order (Assume N is odd, so median is unique). Please help Shrija in finding the answer .   Input Line 1: Two space-separated integers, N M. Lines 2 to 1+M: Each line contains one of Shrija's instructions in the form of two space separated integers A B (1 <= A <= B <= N). Output Only 1 integer which is the required answer. Constraints 1 ≤ N ≤ 100000 1 ≤ M ≤ 10000   Example Input: 5 3 2 4 1 3 3 5 Output: 2
1
6
n,m=map(int,raw_input().split()) a=[0]*(n+2) #print a for i in range(m): left,right=map(int,raw_input().split()) a[left]+=1 a[right+1]-=1 for j in range(1,n+1): a[j]+=a[j-1] a=a[:n+1] a.sort() #print a print a[(n/2)+1]
PYTHON
tr004
Problem Statement Chef Diablo has set up a stall for selling his famous Modaks in the Trinity fest. He wants to earn as much profit as he can. So he has bought a chemical that increases the taste level of the Modaks. The tastier the Modaks the higher he can price them. This chemical is very costly hence he applies only certain amount of chemical on the Modaks(also he does not make it so costly that no one would buy it...:P). If Chef Diablo applies the chemical on the Modak having an initial of taste level TL, its taste level becomes TL^N where N is the number of drops of the chemical. Given TL and N, tell Chef Diablo the final Taste level of the Modak. Input: First line contains t, the number of test cases. Then t lines follow, each line containing 2 space separated integers denoting TL and N respectively. Output: It will consist of t lines having the final taste level of the Modak modulo 10^9+7. Constraints: 1<=t<=100 1<=TL<=10^101 0<=N<=10^101 Input: 3 1 1 2 2 123123123123 2 Output: 1 4 293883007
1
6
T=input() while T: T-=1 tl,n=[int(x) for x in raw_input().split()] if n==0: n=1 print pow(tl,n,pow(10,9)+7)
PYTHON
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
ch=input() l=list(map(int,ch.split())) m=l[0] n=l[1] p=l[2] s=input() t=input() l1=[] for i in range(p): ch=input() l1.append(ch) res=0 l2=[0] for i in range (len(t)-1): l2.append(0) for i in range ((len(t)-1),len(s)): if s[(i-len(t)+1):(i+1)]==t: res+=1 l2.append(res) for i in range(p): ch=l1[i] l=list(map(int,ch.split())) r=l[0] k=l[1] if (k-r+1)>=(len(t)): print(l2[k]-l2[(r-2+len(t))]) else: print(0)
PYTHON3
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
import java.util.*; import java.io.*; public class Q1{ public static void main(String[] args) throws Exception{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter w=new PrintWriter(System.out); int i,j,k1,k2,n,m,q,c=0,temp; long sum=0; String s1[]=br.readLine().split(" "); n=Integer.parseInt(s1[0]); m=Integer.parseInt(s1[1]); q=Integer.parseInt(s1[2]); String s=br.readLine(); String t=br.readLine(); HashMap<Integer,Integer>hm=new HashMap<>(); ArrayList<Integer>al=new ArrayList<>(); int dp[]=new int[s.length()+1]; for(i=0;i<=s.length()-t.length();i++){ if(s.substring(i,i+t.length()).equals(t)) hm.put(i+1,i+t.length()); } for(i=1;i<=s.length();i++){ if(hm.containsKey(i)){ dp[i]=dp[i-1]+1; } else dp[i]=dp[i-1]; } for(i=0;i<q;i++){ c=0; String ss[]=br.readLine().split(" "); k1=Integer.parseInt(ss[0]); k2=Integer.parseInt(ss[1]); if(k2-t.length()+1>=k1) c=dp[k2-t.length()+1]-dp[k1-1]; al.add(c); } for(Integer r:al){ System.out.println(r); } } }
JAVA
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#include <bits/stdc++.h> using namespace std; void qmax(int &x, int y) { if (x < y) x = y; } void qmin(long long &x, long long y) { if (x > y) x = y; } inline long long read() { char s; long long k = 0, base = 1; while ((s = getchar()) != '-' && s != EOF && !(isdigit(s))) ; if (s == EOF) exit(0); if (s == '-') base = -1, s = getchar(); while (isdigit(s)) { k = k * 10 + (s ^ '0'); s = getchar(); } return k * base; } inline void write(int x) { static char cnt, num[15]; cnt = 0; if (!x) { putchar('0'); return; } for (; x; x /= 10) num[++cnt] = x % 10; for (; cnt; putchar(num[cnt--] + 48)) ; } int n, m, q; char b[1010], a[1010]; int dp[1010][1010]; int Next[1010]; int main() { n = read(), m = read(), q = read(); scanf("%s", b + 1); scanf("%s", a + 1); int p; Next[1] = 0; p = 0; for (int i = 2; i <= m; i++) { while (p && a[p + 1] != a[i]) p = Next[p]; if (a[p + 1] == a[i]) { p++; Next[i] = p; } } p = 0; for (int i = 1; i <= n; i++) { while ((p == m) || (p && a[p + 1] != b[i])) p = Next[p]; if (a[p + 1] == b[i]) { p++; } if (p == m) { dp[i - m + 1][i] = 1; } } for (int l = m; l < n; l++) for (int i = 1, j = i + l; j <= n; i++, j++) { dp[i][j] = dp[i][j - 1]; if (j - m + 1 > 0) dp[i][j] = dp[i][j - 1] + dp[j - m + 1][j]; } int X, Y; while (q--) { X = read(); Y = read(); printf("%d\n", dp[X][Y]); } return 0; }
CPP
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#include <bits/stdc++.h> using namespace std; int N, M, Q; string S, T; int ok[1010]; void solve() { int i, j, k, l, r, x, y; string s; cin >> N >> M >> Q >> S >> T; for (i = 0; i < (N); i++) { ok[i + 1] = ok[i]; if (i + M <= N) { x = 1; for (j = 0; j < (M); j++) if (S[i + j] != T[j]) x = 0; ok[i + 1] += x; } } while (Q--) { cin >> x >> y; y -= M - 1; if (y < x) { cout << 0 << endl; } else { cout << ok[y] - ok[x - 1] << endl; } } } int main(int argc, char** argv) { string s; int i; if (argc == 1) ios::sync_with_stdio(false), cin.tie(0); for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s += '\n'; for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin); cout.tie(0); solve(); return 0; }
CPP
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#include <bits/stdc++.h> using namespace std; int main() { string s, t; int n, m, q, l, r; cin >> n >> m >> q; cin >> s >> t; int arr[10000] = {0}; for (int i = 0; i <= n - m; ++i) { arr[i + 1] = arr[i] + (s.substr(i, m) == t); } while (q--) { cin >> l >> r; if (r - l + 1 < m) cout << "0\n"; else cout << arr[r - m + 1] - arr[l - 1] << endl; } return 0; }
CPP
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
def subamount(l, r): if r - l < m - 1: return 0 elif m + l - 2 < 0: return pr[r] - 0 return pr[r] - pr[m + l - 2] n, m, q = map(int, input().split()) s = input() t = input() pr = [0] * n if m <= n: pr[m - 1] = int(t == s[:m]) for i in range(m, n): pr[i] = pr[i - 1] + (t == s[i - m + 1:i + 1]) for _ in range(q): l, r = map(int, input().split()) print(subamount(l - 1, r - 1))
PYTHON3
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author llamaoo7 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); BSegmentOccurrences solver = new BSegmentOccurrences(); solver.solve(1, in, out); out.close(); } static class BSegmentOccurrences { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); int q = in.nextInt(); char[] s = in.next().toCharArray(); char[] t = in.next().toCharArray(); boolean[] g = new boolean[n + 10]; for (int i = 0; i < s.length; i++) { if (s[i] == t[0]) { boolean good = true; for (int j = 1; j < t.length; j++) { if (i + j >= s.length || s[i + j] != t[j]) { good = false; break; } } if (good) { g[i] = true; } } } for (int i = 0; i < q; i++) { int l = in.nextInt() - 1; int r = in.nextInt() - 1; int c = 0; for (int j = l; j <= r - t.length + 1; j++) if (g[j]) c++; out.println(c); } } } 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()); } } }
JAVA
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
string = "" array = [] n, m, q = map(int, input().split()) s = input() t = input() for i in range(0, n - m + 1) : if s[ i : i + m] == t : string += '1' else : string += '0' for i in range(0, q) : a, b = map(int, input().split()) if b - a + 1 >= m : array.append((string[ a - 1: b - m + 1]).count('1')) else : array.append(0) for i in range(0, q) : print(array[i], end = '\n')
PYTHON3
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#list_int 並べて出力 print (' '.join(map(str,ans_li))) #list_str 並べて出力 print (' '.join(list)) # 2進数 format(10, 'b') # '1010' ''' 二次元配列を一列ずつ for i in ans: print(*i) ''' ''' heapq queue = [] heapq.heapify(queue) #heapqの作成 heapq.heappush(queue,num) #numのpush(値の追加) pop = heapq.heappop(queue) #numのpop(最小値の出力) pop = heapq.heappushpop(queue,num) #push -> pop ''' from collections import defaultdict import sys,heapq,bisect,math,itertools,string,queue,datetime sys.setrecursionlimit(10**8) INF = float('inf') mod = 10**9+7 eps = 10**-7 AtoZ = [chr(i) for i in range(65,65+26)] atoz = [chr(i) for i in range(97,97+26)] def inpl(): return list(map(int, input().split())) def inpl_str(): return list(input().split()) N,M,q = inpl() S = input() T = input() azu = [0]*(N-M+1) qq = [] for i in range(q): qq.append(inpl()) if M > N: for i in range(q): print(0) sys.exit() for i in range(0,N-M+1): if S[i:i+M] == T: azu[i] = 1 SUM = 0 nyan = [0] for az in azu: SUM += az nyan.append(SUM) for l,r in qq: l -= 1 if r - l < M: print(0) else: print(nyan[r-M+1] - nyan[l])
PYTHON3
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
import java.awt.List; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.math.BigDecimal; import java.math.BigInteger; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.InputMismatchException; import java.util.Locale; import java.util.TimeZone; public class C { public static void main(String[] args) { Locale.setDefault(Locale.US); InputStream inputstream = System.in; OutputStream outputstream = System.out; FastReader in = new FastReader(inputstream); PrintWriter out = new PrintWriter(outputstream); TaskA solver = new TaskA(); // int testcase = in.ni(); for (int i = 0; i < 1; i++) solver.solve(i, in, out); out.close(); } } class TaskA { public void solve(int testnumber, FastReader in, PrintWriter out) { int n = in.ni(); int m = in.ni(); int q = in.ni(); String s = in.ns(); String t = in.ns(); int occurence[] = new int[n+1]; for(int i = 0;i<=n-m;i++){ occurence[i+1] = occurence[i]; if(s.substring(i, i+m).equals(t)){ occurence[i+1]++; } } while(q >0){ int l = in.ni(); int r = in.ni(); if( r - l + 1 < m){ out.println(0); }else{ out.println(occurence[r-m+1] - occurence[l-1]); } q--; } } } class FastReader { public InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public FastReader() { } 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 ns() { 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 isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int[] iArr(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } public String next() { return ns(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } }
JAVA
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#include <bits/stdc++.h> using namespace std; int n, m, pre[2005], q; char s[2005], t[2005]; int main() { scanf("%d%d%d", &n, &m, &q); scanf("%s%s", s, t); for (int i = 0; i < n - m + 1; i++) { int flag = 1; for (int j = 0; j < m; j++) if (s[i + j] != t[j]) { flag = 0; break; } if (flag) pre[i + 1]++; } for (int i = 1; i <= n; i++) pre[i] += pre[i - 1]; while (q--) { int l, r; scanf("%d%d", &l, &r); printf("%d\n", pre[max(r - m + 1, l - 1)] - pre[l - 1]); } return 0; }
CPP
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
//package que_a; import java.io.*; import java.util.*; import java.math.*; public class utkarsh { InputStream is; PrintWriter out; long mod = (long) (1e9 + 7); boolean SHOW_TIME; void solve() { //Enter code here utkarsh //SHOW_TIME = true; int n = ni(), m = ni(), q = ni(); char s[] = ns(n); char t[] = ns(m); int lps[] = new int[m]; int l = 0; int i = 1; while(i < m) { if(t[i] == t[l]) { l++; lps[i] = l; i++; } else { if(l != 0) { l = lps[l-1]; } else { lps[i] = 0; i++; } } } //System.out.println("111"); while(q-- > 0) { int u = ni()-1, v = ni(); i = u; int j = 0, ans = 0; while(i < v) { if(j < m && t[j] == s[i]) { i++; j++; } if(j == m) ans++; if(j == m || (i < v && t[j] != s[i])) { if(j == 0) i++; else j = lps[j-1]; } } System.out.println(ans); } } //---------- I/O Template ---------- public static void main(String[] args) { new utkarsh().run(); } void run() { is = System.in; out = new PrintWriter(System.out); long start = System.currentTimeMillis(); solve(); long end = System.currentTimeMillis(); if(SHOW_TIME) out.println("\n" + (end - start) + " ms"); out.flush(); } byte input[] = new byte[1024]; int len = 0, ptr = 0; int readByte() { if(ptr >= len) { ptr = 0; try { len = is.read(input); } catch(IOException e) { throw new InputMismatchException(); } if(len <= 0) { return -1; } } return input[ptr++]; } boolean isSpaceChar(int c) { return !( c >= 33 && c <= 126 ); } int skip() { int b = readByte(); while(b != -1 && isSpaceChar(b)) { b = readByte(); } return b; } char nc() { return (char)skip(); } String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!isSpaceChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } String nLine() { int b = skip(); StringBuilder sb = new StringBuilder(); while( !(isSpaceChar(b) && b != ' ') ) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } int ni() { int n = 0, b = readByte(); boolean minus = false; while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); } if(b == '-') { minus = true; b = readByte(); } if(b == -1) { return -1; } //no input while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); } return minus ? -n : n; } long nl() { long n = 0L; int b = readByte(); boolean minus = false; while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); } if(b == '-') { minus = true; b = readByte(); } while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); } return minus ? -n : n; } double nd() { return Double.parseDouble(ns()); } float nf() { return Float.parseFloat(ns()); } int[] na(int n) { int a[] = new int[n]; for(int i = 0; i < n; i++) { a[i] = ni(); } return a; } char[] ns(int n) { char c[] = new char[n]; int i, b = skip(); for(i = 0; i < n; i++) { if(isSpaceChar(b)) { break; } c[i] = (char)b; b = readByte(); } return i == n ? c : Arrays.copyOf(c,i); } }
JAVA
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
import java.util.*; import java.io.*; public class B1016{ public static void main(String args[])throws IOException{ Scanner sc=new Scanner(new BufferedReader(new InputStreamReader(System.in))); PrintWriter pw=new PrintWriter(System.out); int n=sc.nextInt(); int m=sc.nextInt(); int q=sc.nextInt(); String s=sc.next(); String t=sc.next(); if(m>n){ for(int i=0;i<q;i++){ int l=sc.nextInt(); l=sc.nextInt(); pw.println(0); } } else{ int arr[]=new int[n-m+2]; arr[arr.length-1]=0; for(int i=n-m;i>=0;i--){ arr[i]=arr[i+1]; int k=0; if(s.charAt(i)==t.charAt(0)){ for(k=1;k<m;k++){ if(s.charAt(i+k)!=t.charAt(k)) break; } if(k==m) arr[i]+=1; } //System.out.print(arr[i]+" "+s.charAt(i)); } for(int i=0;i<q;i++){ int l=sc.nextInt()-1; int r=sc.nextInt()-1; if((r-l+1)<m){ pw.println(0); } else{ pw.println(arr[l]-arr[r-m+2]); } } } pw.close(); } }
JAVA
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { FastReader reader = new FastReader(); PrintWriter writer = new PrintWriter(System.out); //ifieverknew int n = reader.nextInt(); int m = reader.nextInt(); int q = reader.nextInt(); char[] s = reader.nextLine().toCharArray(); char[] t = reader.nextLine().toCharArray(); int[] count = new int[n]; for (int i=0; i+m-1<n; i++) { boolean check = true; for (int j=0; j<m; j++) { if (s[j+i] != t[j]) check = false; } if (check) count[i]++; } for (int i=1; i<n; i++) count[i] += count[i-1]; while (q > 0) { int l = reader.nextInt()-1; int r = reader.nextInt()-1; int ans = 0; if (r-m+1 >= 0) ans += count[r-m+1]; if (l-1 >= 0) ans -= count[l-1]; writer.println(Math.max(ans,0)); q--; } writer.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; } } }
JAVA
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#include <bits/stdc++.h> using namespace std; int n, m, Q, cnt = 0; int a[1010], L[1010]; char s[1010], t[1010]; int main() { scanf("%d%d%d", &n, &m, &Q); scanf("%s%s", s, t); for (int i = 0; i < n; i++) { if (s[i] == t[0]) { bool bo = 0; for (int j = 1; j < m; j++) { if (s[i + j] != t[j]) { bo = 1; break; } } if (!bo) { a[cnt] = i + 1; L[i + 1] = cnt; cnt++; } } } int x, y; while (Q--) { scanf("%d%d", &x, &y); int sum = 0; for (int i = 0; i < cnt; i++) { if (a[i] >= x && a[i] + m - 1 <= y) sum++; if (a[i] > y) break; } printf("%d\n", sum); } return 0; }
CPP
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, m, q; string s, t; cin >> n >> m >> q >> s >> t; map<long long, long long> d; long long cont = 0; for (long long i = (0); i < (long long)(n); i++) { if (s.substr(i, m) == t) { d[i] = ++cont; } } while (q--) { long long l, r; cin >> l >> r; l--; r--; auto it = d.lower_bound(l); auto jt = d.upper_bound(r - m + 1); if (jt == d.begin()) { puts("0"); continue; } jt--; long long x = jt->second - it->second + 1; if (x < 0) x = 0; printf("%lld\n", x); } }
CPP
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; public class Main { public static void main(String[] args) { new Main().run(); } private IO io = new IO(); private String text; private String pattern; private int[] pos = new int[10000]; private int numPos = 0; private void run() { // Trash io.readLine(); io.getInteger(); io.getInteger(); // int casos = io.getInteger(); text = io.readLine(); pattern = io.readLine(); int pl = pattern.length(); // Fill pos int i = 0; while (i >= 0) { i = text.indexOf(pattern, i); if (i >= 0) { pos[numPos++] = i++; } } // Rangos de búsqueda {l,r) for (int c = 0; c < casos; c++) { io.readLine(); int l = io.getInteger() - 1; int r = io.getInteger() - 1; int cont = 0; if (r - l + 1 >= pl) { for (i = 0; i < numPos; i++) { if ((pos[i] >= l) && (pos[i] + pl - 1 <= r)) { cont++; } else { if (cont > 0) { break; } } } } io.println(cont); } } public class IO { private final BufferedReader in; private final PrintWriter out; private String[] tokens; private int currentToken; public IO() { tokens = new String[0]; currentToken = 0; in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } public int getNumItems() { return tokens.length; } public String getString() { if (currentToken >= tokens.length) { return null; } return tokens[currentToken++]; } public int getInteger() { String s = getString(); int n; try { n = Integer.parseInt(s); } catch (NumberFormatException e) { n = 0; } return n; } public long getLong() { String s = getString(); long n; try { n = Long.parseLong(s); } catch (NumberFormatException e) { n = 0; } return n; } public double getDouble() { String s = getString(); double r; try { r = Double.parseDouble(s); } catch (NumberFormatException e) { r = 0.0; } return r; } public int[] getIntegerArray() { int l = getNumItems(); int[] v = new int[l]; for (int i = 0; i < l; i++) { v[i] = getInteger(); } return v; } public long[] getLongArray() { int l = getNumItems(); long[] v = new long[l]; for (int i = 0; i < l; i++) { v[i] = getLong(); } return v; } public void print(Object o) { out.print(o); out.flush(); } public void println(Object o) { print(o); out.println(); out.flush(); } public String readLine() { String s = ""; try { s = in.readLine(); tokens = s.split(" "); } catch (Exception e) { tokens = new String[0]; } currentToken = 0; return s; } } }
JAVA
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
def binary_search(array, target): lower = 0 upper = len(array) while lower < upper: # use < instead of <= x = lower + (upper - lower) // 2 val = array[x] if target == val: return x elif target > val: if lower == x: # these two are the actual lines break # you're looking for lower = x elif target < val: upper = x n,m,q=[int(i) for i in input().split()] s=str(input()) t=str(input()) z=[[int(i) for i in input().split()] for j in range(q)] a=[] for i in range(n-m+1): a+=[i+1] if s[i:i+m]==t: a.append(i+1) a+=[i+1 for i in range(n-m+1,n)] for i in range(q): l=z[i][0] r=z[i][1]-m+1 if l>r: print(0) continue c=binary_search(a,l) d=binary_search(a,r) try: if a[d+1]==a[d]: d+=1 except: pass try: if a[c-1]==a[c] and c>=1: c-=1 except: pass print(d-c-r+l)
PYTHON3
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
'''input 3 5 2 aaa baaab 1 3 1 1 ''' from bisect import bisect_left, bisect_right n, m, q = map(int, input().split()) s, t = input(), input() x = [] for i in range(len(s) - len(t) + 1): if s[i:i+len(t)] == t: x.append(i) sx = set(x) for _ in range(q): l, r = map(int, input().split()) if r - l + 1 < len(t): print(0) continue i = bisect_right(x, l - 1) j = bisect_left(x, r - len(t)) if l - 1 in sx: i -= 1 if r - len(t) not in sx: j -= 1 print(j - i + 1)
PYTHON3
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
def z_algorithm(s): n = len(s) Z = [0]*n Z[0] = n i = 1 j = 0 while i < n: while i+j < n and s[j] == s[i+j]: j += 1 Z[i] = j if j == 0: i += 1 continue k = 1 while i+k < n and k+Z[k] < j: Z[i+k] = Z[k] k += 1 i += k j -= k return Z class BIT: def __init__(self, n): self.n = n self.bit = [0]*(self.n+1) # 1-indexed def init(self, init_val): for i, v in enumerate(init_val): self.add(i, v) def add(self, i, x): # i: 0-indexed i += 1 # to 1-indexed while i <= self.n: self.bit[i] += x i += (i & -i) def sum(self, i, j): # return sum of [i, j) # i, j: 0-indexed return self._sum(j) - self._sum(i) def _sum(self, i): # return sum of [0, i) # i: 0-indexed res = 0 while i > 0: res += self.bit[i] i -= i & (-i) return res def lower_bound(self, x): s = 0 pos = 0 depth = self.n.bit_length() v = 1 << depth for i in range(depth, -1, -1): k = pos + v if k <= self.n and s + self.bit[k] < x: s += self.bit[k] pos += v v >>= 1 return pos def __str__(self): # for debug arr = [self.sum(i,i+1) for i in range(self.n)] return str(arr) import sys import io, os input = sys.stdin.readline n, m, q = map(int, input().split()) s = str(input().rstrip()) t = str(input().rstrip()) S = t+'#'+s Z = z_algorithm(S) #print(Z) Z = Z[m+1:] LR = [] for i in range(q): l, r = map(int, input().split()) l, r = l-1, r-1 LR.append((l, r, 1, i)) for i in range(n): if Z[i] == m: LR.append((i, i+m-1, 0, 0)) LR.sort(key=lambda x: (x[1], x[2])) bit = BIT(10**3+50) ans = [0]*q for l, r, t, i in LR: if t == 0: bit.add(l, 1) else: ans[i] = bit.sum(l, r+1) print(*ans, sep='\n')
PYTHON3
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
def main(): [n,m,q] = list(map(int, input().split(" "))) s = input() t = input() queries = [] for _ in range(q): queries.append(list(map(int, input().split(" ")))) #toss the obvious if len(s) < len(t): for _ in range(q): print(0) else: ref = [0] for i in range(len(s)-len(t)+1): j = i #print(j) if s[j:j+len(t)] == t: ref.append(ref[-1]+1) else: ref.append(ref[-1]) #print(ref) for x in queries: if x[1]-len(t)+1 < x[0]-1: print(0) else: print(ref[x[1]-len(t)+1]-ref[x[0]-1]) main()
PYTHON3
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#coding:utf-8 tmp = raw_input() n = int(tmp.split()[0]) m = int(tmp.split()[1]) q = int(tmp.split()[2]) s = raw_input() t = raw_input() st = [0]*n for i in range(n-m+1): if t == s[i:i+m]: st[i] += 1 data = [] res = [] for i in range(q): da = [int(i) for i in raw_input().split()] # data.append(da) # print(st) # for da in data: L = da[0] R = da[1] cnt = 0 # for j in range(L-1,R): # if st[j] == 1 and j+m <= R: # cnt += 1 # print(st[L-1:R],L-1,R) # print(st[R-1]) if L-1+m <= R: cnt = st[L-1:R-m+1].count(1) res.append(str(cnt)) print("\n".join(res))
PYTHON
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#include <bits/stdc++.h> using namespace std; int fx[] = {-1, 1, 0, 0}; int fy[] = {0, 0, -1, 1}; int dx[] = {1, 1, 0, -1, -1, -1, 0, 1}; int dy[] = {0, 1, 1, 1, 0, -1, -1, -1}; int kx[] = {1, 1, -1, -1, 2, 2, -2, -2}; int ky[] = {2, -2, 2, -2, 1, -1, 1, -1}; bool cmp(const pair<int, int> &a, const pair<int, int> &b) { return a.first < b.first; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, q; cin >> n >> m >> q; string s, t; cin >> s >> t; s = '?' + s; int ara[1010], sum[1010]; memset(ara, 0, sizeof(ara)), memset(sum, 0, sizeof(sum)); for (int i = 1; i <= n - m + 1; i++) { ara[i] = 1; for (int j = 0; j < t.size(); j++) { if (s[i + j] != t[j]) { ara[i] = 0; break; } } sum[i] = sum[i - 1] + ara[i]; } for (int i = 1; i <= q; i++) { int l, r; cin >> l >> r; if (r - l + 1 < m) { cout << "0\n"; } else { cout << sum[r - m + 1] - sum[l - 1] << "\n"; } } return 0; }
CPP
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
import java.io.BufferedInputStream; import java.util.ArrayList; import java.util.Deque; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Scanner; import java.util.Set; import java.util.Stack; /** * */ public class SegmentOccurrences { public static void main(String[] args) { Scanner in = new Scanner(new BufferedInputStream(System.in)); while (in.hasNext()) { int n = in.nextInt(); int m = in.nextInt(); int q = in.nextInt(); in.nextLine(); String s = in.nextLine(); String t = in.nextLine(); int[] ans = new int[n + 1]; int sum = 0; if (n >= m) { for (int i = 0; i < n - m + 1; i++) { boolean ok = true; for (int j = 0; j < m; j++) { if (s.charAt(i + j) == t.charAt(j)) { continue; } ok = false; } if (ok) { sum++; } ans[i + 1] = sum; } } for (int i = 0; i < q; i++) { int l = in.nextInt(); int r = in.nextInt(); if (r - l + 1 < m) { System.out.println(0); continue; } System.out.println(ans[r - m + 1] - ans[l - 1]); } } } }
JAVA
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
from sys import stdin, stdout n,m,q = stdin.readline().split() s = stdin.readline().rstrip() sub = stdin.readline().rstrip() def countSubtrings(s,sub): resp = [0]*len(s) sum = 1 for x in range(len(s)-len(sub)+1): if len(sub) == 1 and s[x] == sub: resp[x] = 1 continue if s[x] == sub[0] and s[x:x+len(sub)] == sub: sum += 1 resp[x+len(sub)-1] = 1 return resp def suma(listToSum): sum = 0 for x in listToSum: sum+=x return sum precalc = countSubtrings(s, sub) #print(precalc) resTotal = '' cache = {} for x in range(int(q)): l,r = stdin.readline().split() if l+'-'+r in cache: resTotal+= cache[l+'-'+r] continue if int(r) - int(l) + 2 <= len(sub): resTotal += '0\n' cache[l+'-'+r] = '0\n' else: calculated = str(suma(precalc[int(l)-1 + len(sub) - 1: int(r)]))+'\n' resTotal += calculated cache[l+'-'+r] = calculated stdout.write(resTotal)
PYTHON
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
n, m, q = map(int, input().split()) s = input() t = input() k = [0] * (n + 1) cntr = 0 for i in range(1, n+1): k[i] = k[i-1] if s[i - 1:i + m - 1] == t: k[i] += 1 for i in range(q): l, r = map(int, input().split()) print(k[max(l-1, r-m+1)]-k[l-1])
PYTHON3
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; int main() { int n, m, q; cin >> n >> m >> q; string a, b; cin >> a >> b; int mark[1000]; memset(mark, 0, sizeof(mark)); for (int i = 0; i < n; i++) { if (a[i] == b[0]) { int flag = 1, j; for (j = 1; j < m && i + j < n; j++) { if (a[i + j] != b[j]) { flag = 0; break; } } if (flag && j == m) mark[i] = 1; } } while (q--) { int l, r; scanf("%d%d", &l, &r); int cnt = 0; for (int i = l - 1; i <= r - 1; i++) { if (mark[i] == 1 && i + m - 1 <= r - 1) cnt++; } cout << cnt << endl; } return 0; }
CPP
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
while True: try: def failur(p, m): failr = [0]*(m+2) failr[0]= failr[1] = 0 i = 2 while i <= m: j = failr[i-1] while True: if p[i-1] == p[j]: failr[i] = j+1 break if j == 0: failr[i] = j break j = failr[j] i += 1 return failr def matches(t, p, f): n = len(t) m = len(p) c = [0]*(n+2) ti = pi = 0 while True: if ti == n: break if t[ti] == p[pi]: pi += 1 ti += 1 if pi == m: pi = f[m] c[(ti-m)] += 1 else: if pi == 0: ti += 1 else: pi = f[pi] return c def solution(n, m, q): qury = [] test = input() patt = input() for i in range(q): l, r = map(int, input().split()) l -= 1;r -= 1 qury.append([l, r]) f = failur(patt, m) matc = matches(test, patt, f) for i in range(q): a, b= qury[i][0], qury[i][1] rng = (b -m) +1 cunt = 0 for j in range(a, rng+1, 1): #print(j, " ", i) if matc[j] > 0: cunt += 1 print(cunt) #print(matc) def read(): n, m, q = map(int, input().split()) solution(n, m, q) if __name__ == "__main__": read() except EOFError: break
PYTHON3
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
import java.io.BufferedReader; import java.io.IOException; import java.util.*; import java.io.BufferedWriter; import java.io.InputStream; import java.io.OutputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Writer; public class B1016 { static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(int[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) writer.print(' '); writer.print(array[i]); } } public void printLine(int[] array) { print(array); writer.println(); } public void close() { writer.close(); } } static class Pair { int value; int index; Pair(int value, int index) { this.value = value; this.index = index; } } static class MyComparator implements Comparator<Pair> { @Override public int compare(Pair p1, Pair p2) { if (p1.value != p2.value) return p1.value - p2.value; return p1.index - p2.index; } } public static void main(String[] args) { OutputStream outputStream = System.out; OutputWriter out = new OutputWriter(outputStream); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); Scanner sc= new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int q =sc.nextInt(); sc.nextLine(); String a = sc.nextLine(); String b = sc.nextLine(); int index = a.indexOf(b); int[] indexes = new int[n]; while(index >= 0) { //System.out.println(index+m-1); indexes[index+m-1] = 1; index = a.indexOf(b, index+1); } //out.printLine(indexes); for (int i=1; i<indexes.length; i++) { indexes[i] += indexes[i-1]; } //out.printLine(indexes); for (int i=0; i<q; i++) { int x = sc.nextInt(); int y = sc.nextInt(); if (y-x+1 < b.length()) System.out.println(0); else if (x == 1) System.out.println(indexes[y-1]); else if (x == y) { if (b.length()==1 && a.charAt(x-1) == b.charAt(0)) System.out.println(1); else System.out.println(0); } else System.out.println(indexes[y-1]-indexes[x-2+m-1]); } //int n = in.readInt(); //int[] temparr = IOUtils.readIntArray(in, n); //int[] rating = new int[n]; //out.printLine(indexes); out.close(); } }
JAVA
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
import sys n,m,t=map(int,input().split()) s1=list(input()) s2=list(input()) d={} for i in range(n): inc=0 j=0 while i+inc<n and j<m and s1[i+inc]==s2[j]: inc+=1 j+=1 if inc==m: d[i+m]=d.get(i+m,0)+1 #print(d) #d[0]=-1 s=0 for i in range(0,1001): d[i]=d.get(i,0)+s s=d[i] #print(d) for _ in range(t): x,y=map(int,sys.stdin.readline().split()) if (y-x+1)<m: print(0) else: ans=d[y]-d[x-2+m] print(ans)
PYTHON3
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
import java.util.*; import java.io.*; public class A { public static void main(String[] args) { FastScanner fs=new FastScanner(); int n=fs.nextInt(); int m=fs.nextInt(); int q=fs.nextInt(); char[] first=fs.next().toCharArray(), second=fs.next().toCharArray(); if (m>n) { for (int i=0; i<q; i++) System.out.println(0); return; } boolean[] matches=new boolean[n-m+1]; for (int i=0; i<n-m+1; i++) { matches[i]=true; for (int j=0; j<m; j++) if (first[i+j]!=second[j]) matches[i]=false; } int[] cs=new int[matches.length]; cs[0]=matches[0]?1:0; for (int i=1; i<cs.length; i++) cs[i]=cs[i-1]+(matches[i]?1:0); for (int i=0; i<q; i++) { int from=fs.nextInt()-1, to=fs.nextInt()-1-m+1; if (to<from) { System.out.println(0); } else { System.out.println(from==0?cs[to]:(cs[to]-cs[from-1])); } } } private static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readLongArray(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } }
JAVA
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
import java.io.*; import java.util.*; public class B implements Runnable{ public static void main (String[] args) {new Thread(null, new B(), "_cf", 1 << 28).start();} public void run() { FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); System.err.println("Go!"); int n = fs.nextInt(); int m = fs.nextInt(); int q = fs.nextInt(); int[] pref = new int[n]; char[] s = fs.next().toCharArray(); char[] t = fs.next().toCharArray(); for(int i = 0; i < n; i++) { boolean match = true, yup = false; for(int j = 0; j < m && i+j < n; j++) { match &= s[i+j] == t[j]; if(j == m-1) { yup = true; } } if(match && yup) { pref[i] = 1; } } for(int i = 0; i < n; i++) { if(i > 0) pref[i] += pref[i-1]; } while(q-->0) { int l = fs.nextInt()-1, r = fs.nextInt()-1; r -= m-1; if(r < l) { out.println(0); } else { out.println(query(pref, l, r)); } } out.close(); } int query(int[] a, int l, int r) { int sum = a[r]; if(l > 0) sum -= a[l-1]; return sum; } void sort (int[] a) { int n = a.length; for(int i = 0; i < 50; i++) { Random r = new Random(); int x = r.nextInt(n), y = r.nextInt(n); int temp = a[x]; a[x] = a[y]; a[y] = temp; } Arrays.sort(a); } class FastScanner { public int BS = 1<<16; public char NC = (char)0; byte[] buf = new byte[BS]; int bId = 0, size = 0; char c = NC; double num = 1; BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) throws FileNotFoundException { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } public char nextChar(){ while(bId==size) { try { size = in.read(buf); }catch(Exception e) { return NC; } if(size==-1)return NC; bId=0; } return (char)buf[bId++]; } public int nextInt() { return (int)nextLong(); } public long nextLong() { num=1; boolean neg = false; if(c==NC)c=nextChar(); for(;(c<'0' || c>'9'); c = nextChar()) { if(c=='-')neg=true; } long res = 0; for(; c>='0' && c <='9'; c=nextChar()) { res = (res<<3)+(res<<1)+c-'0'; num*=10; } return neg?-res:res; } public double nextDouble() { double cur = nextLong(); return c!='.' ? cur:cur+nextLong()/num; } public String next() { StringBuilder res = new StringBuilder(); while(c<=32)c=nextChar(); while(c>32) { res.append(c); c=nextChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while(c<=32)c=nextChar(); while(c!='\n') { res.append(c); c=nextChar(); } return res.toString(); } public boolean hasNext() { if(c>32)return true; while(true) { c=nextChar(); if(c==NC)return false; else if(c>32)return true; } } public int[] nextIntArray(int n) { int[] res = new int[n]; for(int i = 0; i < n; i++) res[i] = nextInt(); return res; } } }
JAVA
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
import sys def get_array(): return list(map(int, sys.stdin.readline().split())) def get_ints(): return map(int, sys.stdin.readline().split()) def input(): return sys.stdin.readline().strip('\n') n , m , q = get_ints() s = input() p = input() ans = [] i = 0 count = 0 while True: si = s.find(p,i) if si != -1: for _ in range(si-i+1): ans.append(count) count += 1 i = si+1 else: ans += [count]*(n-i+2) break out = [] for i in range(q): a , b = get_ints() a -= 1 b -= m-1 out.append(str(ans[b]-ans[a] if b >= a else 0 )) print('\n'.join(out))
PYTHON3
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.List; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; /** * Built using CHelper plug-in Actual solution is at the top * * @author MaxHeap */ 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.nextInt(); int m = in.nextInt(); int q = in.nextInt(); String s = in.next(); String pat = in.next(); List<Integer> ind = StringUtils.getIndexes(s, pat); int[] val = new int[ind.size()]; int[] acc = new int[10000]; for (int i = 0; i < val.length; i++) { val[i] = ind.get(i); acc[val[i] + 1]++; // acc[val[i] + 1 + pat.length()]--; } for (int i = 1; i < 10000; i++) { acc[i] += acc[i - 1]; } while (q-- > 0) { int a = in.nextInt(); int b = in.nextInt() - m + 1; if (a > b) { out.println(0); continue; } out.println(acc[b] - acc[a - 1]); } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1 << 13]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (this.numChars == -1) { throw new UnknownError(); } else { if (this.curChar >= this.numChars) { this.curChar = 0; try { this.numChars = this.stream.read(this.buf); } catch (IOException ex) { throw new InputMismatchException(); } if (this.numChars <= 0) { return -1; } } return this.buf[this.curChar++]; } } public int nextInt() { int c; for (c = this.read(); isSpaceChar(c); c = this.read()) { } byte sgn = 1; if (c == 45) { sgn = -1; c = this.read(); } int res = 0; while (c >= 48 && c <= 57) { res *= 10; res += c - 48; c = this.read(); if (isSpaceChar(c)) { return res * sgn; } } throw new InputMismatchException(); } public String next() { int c; while (isSpaceChar(c = this.read())) { } StringBuilder result = new StringBuilder(); result.appendCodePoint(c); while (!isSpaceChar(c = this.read())) { result.appendCodePoint(c); } return result.toString(); } public static boolean isSpaceChar(int c) { return c == 32 || c == 10 || c == 13 || c == 9 || c == -1; } } static class StringUtils { public static int[] prefixFunction(char[] s) { int[] p = new int[s.length]; int k = 0; for (int i = 1; i < s.length; i++) { while (k > 0 && s[k] != s[i]) { k = p[k - 1]; } if (s[i] == s[k]) { ++k; } p[i] = k; } return p; } public static List<Integer> getIndexes(String text, String pat) { List<Integer> arr = new ArrayList<>(); int[] p = prefixFunction(pat); int k = 0; for (int i = 0; i < text.length(); i++) { while (k > 0 && text.charAt(i) != pat.charAt(k)) { k = p[k - 1]; } if (text.charAt(i) == pat.charAt(k)) { ++k; } if (k == pat.length()) { arr.add((i + 1 - pat.length())); k = p[k - 1]; } } return arr; } public static int[] prefixFunction(String s) { return prefixFunction(s.toCharArray()); } } }
JAVA
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
import sys n,m,q=map(int,input().split()) s=input() t=input() A=[None]*q for i in range(q): A[i]=list(map(int,input().split())) if m>n: for i in range(q): print(0) sys.exit() score=[0 for i in range(n-m+1)] for i in range(n-m+1): if t==s[i:i+m]: score[i]=1 SUM=[0 for i in range(n-m+1)] SUM[0]=score[0] for i in range(1,n-m+1): SUM[i]=score[i]+SUM[i-1] SUM=[0]+SUM for i in range(q): if A[i][1]-m+1<=A[i][0]-1: print(0) else: print(SUM[A[i][1]-m+1]-SUM[A[i][0]-1])
PYTHON3
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#include <bits/stdc++.h> using namespace std; const int maxn = 2e3 + 5; char a[maxn], b[maxn]; int sum[maxn]; int n, m, q; void solve() { for (int i = 0; i < n; i++) { int flag = 1; for (int j = 0; j < m; j++) if (a[i + j] != b[j]) { flag = 0; break; } if (flag) sum[i + 1]++; sum[i + 1] += sum[i]; } } int main() { scanf("%d %d %d", &n, &m, &q); scanf(" %s %s", a, b); solve(); while (q--) { int l, r; scanf("%d %d", &l, &r); if (r - m + 1 >= 0 && r - m + 1 >= l - 1) printf("%d\n", sum[r - m + 1] - sum[l - 1]); else puts("0"); } return 0; }
CPP
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public final class SegmentOccurencesCF { public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String ss[]=br.readLine().split(" "); int n=Integer.parseInt(ss[0]); int m=Integer.parseInt(ss[1]); int q=Integer.parseInt(ss[2]); String s=br.readLine(); String t=br.readLine(); boolean isStarting[]=new boolean[n]; int pre[]=new int[n+1]; pre[0]=0; for(int i=0;i<(n-m+1);i++){ boolean isTrue=true; for(int j=0;j<m;j++){ if(s.charAt(i+j)!=t.charAt(j)){ isTrue=false; break; } } isStarting[i]=isTrue; pre[i+1]=pre[i]; if(isStarting[i]) pre[i+1]++; } for(int i = Math.max((n - m + 1), 0); i<n; i++){ pre[i+1]=pre[i]; } while (q-->0){ String st[]=br.readLine().split(" "); int l=Integer.parseInt(st[0]); int r=Integer.parseInt(st[1]); l--; r=r-(m-1); if(r>=l){ System.out.println(pre[r]-pre[l]); }else System.out.println("0"); } } }
JAVA
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#include <bits/stdc++.h> using namespace std; int n, m, q; int ans[1005]; int main() { while (scanf("%d%d%d", &n, &m, &q) != EOF) { string s1, s2; cin >> s1 >> s2; for (int i = 0; i + m <= n; i++) { if (s1.substr(i, m) == s2) ans[i + 1]++; } for (int i = 2; i <= n; i++) ans[i] += ans[i - 1]; while (q--) { int li, ri; scanf("%d%d", &li, &ri); ri = ri - m + 1; printf("%d\n", max(0, ans[max(0, ri)] - ans[li - 1])); } } }
CPP
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int q = sc.nextInt(); List<Integer> arr = new ArrayList<Integer>(); String str = sc.next(); String cpy = sc.next(); for(int i=0; i<=(n-m); i++) { if(str.substring(i, i+m).equals(cpy)) { arr.add(i+1); arr.add(i+m); } } // System.out.println(arr); int len = arr.size(); while(q-- > 0) { int st = sc.nextInt(); int end = sc.nextInt(); if(len == 0 || (end-st)+1 < m) System.out.println(0); else { if(st < arr.get(0)) st = arr.get(0); int c=0; for(int i=0; i<len; i+=2) { if(arr.get(i) >= st && arr.get(i+1) <= end) c += 1; } System.out.println(c); } } } }
JAVA
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author bacali */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(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, FastScanner in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); int q = in.nextInt(); String s = in.next(); String t = in.next(); int[][] ind = new int[n][2]; int pos = -1; while ((pos = s.indexOf(t, pos + 1)) != -1) { ind[pos][0]++; ind[pos][1] = pos; } for (int i = 1; i < n; i++) { ind[i][0] += ind[i - 1][0]; if (ind[i][1] == 0) { ind[i][1] = ind[i - 1][1]; } } for (int i = 0; i < q; i++) { int l = in.nextInt() - 1; int r = in.nextInt() - m; if (r < l) { out.println(0); continue; } int ans = ind[r][0] - ((l == 0) ? 0 : ind[l - 1][0]); out.println(ans); } } } static class FastScanner { private BufferedReader br; private StringTokenizer st; public FastScanner(InputStream inputStream) { br = new BufferedReader(new InputStreamReader(inputStream)); } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
JAVA
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
n, m, q = map(int, input().split()) s = input() t = input() arr = [0]*1008 arr2 = [0]*1008 for i in range(n - m + 1): fl = 1 for j in range(m): if s[i+j] != t[j]: fl = 0 arr2[i] = fl arr[i+1] = arr[i] + arr2[i] for i in range(max(0, n - m + 1), n): arr[i+1] = arr[i] for i in range(q): l, r = map(int, input().split()) l -= 1 r -= m - 1 if r >= l: print(arr[r] - arr[l]) else: print(0)
PYTHON3
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#include <bits/stdc++.h> using namespace std; char s[1002], t[1002]; int v[1002]; int main() { int n, m, q, l, r, sol, i, ok, j; cin >> n >> m >> q; cin.get(); cin.getline(s, 1001); cin.getline(t, 1001); for (i = 0; i < n - m + 1; i++) { ok = 1; for (j = i; j <= i + m - 1; j++) if (t[j - i] != s[j]) { ok = 0; break; } if (ok == 1) v[i + m - 1] += 1; } for (i = 1; i < n; i++) v[i] += v[i - 1]; for (i = 1; i <= q; i++) { cin >> l >> r; l--; r--; l += m - 1; if (l > r) cout << 0 << '\n'; else { sol = v[r] - v[l - 1]; cout << sol << '\n'; } } return 0; }
CPP
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 7; const long long INF = 1LL << 60; const long long mod = 1e9 + 7; const long double eps = 1e-8; const long double pi = acos(-1.0); template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } vector<int> Zalgo(const string& s) { int n = (int)s.size(); vector<int> res(n); res[0] = n; int i = 1, j = 0; while (i < n) { while (i + j < n && s[j] == s[i + j]) ++j; res[i] = j; if (j == 0) { ++i; continue; } int k = 1; while (i + k < n && k + res[k] < j) res[i + k] = res[k], ++k; i += k, j -= k; } return res; } void solve() { int n, m, q; cin >> n >> m >> q; string s, t; cin >> s >> t; string u = t + s; auto res = Zalgo(u); vector<int> cnt(n + 1, 0); for (int i = 0; i < n; i++) { cnt[i + 1] = cnt[i] + (res[m + i] >= m ? 1 : 0); } for (int _ = 0; _ < q; _++) { int l, r; cin >> l >> r; cout << cnt[max(r - m + 1, l - 1)] - cnt[l - 1] << endl; } } signed main() { ios::sync_with_stdio(false); cin.tie(0); solve(); return 0; }
CPP
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#include <bits/stdc++.h> using namespace std; void z_function(string &s, vector<int> &z) { int n = s.length(), l = -1, r = -1; z.clear(); z.resize(n, 0); z[0] = n; for (int i = 0; i < n; i++) { if (i <= r) z[i] = min(r - i + 1, z[i - l]); while (z[i] + i < n && s[z[i]] == s[z[i] + i]) z[i]++; if (z[i] + i - 1 > r) { l = i; r = i + z[i] - 1; } } } int n, m, q, l, r; string s, t; vector<int> z, pr; int main() { cin >> n >> m >> q; cin >> s >> t; n = s.length(); m = t.length(); s = t + '#' + s; z_function(s, z); pr.resize(n, 0); if (z[m + 1] == m) pr[0] = 1; for (int i = 1; i < n; i++) { pr[i] = pr[i - 1]; if (z[m + 1 + i] == m) pr[i]++; } for (int i = 0; i < q; i++) { cin >> l >> r; l--; r--; if (r - m + 1 < 0) { cout << 0 << endl; continue; } if (l == 0) cout << pr[r - m + 1]; else cout << max(pr[r - m + 1] - pr[l - 1], 0); cout << endl; } return 0; }
CPP
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#!/usr/bin/python3 def calc(l, r, layers, li): if r - l == 1: return layers[li][l] xl = l if l % 2 == 1: xl -= 1 xr = r if r % 2 == 1: xr += 1 ans = calc(xl // 2, xr // 2, layers, li + 1) if xl != l: ans -= layers[li][xl] if xr != r: ans -= layers[li][xr - 1] return ans def solve(N, M, Q, S, T, A): base = [0] * N i = 0 while True: try: i = S.index(T, i) base[i] = 1 i += 1 except ValueError: break layers = [base] while len(layers[-1]) > 1: prev = layers[-1] if len(prev) % 2 == 1: prev.append(0) layer = [] for i in range(len(prev) // 2): layer.append(prev[2 * i] + prev[2 * i + 1]) layers.append(layer) ans = [] for (l, r) in A: l -= 1 r -= M - 1 if r <= l: ans.append(0) continue ans.append(calc(l, r, layers, 0)) return ans def main(): N, M, Q = [int(e) for e in input().split(' ')] S = input() T = input() A = [map(int, input().split(' ')) for _ in range(Q)] assert len(S) == N assert len(T) == M print(*solve(N, M, Q, S, T, A), sep='\n') if __name__ == '__main__': main()
PYTHON3
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
import java.util.*; import java.io.*; import java.math.*; public class loser { static class InputReader { public BufferedReader br; public StringTokenizer token; public InputReader(InputStream stream) { br=new BufferedReader(new InputStreamReader(stream),32768); token=null; } public String next() { while(token==null || !token.hasMoreTokens()) { try { token=new StringTokenizer(br.readLine()); } catch(IOException e) { throw new RuntimeException(e); } } return token.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } static class card{ String s; int l; public card(String s,int i) { this.s=s; this.l=i; } } static class sort implements Comparator<card> { public int compare(card o1,card o2) { if(o1.l!=o2.l) return (o1.l-o2.l); else return o1.s.compareTo(o2.s); } } static void shuffle(int a[]) { List<Integer> l=new ArrayList<>(); for(int i=0;i<a.length;i++) l.add(a[i]); Collections.shuffle(l); for(int i=0;i<a.length;i++) a[i]=l.get(i); } /*static long gcd(long a,long b) { if(b==0) return a; else return gcd(b,a%b); } static boolean valid(int i,int j,int r,int c) { if(i<r && i>=0 && j<c && j>=0) return true; else return false; }*/ static class Pair { int a;int b; public Pair(int a,int b) { this.a =a; this.b =b; } } static boolean beauty(int n) { String s1=Integer.toBinaryString(n); int f=1,o=0,z=0;boolean res=true; for(int i=0;i<s1.length();i++) { if(f==1 && s1.charAt(i)=='1') o++; else if(s1.charAt(i)=='0' && f==1) { z++; f=0; } else if(s1.charAt(i)=='0' && f==0) z++; else if(f==0 && s1.charAt(i)=='1') { res=false; break; } } if((o-1)!=z) res=false; return res; } public static void main(String[] args) { InputReader sc=new InputReader(System.in); int n=sc.nextInt(); int m=sc.nextInt(); int q=sc.nextInt(); String s=sc.next(); String t=sc.next(); if(m>n) { for(int i=0;i<q;i++) { int l=sc.nextInt(); int r=sc.nextInt(); System.out.println(0); } } else { int b[]=new int[n]; for(int i=0;i<=n-m;i++) { if( s.substring(i,i+m).equals(t)) { b[i]=1; } } for(int i=0;i<q;i++) { int l=sc.nextInt(); int r=sc.nextInt(); if((r-l+1)>=m) { int ans=0; for(int j=l-1;j<=r-m;j++) { if(b[j]==1) ans++; } System.out.println(ans); } else System.out.println(0); } } } }
JAVA
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
R=lambda:map(int,input().split()) n,m,q=R() s,t=input(),input() a=[0,0] b=0 for i in range(n):b+=s[i:i+m]==t;a+=[b] for _ in[0]*q:l,r=R();print(a[max(l,r-m+2)]-a[l])
PYTHON3
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
a,b,c=map(int,input().split()) s1=input();s2=input() coun=[0 for i in range(a+5)] if a>=b: for i in range(a-b+1): coun[i+1]=coun[i]+(s1[i:i+b] == s2) for i in range(a-b+1,a+5): if coun[i]==0: coun[i]=coun[i-1] for i in range(c): x,y=map(int,input().split()) if (y-x+1<b): print("0") else: print(coun[y-b+1]-coun[x-1]) else: print("0\n"*c)
PYTHON3
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
//import java.lang.*; import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); // length of string s int m = scanner.nextInt(); // length of string t int q = scanner.nextInt(); // number of queries String s = scanner.next(); String t = scanner.next(); int [] ans = new int [10000]; String str = ""; for (int j = 0; n - j >= m; ++j) { str = ""; for (int j1 = j, k = 0; k < m; ++k, ++j1) { str += s.charAt(j1); } if (t.equals(str)) ans[j] = 1; else ans[j] = 0; } for (int i = 0; i < q; ++i) { int cnt = 0; int a = scanner.nextInt(); int b = scanner.nextInt(); a--; b--; for (int j = a; b - j + 1 >= m; ++j) { cnt += ans[j]; } System.out.println(cnt); } scanner.close(); } // end main }
JAVA
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#E48_B ln = [int(i) for i in input().split(" ")] n = ln[0] m = ln[1] q = ln[2] s = input() t = input() def runAns(s, t): ss = 0 sso = [0] * (len(t) - 1) for i in range(0, n - len(t) + 1): f = True for j in range(0, n - i): if j >= len(t): break if s[i + j] != t[j]: f = False break if f: ss += 1 sso.append(ss) return [ss, sso] o = runAns(s, t) ss = o[0] o1 = o[1] o = runAns("".join(list(s)[::-1]), "".join(list(t)[::-1])) o2 = o[1][::-1] for i in range(0, q): ln = [int(j) for j in input().split(" ")] n1 = ln[0] n2 = ln[1] print(max(0, o2[n1 - 1] + o1[n2 - 1] - ss))
PYTHON3
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#include <bits/stdc++.h> using namespace std; const int MX = 1e3; int sum[MX + 2]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, m, q, l, r; string str, str2; cin >> n >> m >> q; cin >> str >> str2; for (long long i = 0; i < n - m + 1; i++) { sum[i] = (str.substr(i, m) == str2); } while (q-- > 0) { cin >> l >> r; l--, r--; r = r - m + 1; int ans = 0; for (long long i = l; i <= r; i++) ans += sum[i]; cout << ans << '\n'; } }
CPP
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#include <bits/stdc++.h> using namespace std; vector<int> vet; int p[201010]; void kmp(string &s, int sz) { p[0] = 0; int idx = 0, n = (int)s.size(); for (int i = 1; i < n; i++) { while (s[idx] != s[i] && idx > 0) idx = p[idx - 1]; if (s[idx] == s[i]) idx++; p[i] = idx; if (p[i] == sz) { vet.push_back(i - sz - sz + 1); } } } int solve(int l, int r) { if (l > r) return 0; int a = lower_bound(vet.begin(), vet.end(), l) - vet.begin(); int b = upper_bound(vet.begin(), vet.end(), r) - vet.begin(); return b - a; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m, q, l, r; string s, t; cin >> n >> m >> q; cin >> s >> t; string aux = t + "$" + s; kmp(aux, (int)t.size()); sort(vet.begin(), vet.end()); while (q--) { cin >> l >> r; cout << solve(l, r - (int)t.size() + 1) << "\n"; } return 0; }
CPP
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
import sys FILE_IO = False if FILE_IO: input_stream = open('input_test.txt') sys.stdout = open('current_output.txt', 'w') else: input_stream = sys.stdin n, m, q = map(int, input_stream.readline().split()) s = input_stream.readline().split()[0] t = input_stream.readline().split()[0] dp = [[0] * (n + 1) for i in range(m + 1)] for i in range(m + 1): for j in range(n + 1): if i == 0 or j == 0: dp[i][j] = 0 elif t[i - 1] == s[j - 1]: dp[i][j] = dp[i - 1][j - 1] + 1 else: dp[i][j] = 0 sum_list = [] current_sum = 0 for elem in dp[m]: if elem == m: current_sum += 1 sum_list.append(current_sum) for _ in range(q): start, end = map(int, input_stream.readline().split()) if start + m - 1 > end: print(0) continue print(sum_list[end] - sum_list[start + m - 2]) # if FILE_IO: # assert filecmp.cmp('current_output.txt', 'expected_output.txt', shallow=False) == True
PYTHON3
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.FilterInputStream; import java.io.BufferedInputStream; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author nirav */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scan in = new Scan(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, Scan in, PrintWriter out) { int n = in.scanInt(); int m = in.scanInt(); int q = in.scanInt(); String s = in.scanString(); String t = in.scanString(); int ans[] = new int[s.length() + 5]; for (int i = 0; i < s.length(); i++) { int u = 0; while ((u + i) < s.length() && u < t.length()) { if (t.charAt(u) != s.charAt(u + i)) { break; } else { u++; } } if (u == t.length()) { ans[i + 1] += 1; } } while (q-- > 0) { int h = in.scanInt(); int j = in.scanInt(); int an = 0; for (int i = h; i <= j; i++) { if ((j - i + 1) >= t.length()) { an += ans[i]; } else { break; } } out.println(an); } } } static class Scan { private byte[] buf = new byte[4 * 1024]; private int INDEX; private BufferedInputStream in; private int TOTAL; public Scan(InputStream inputStream) { in = new BufferedInputStream(inputStream); } private int scan() { if (INDEX >= TOTAL) { INDEX = 0; try { TOTAL = in.read(buf); } catch (Exception e) { e.printStackTrace(); } if (TOTAL <= 0) return -1; } return buf[INDEX++]; } public int scanInt() { int I = 0; int n = scan(); while (isWhiteSpace(n)) n = scan(); int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { I *= 10; I += n - '0'; n = scan(); } } return neg * I; } public String scanString() { int c = scan(); while (isWhiteSpace(c)) c = scan(); StringBuilder RESULT = new StringBuilder(); do { RESULT.appendCodePoint(c); c = scan(); } while (!isWhiteSpace(c)); return RESULT.toString(); } private boolean isWhiteSpace(int n) { if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true; else return false; } } }
JAVA
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#include <bits/stdc++.h> #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) using namespace std; const int N = 1e3 + 5; int sum[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; int n, m, q, l, r; string s, t; cin >> n >> m >> q; cin >> s >> t; for (int i = 0; i + m - 1 < n; i++) { bool f = true; for (int j = 0; j < m; j++) { if (s[i + j] != t[j]) { f = false; break; } } if (f) sum[i + 1]++; } for (int i = 1; i <= n; i++) sum[i] += sum[i - 1]; while (q--) { cin >> l >> r; r = r - m + 1; if (r >= l) cout << sum[r] - sum[l - 1] << endl; else cout << 0 << endl; } return 0; }
CPP
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
import java.io.*; import java.util.*; public class Asd { static PrintWriter w=new PrintWriter(System.out); static FastScanner s=new FastScanner(); public static void main(String[] args) { //int t=s.nextInt(); int t=1; while(t-->0) { solve(); } w.close(); } /* Function to print all the permutations of the string static String swap(String str, int i, int j) { char ch; char[] array = str.toCharArray(); ch = array[i]; array[i] = array[j]; array[j] = ch; return String.valueOf(array); } static void permute(String str,int low,int high) { if(low == high) list.add(Long.parseLong(str)); int i; for(i = low; i<=high; i++){ str = swap(str,low,i); permute(str, low+1,high); str = swap(str,low,i); } } use permute(str2,0,str2.length()-1); to perform combinations */ public static void solve() { int n=s.nextInt(); int m=s.nextInt(); int q=s.nextInt(); String str1=s.next(); String str2=s.next(); ArrayList<Integer> list=new ArrayList<>(); for(int i=0;i<=n-m;i++) { if(str1.substring(i,i+m).equals(str2)) {list.add(i);} } while(q-->0) { int a=s.nextInt()-1;int count=0; int b=s.nextInt();int flag=3; for(int i=0;i<list.size();i++) { if(list.get(i)>=a&&list.get(i)+m<=b) count++; } w.println(count); } } static int noofdivisors(int n) { //it counts no of divisors of every number upto number n int arr[]=new int[n+1]; for(int i=1;i<=(n);i++) for(int j=i;j<=(n);j=j+i) arr[j]++; return arr[0]; } static char[] reverse(char arr[]) { char[] b = new char[arr.length]; int j = arr.length; for (int i = 0; i < arr.length; i++) { b[j - 1] = arr[i]; j = j - 1; } return b; } static long factorial(int n) { long su=1; for(int i=1;i<=n;i++) su*=(long)i; return su; } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); public String next() { while (!st.hasMoreElements()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } int nextInt () { return Integer.parseInt(next()); } long nextLong () { return Long.parseLong(next()); } double nextDouble () { return Double.parseDouble(next()); } } }
JAVA
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
n, m, q = [int(i) for i in input().split()] s = input() t = input() positions = '' for i in range(n-m+1): if s[i:i + m] == t: positions += '1' else: positions += '0' for i in range(q): l,r = map(int, input().split()) if r - l + 1 >= m: print (positions[l-1:r-m+1].count('1')) else: print(0)
PYTHON3
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
from sys import stdin,stdout from collections import Counter import math I=lambda: map(int,stdin.readline().split()) I1=lambda: stdin.readline() n,m,q=I() s,t=I1().strip(),I1().strip() a=[0]*(n+1) for i in range(n): if i+m<=n and s[i:i+m]==t: a[i+1]+=a[i]+1 else: a[i+1]+=a[i] for i in range(q): l,r=I() if r-l+1<m: print(0) else: print(a[r-m+1]-a[l-1])
PYTHON3
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#include <bits/stdc++.h> using namespace std; const double PI = 2 * acos(0.0), EPS = 1e-9; const int INF = 1e9, NINF = -1e9, MOD = 1e9 + 7; void GO() { cout << fixed << setprecision(10); } int main() { GO(); int n, m, q, r, l, c, a[1001] = {}; string second, t; cin >> n >> m >> q >> second >> t; for (int i = 0; i <= n - m; i++) if (second.substr(i, m) == t) a[i] = 1; while (q--) { c = 0; cin >> r >> l; for (int i = r - 1; i <= l - m; i++) if (a[i]) c++; cout << c << "\n"; } return 0; }
CPP
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
n, m, q = (int(x) for x in input().split()) s = input() t = input() X = set() for i in range(len(s) - len(t) + 1): if s[i:i + len(t)] == t: X.add(i) Y = [0] for i in range(len(s)): tm = Y[-1] if i in X: tm += 1 Y.append(tm) for _ in range(q): l, r = (int(x) for x in input().split()) print(0 if r - len(t) + 1 < l - 1 else Y[r - len(t) + 1] - Y[l - 1])
PYTHON3
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
import java.io.*; import java.lang.reflect.Array; import java.math.BigInteger; import java.util.*; import java.util.Random; import java.util.StringTokenizer; public class Main { final long b = 31; String fileName = ""; ////////////////////// SOLUTION SOLUTION SOLUTION ////////////////////////////// int INF = Integer.MAX_VALUE/10; long MODULO = 1000_1000_100l; int MAX_VALUE = 1000_1000; long sum = 0; void solve() throws IOException { int n = readInt(); int m = readInt(); int q = readInt(); char[] s = readString().toCharArray(); char[] t = readString().toCharArray(); HashSet<Integer> st = new HashSet<>(); for (int i=0; i<n; ++i){ if (i+m<=n){ boolean flag = true; for (int j=0; j<m; ++j){ if (s[i+j] != t[j]) flag = false; } if (flag){ st.add(i); } } } int[] plus = new int[n]; for (int i=0; i<n; ++i){ plus[i] += plus[Math.max(i-1, 0)]; if (st.contains(i)) plus[i]++; } while (q-- > 0){ int l = readInt() - 1; int r = readInt() - 1; r -= m - 1; int ans = 0; if (r >=l){ ans+=plus[r]; if (l>0) ans-= plus[l-1]; } out.println(ans); } } class Star{ int x, y, len; Star(int x, int y, int len){ this.x = x; this.len = len; this.y = y; } } class Number implements Comparable<Number>{ int x, cost; Number(int x, int cost){ this.x = x; this.cost = cost; } @Override public int compareTo(Number o) { return Integer.compare(this.cost, o.cost); } } class Point{ int x, y; Point(int x, int y){ this.x = x; this.y = y; } } class Vertex implements Comparable<Vertex>{ int num, depth, e, c; Vertex(int num, int depth, int e, int c){ this.num = num; this.e = e; this.depth = depth; this.c = c; } @Override public int compareTo(Vertex o) { return Integer.compare(this.e, o.e); } } /////////////////////////////////////////////////////////////////////////////////////////// class Edge{ int from, to, num; Edge(int to, int num){ this.to = to; this.num = num; } } class SparseTable{ int[][] rmq; int[] logTable; int n; SparseTable(int[] a){ n = a.length; logTable = new int[n+1]; for(int i = 2; i <= n; ++i){ logTable[i] = logTable[i >> 1] + 1; } rmq = new int[logTable[n] + 1][n]; for(int i=0; i<n; ++i){ rmq[0][i] = a[i]; } for(int k=1; (1 << k) < n; ++k){ for(int i=0; i + (1 << k) <= n; ++i){ int max1 = rmq[k - 1][i]; int max2 = rmq[k-1][i + (1 << (k-1))]; rmq[k][i] = Math.max(max1, max2); } } } int max(int l, int r){ int k = logTable[r - l]; int max1 = rmq[k][l]; int max2 = rmq[k][r - (1 << k) + 1]; return Math.max(max1, max2); } } long checkBit(long mask, int bit){ return (mask >> bit) & 1; } class Dsu{ int[] parent; int countSets; Dsu(int n){ countSets = n; parent = new int[n]; for(int i=0; i<n; ++i){ parent[i] = i; } } int findSet(int a){ if(parent[a] == a) return a; parent[a] = findSet(parent[a]); return parent[a]; } void unionSets(int a, int b){ a = findSet(a); b = findSet(b); if(a!=b){ countSets--; parent[a] = b; } } } static int checkBit(int mask, int bit) { return (mask >> bit) & 1; } boolean isLower(char c){ return c >= 'a' && c <= 'z'; } //////////////////////////////////////////////////////////// class SegmentTree{ int[] t; int n; SegmentTree(int n){ t = new int[4*n]; build(new int[n+1], 1, 1, n); } void build (int a[], int v, int tl, int tr) { if (tl == tr) t[v] = a[tl]; else { int tm = (tl + tr) / 2; build (a, v*2, tl, tm); build (a, v*2+1, tm+1, tr); } } void update (int v, int tl, int tr, int l, int r, int add) { if (l > r) return; if (l == tl && tr == r) t[v] += add; else { int tm = (tl + tr) / 2; update (v*2, tl, tm, l, Math.min(r,tm), add); update (v*2+1, tm+1, tr, Math.max(l,tm+1), r, add); } } int get (int v, int tl, int tr, int pos) { if (tl == tr) return t[v]; int tm = (tl + tr) / 2; if (pos <= tm) return t[v] + get (v*2, tl, tm, pos); else return t[v] + get (v*2+1, tm+1, tr, pos); } } class Fenwik { long[] t; int length; Fenwik(int[] a) { length = a.length + 100; t = new long[length]; for (int i = 0; i < a.length; ++i) { inc(i, a[i]); } } void inc(int ind, int delta) { for (; ind < length; ind = ind | (ind + 1)) { t[ind] = Math.max(delta, t[ind]); } } long getMax(int r) { long sum = 0; for (; r >= 0; r = (r & (r + 1)) - 1) { sum = Math.max(sum, t[r]); } return sum; } } int gcd(int a, int b){ return b == 0 ? a : gcd(b, a%b); } long gcd(long a, long b){ return b == 0 ? a : gcd(b, a%b); } double binPow(double a, int pow){ if (pow == 0) return 1; if (pow % 2 == 1) { return a * binPow(a, pow - 1); } else { double c = binPow(a, pow / 2); return c * c; } } long binPow(long a, long b, long m) { if (b == 0) { return 1; } if (b % 2 == 1) { return ((a % m) * (binPow(a, b - 1, m) % m)) % m; } else { long c = binPow(a, b / 2, m); return (c * c) % m; } } int minInt(int... values) { int min = Integer.MAX_VALUE; for (int value : values) min = Math.min(min, value); return min; } int maxInt(int... values) { int max = Integer.MIN_VALUE; for (int value : values) max = Math.max(max, value); return max; } public static void main(String[] args) throws NumberFormatException, IOException { // TODO Auto-generated method stub new Main().run(); } void run() throws NumberFormatException, IOException { solve(); out.close(); }; BufferedReader in; PrintWriter out; StringTokenizer tok; String delim = " "; Random rnd = new Random(); Main() throws FileNotFoundException { try { in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } catch (Exception e) { if (fileName.isEmpty()) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } else { in = new BufferedReader(new FileReader(fileName + ".in")); out = new PrintWriter(fileName + ".out"); } } tok = new StringTokenizer(""); } String readLine() throws IOException { return in.readLine(); } String readString() throws IOException { while (!tok.hasMoreTokens()) { String nextLine = readLine(); if (null == nextLine) { return null; } tok = new StringTokenizer(nextLine); } return tok.nextToken(); } int readInt() throws NumberFormatException, IOException { return Integer.parseInt(readString()); } byte readByte() throws NumberFormatException, IOException { return Byte.parseByte(readString()); } int[] readIntArray (int n) throws NumberFormatException, IOException { int[] a = new int[n]; for(int i=0; i<n; ++i){ a[i] = readInt(); } return a; } Integer[] readIntegerArray (int n) throws NumberFormatException, IOException { Integer[] a = new Integer[n]; for(int i=0; i<n; ++i){ a[i] = readInt(); } return a; } long readLong() throws NumberFormatException, IOException { return Long.parseLong(readString()); } double readDouble() throws NumberFormatException, IOException { return Double.parseDouble(readString()); } }
JAVA
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
import bisect def occurances(s, t): locations = [] for i in range(len(s) - len(t) + 1): if t == s[i:i+len(t)]: locations.append(i) return locations if __name__ == '__main__': n, m, num_queries = list(map(int, input().split(' '))) s = input() t = input() locations = occurances(s, t) #print(locations) for i in range(num_queries): start, end = list(map(int, input().split(' '))) start -= 1 end -= 1 #print('start', start, 'end', end) if end - start + 1 < len(t): print(0) else: start_idx = bisect.bisect_left(locations, start) end_idx = bisect.bisect(locations, end - len(t) + 1) #print('start_idx', start_idx, 'end_idx', end_idx) print(end_idx - start_idx)
PYTHON3
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
n , m , q = map(int,input().split()) s = input() t = input() f = '' for i in range(n - m + 1): if s[i : i + m] == t : f += '1' else: f += '0' #print(f) ans = [] for i in range(q): l , r= map(int,input().split()) l-= 1 ans.append(f[l : max(r - m + 1 , 0 )].count('1')) for i in ans: print(i)
PYTHON3
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.util.InputMismatchException; /* * */ public class E48_B implements Runnable{ public static void main(String[] args) { new Thread(null, new E48_B(),"Main",1<<27).start(); } @Override public void run() { FastReader fd = new FastReader(System.in); PrintWriter out = new PrintWriter(System.out); int n = fd.nextInt(); int m = fd.nextInt(); int q = fd.nextInt(); String data = fd.next(); String sub = fd.next(); int[] pos = new int[n+1]; for (int i = 0; i <= n - m; i++) { boolean same = true; for (int j = i; j < i + m && same; j++) { if (data.charAt(j) != sub.charAt(j - i)) same = false; } if (same) pos[i + 1] = 1; } for (int i = 1; i <= n; i++) { pos[i] += pos[i-1]; //out.println(pos[i]); } for(int i = 0; i < q; i++){ int start = fd.nextInt(); int end = fd.nextInt(); if(end-start+1 < m){ out.println(0); } else{ out.println(pos[end-m+1] - pos[start-1]); } } out.close(); } //Helper functions static int[] getArray(int n,boolean isSorted, FastReader fd){ int[] data = new int[n]; for(int i = 0; i < data.length; i++){ data[i] = fd.nextInt(); } if(isSorted) Arrays.sort(data); return data; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static int lcm(int a, int b) { return (a*b)/gcd(a, b); } static boolean checkDistinct(int next){// check all digits are distinct. String process = String.valueOf(next); for(int i = 0;i < process.length()-1; i++){ String character = String.valueOf(process.charAt(i)); if(process.substring(i+1).contains(character)){ return false; } } return true; } static int limit = (int) 1e7 + 1; static int[] facts = new int[limit]; static void sieve() { // Store the minimum prime factors of 1 to LIMIT facts[1] = 1; for (int i = 2; i < limit; i++) { if (i % 2 == 0 && i > 2) { facts[i] = 2; } else { facts[i] = i; } } for (int i = 3; i * i < limit; i++) { if (facts[i] == i) { for (int j = i * i; j < limit; j += i) if (facts[j] == j) facts[j] = i; } } } static class FastReader { 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 FastReader(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); } } }
JAVA
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#include <bits/stdc++.h> using namespace std; const int N = 1111; string s, t; int sum[N]; int n, m, q; signed main() { ios_base::sync_with_stdio(false); cin >> n >> m >> q >> s >> t; for (int i = (1); i <= (n); i++) { bool found = true; for (int j = (0); j <= (m - 1); j++) { int ii = i - 1 + j; if (ii < n && s[ii] != t[j]) { found = false; break; } } sum[i] = sum[i - 1] + found; } while (q--) { int l, r; cin >> l >> r; int ans = 0; if (r - m + 1 >= 1) { ans = max(0, sum[r - m + 1] - sum[l - 1]); } cout << ans << "\n"; } return 0; }
CPP
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
import java.util.*; public class Segment{ public static void main(String[] args) { Scanner in=new Scanner(System.in); int n=in.nextInt(); int m=in.nextInt(); int q=in.nextInt(); String s=in.next(); String t=in.next(); int[] arr=new int[n]; Arrays.fill(arr,0); for(int i=0;i<n-m+1;i++){ if(s.charAt(i)==t.charAt(0)){ if(s.substring(i,i+m).equals(t)) arr[i]=1; } } while(q-->0){ int l=in.nextInt()-1; int r=in.nextInt()-1; int count=0; for(int i=l;i<=r-m+1;i++){ if(arr[i]==1) count++; }System.out.println(count); } in.close(); } }
JAVA
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const int maxn = 1010; char a[maxn], b[maxn]; int n, m, q; vector<int> pos; void solve() { while (q--) { int l, r, tr; scanf("%d%d", &l, &r); tr = r - m + 1; if (l > tr) puts("0"); else { printf("%d\n", upper_bound(pos.begin(), pos.end(), tr) - pos.begin() - (lower_bound(pos.begin(), pos.end(), l) - pos.begin())); } } } int main() { cin >> n >> m >> q; cin >> a + 1; cin >> b + 1; for (int i = 1; i <= n; ++i) { bool ok = true; for (int j = 0; j < m; ++j) { if (i + j > n || (a[i + j] != b[j + 1])) { ok = false; break; } } if (ok) pos.push_back(i); } solve(); return 0; }
CPP
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
from sys import stdin,stdout from math import gcd st=lambda:list(stdin.readline().strip()) li=lambda:list(map(int,stdin.readline().split())) mp=lambda:map(int,stdin.readline().split()) inp=lambda:int(stdin.readline()) pr=lambda n: stdout.write(str(n)+"\n") def solve(): a,b,q=mp() s=input() t=input() dp=[0] for i in range(a-b+1): if s[i:i+b]==t: dp.append(dp[-1]+1) else: dp.append(dp[-1]) for i in range(len(dp),a+1): dp.append(dp[-1]) for i in range(q): x,y=mp() x-=1 y-=b-1 if x<y: pr(dp[y]-dp[x]) else: pr(0) for _ in range(1): solve()
PYTHON3
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
n, m, q = map(int, raw_input().split()) s = raw_input() t = raw_input() tins = [] for i in range(n): if i + m > n: break found = True for j in range(m): if s[i + j] != t[j]: found = False break if found: tins.append([i + 1, i + m]) newtins = [] firsttins = [] lasttins = [] for i in tins: newtins.append(i[0]) newtins.append(i[1]) firsttins.append(i[0]) lasttins.append(i[1]) # print tins # print newtins # print firsttins # print lasttins from bisect import bisect_left as bl from bisect import bisect_right as br # print firsttins # print lasttins for i in range(q): l, r = map(int, raw_input().split()) fil = bl(firsttins, l) nil = bl(newtins, l) fir = br(lasttins, r) nir = br(newtins, r) if (fir - fil) < 0: print 0 else: print fir - fil
PYTHON
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#include <bits/stdc++.h> using namespace std; int n, m, q, dp[10004]; string s, t; int main() { cin >> n >> m >> q >> s >> t; for (int i = 0, ok = true; i <= n - m; ++i, ok = true) { for (int j = 0; j < m; ++j) { if (s[i + j] != t[j]) ok = false; } dp[i + 1] = dp[i] + ok; } for (int i = 0, l, r; i < q; ++i) { cin >> l >> r; cout << (l > r - m + 1 ? 0 : dp[r - m + 1] - dp[l - 1]) << endl; } }
CPP
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
n,m,q=map(int, raw_input().split()) s=raw_input() t=raw_input() ans=[] for i in range(0,max(m,n)+1): ans.append(0) if n>=m: for i in range(0,n-m+1): if s[i:i+m]==t: ans[i]=1 for i in range(1,max(m,n)+1): ans[i]=ans[i]+ans[i-1] for i in range(0,q): l,r=map(int,raw_input().split()) l-=1 r-=1 if r-m+1>=l: if l>=1: print ans[r-m+1]-ans[l-1] else: print ans[r-m+1] else: print 0
PYTHON
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n, m, q; cin >> n >> m >> q; string s1, s2; cin >> s1 >> s2; int occ[n]; int sta[n]; int sum[n]; for (int o = 0; o < n; o++) { occ[o] = 0; sum[o] = 0; sta[o] = 0; } int v = 0; for (int e = 0; e < n - m + 1; e++) { int fl = 1; for (int l = 0; l < m; l++) { if (s1[e + l] != s2[l]) { fl = 0; break; } } if (fl == 1) occ[e + m - 1] = 1; } for (int g = 0; g < n; g++) { v = v + occ[g]; sum[g] = v; } for (int a = 1; a <= q; a++) { int p, q; cin >> p >> q; if (q - p < m - 1) cout << "0" << endl; else if (p != 1) cout << sum[q - 1] - sum[p - 2 + m - 1] << endl; else cout << sum[q - 1] << endl; } }
CPP
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
import java.util.*; public class A{ public static void main(String arg[]){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(), m = sc.nextInt(), q = sc.nextInt(); sc.nextLine(); String s = sc.nextLine(); String a = sc.nextLine(); int al = a.length(); int ar[] = new int[n]; for(int i=0;i<n;i++){ if(i+al > n) break; if(s.substring(i, i+al).equals(a)) ar[i]++; } int cs[] = new int[n]; cs[0] = ar[0]; for(int i=1;i<n;i++){ cs[i] = cs[i-1] + ar[i]; } int t = 1; while(q-->0){ int l = sc.nextInt(), r = sc.nextInt(); l--; r = r-al; int ans = 0; if(r>=0 && r>=l){ ans = cs[r]; if(l!=0) ans = ans - cs[l-1]; } System.out.println(ans); t++; } } }
JAVA
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#include <bits/stdc++.h> int n, m, q, a0, a1; char s[1003], t[1003]; int pp[1003]; int main() { scanf("%d %d %d", &n, &m, &q); scanf("%s", s); scanf("%s", t); for (int i = 0; i <= n - m; ++i) { int j = 0; for (; j < m && s[i + j] == t[j]; ++j) ; if (j == m) pp[i] = 1; } for (int i = 1; i <= n - m; ++i) pp[i] += pp[i - 1]; for (int i = 0; i < q; ++i) { scanf("%d %d", &a0, &a1); a0--; a1--; if (a0 + m - 1 <= a1) { if (a0 > 0) printf("%d\n", pp[a1 - m + 1] - pp[a0 - 1]); else printf("%d\n", pp[a1 - m + 1]); } else printf("0\n"); } return 0; }
CPP
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; public class Abc { public static void main(String[] args) throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer t=new StringTokenizer(br.readLine()); int N=Integer.parseInt(t.nextToken()); int M=Integer.parseInt(t.nextToken()); int Q=Integer.parseInt(t.nextToken()); String S=br.readLine(); String T=br.readLine(); ArrayList<Integer> A=new ArrayList<>(); ArrayList<Integer> B=new ArrayList<>(); int k=0; for(int i=0;i<(N-M+1);i++) { if(S.substring(i,i+M).equals(T)) { A.add(i+1); B.add(i+M); } } while(Q>0) { int cnt=0; t=new StringTokenizer(br.readLine()); int L=Integer.parseInt(t.nextToken()); int R=Integer.parseInt(t.nextToken()); for(int i=0;i<A.size();i++) { if(L<=A.get(i)&&L<=B.get(i)&&R>=A.get(i)&&R>=B.get(i)) { cnt++; } } System.out.println(cnt); Q--; } } }
JAVA
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
n,m,q=[int(x) for x in input().split()] s=input() t=input() li=[] for i in range(q): a,b=input().split() li.append([int(a)-1,int(b)-1]) dp=[] for i in range(n-m+1): if s[i:i+m]==t: dp.append(1) else: dp.append(0) for i in li: if i[1]-i[0]<m-1: print(0) else: print(sum(dp[i[0]:(i[1]-m+2)]))
PYTHON3
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.OutputStream; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Pradyumn */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(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, FastReader in, PrintWriter out) { Debug debug = new Debug(); int n = in.nextInt(); int m = in.nextInt(); int Q = in.nextInt(); int[] count = new int[n]; String s1 = in.nextString(); String s2 = in.nextString(); for (int i = 0; i + m <= n; ++i) { String s = s1.substring(i, i + m); if (s.equals(s2)) count[i] += 1; } // for (int i = 1;i < n; ++i) count[i] += count[i - 1]; // debug.tr(count); for (int q = 0; q < Q; ++q) { int l = in.nextInt() - 1; int r = in.nextInt() - 1; int ans = 0; for (int i = l; i + m <= r + 1; ++i) ans += count[i]; out.println(ans); } } } static class Debug { PrintWriter out; boolean oj; boolean system; long timeBegin; Runtime runtime; public Debug(PrintWriter out) { oj = System.getProperty("ONLINE_JUDGE") != null; this.out = out; this.timeBegin = System.currentTimeMillis(); this.runtime = Runtime.getRuntime(); } public Debug() { system = true; oj = System.getProperty("ONLINE_JUDGE") != null; OutputStream outputStream = System.out; this.out = new PrintWriter(outputStream); this.timeBegin = System.currentTimeMillis(); this.runtime = Runtime.getRuntime(); } } static class FastReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int pnumChars; public FastReader(InputStream stream) { this.stream = stream; } private int pread() { 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 int nextInt() { int c = pread(); while (isSpaceChar(c)) c = pread(); int sgn = 1; if (c == '-') { sgn = -1; c = pread(); } int res = 0; do { if (c == ',') { c = pread(); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = pread(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = pread(); while (isSpaceChar(c)) c = pread(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = pread(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
JAVA
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#include <bits/stdc++.h> using namespace std; int z[1000005]; string s; void Z() { int n = s.size(), l = 0, r = 0, i; for (i = 0; i < n; i++) { if (i > r) { l = r = i; while (r < n && s[r] == s[r - l]) { r++; } z[i] = r - l; r--; } else { if (i + z[i - l] <= r) { z[i] = z[i - l]; } else { l = i; while (r < n && s[r] == s[r - l]) { r++; } z[i] = r - l; r--; } } } } int main() { int n, m, q; scanf("%d %d %d", &n, &m, &q); string d, t; cin >> d >> t; s = t + '$' + d; Z(); vector<int> a; for (int i = 0; i < t.size() + d.size() + 1; i++) { if (z[i] >= t.size()) { a.push_back(i - t.size() - 1); } } int i, l, r, u, v, mi; for (i = 0; i < q; i++) { scanf("%d %d", &u, &v); u--; v--; l = 0, r = a.size() - 1; if (r == -1) { printf("0\n"); continue; } while (l < r) { mi = (l + r) >> 1; if (a[mi] >= u) { r = mi; } else { l = mi + 1; } } if ((a[r] < u) || (a[r] > v) || (a[r] + t.size() - 1 > v)) { printf("0\n"); continue; } int idxst = r; l = 0, r = a.size() - 1; while (l < r) { mi = (l + r + 1) >> 1; if (a[mi] + t.size() - 1 <= v) { l = mi; } else { r = mi - 1; } } if ((a[l] < u) || (a[l] > v)) { printf("0\n"); continue; } int ans = l - idxst + 1; printf("%d\n", ans); } return 0; }
CPP
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
n,m,q=map(int,input().split()) s=input() t=input() x=0 dp1=[] 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()) print(dp[l-1][r-1])
PYTHON3
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#include <bits/stdc++.h> using namespace std; vector<int> l, r; int main() { string s, t; int n, m, q, x, y; cin >> n >> m >> q >> s >> t; for (int i = 0; i < n; i++) { int flag = 1; if (s[i] == t[0]) { for (int j = i, k = 0; j < n && k < m; j++, k++) if (s[j] != t[k]) { flag = 0; break; } if (flag) { l.push_back(i + 1); r.push_back(i + m); } } } while (q--) { cin >> x >> y; int a, cnt = 0; a = lower_bound(l.begin(), l.end(), x) - l.begin(); for (int i = a; i < l.size(); i++) { if (l[i] >= x && r[i] <= y) cnt++; if (r[i] > y) break; } cout << cnt << endl; } return 0; }
CPP
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#include <bits/stdc++.h> using namespace std; const int64_t MOD = 1e9 + 7; const int64_t N = 2e6; bool isQuery = false; int64_t power(int64_t x, int64_t n) { if (n == 0) return 1; else if (n % 2 == 0) return power(x, n / 2) * power(x, n / 2); else return x * power(x, n / 2) * power(x, n / 2); } int64_t powerMod(int64_t x, int64_t n, int64_t mod) { if (n == 0) return 1; else if (n % 2 == 0) return (powerMod(x, n / 2, mod) * powerMod(x, n / 2, mod)) % mod; else return (x * powerMod(x, n / 2, mod) * powerMod(x, n / 2, mod)) % mod; } void solve() { int64_t n, m, q; cin >> n >> m >> q; string s, t; cin >> s >> t; vector<int64_t> dp(n, 0), pref(n, -1), suff(n, -1), id; int64_t ans = 0, p = -1; for (int64_t i = 0, j; i < n; i++) { for (j = 0; j < m && i + j < n; j++) { if (s[i + j] != t[j]) break; } if (j == m) pref[i + m - 1] = i + m - 1, id.push_back(i + m - 1), dp[i + m - 1] = 1; } for (int64_t i = 1; i < n; i++) if (pref[i] == -1) pref[i] = pref[i - 1]; for (int64_t i = n - 1, j; i >= 0; i--) { for (j = m - 1; j >= 0 && i - j >= 0; j--) { if (s[i - (m - 1 - j)] != t[j]) break; } if (j == -1) suff[i - m + 1] = i; } for (int64_t i = n - 2; i >= 0; i--) if (suff[i] == -1) suff[i] = suff[i + 1]; for (int64_t i = 1; i < n; i++) dp[i] += dp[i - 1]; while (q--) { int64_t l, r; cin >> l >> r; l--, r--; if (pref[r] == -1 || suff[l] == -1 | pref[r] < suff[l]) cout << 0; else if (suff[l] > 0) cout << dp[pref[r]] - dp[suff[l] - 1]; else cout << dp[pref[r]]; cout << endl; } } signed main() { int64_t t = 1; if (isQuery) cin >> t; while (t--) { solve(); cout << endl; } }
CPP
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long n, m, i, q, j, k, l, count, c, p = 0, a[1001] = {0}; cin >> n >> m >> q; string s, t; cin >> s >> t; for (i = 0; i <= n - m; i++) { if (s.substr(i, m) == t) a[i] = {1}; } for (i = 1; i <= q; i++) { long long d, e; c = 0; cin >> d >> e; for (j = d - 1; j <= e - m; j++) { if (a[j]) c++; } cout << c << "\n"; } }
CPP
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#include <bits/stdc++.h> using namespace std; int n, m, q; string s; string t; int d[1001]; bool check(string s) { for (int i = 0; i < s.length(); i++) { if (s[i] != t[i]) return false; } return true; } int main() { cin >> n >> m >> q; cin >> s >> t; for (int i = 0; i < n - m + 1; i++) { string tmp = ""; for (int j = i; j < i + m; j++) { tmp += s[j]; } if (check(tmp)) { d[i + m]++; } } for (int i = 1; i <= n; i++) { d[i] += d[i - 1]; } while (q--) { int l, r; scanf("%d%d", &l, &r); int len = r - l + 1; if (len < t.length()) { cout << 0 << "\n"; continue; } cout << d[r] - d[l + m - 2] << "\n"; } return 0; }
CPP
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#include <bits/stdc++.h> using namespace std; int a[1001]; int main() { int i, n, m, q, j, l, r; string str1, str2; cin >> n >> m >> q; cin >> str1; cin >> str2; for (i = 0; i <= n - m; i++) { for (j = 0; j < m; j++) { if (str1[i + j] != str2[j]) { break; } } if (j == m) a[i + 1] = 1; } for (i = 2; i <= n; i++) { a[i] = a[i] + a[i - 1]; } for (i = 1; i <= q; i++) { cin >> l >> r; if (r - l + 1 < m) { cout << 0 << endl; continue; } if (l == 1) { cout << a[r - m + 1] << endl; ; } else { cout << a[r - m + 1] - a[l - 1] << endl; } } }
CPP
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
n,m,q=map(int,raw_input().split()) s=raw_input() t=raw_input() l=[] for i in xrange(n-m+1): if s[i:i+m]==t: l.append([i,i+m-1]) for i in xrange(q): a,b=map(int,raw_input().split()) count=0 for j in xrange(len(l)): if l[j][0]>=a-1 and l[j][1]<b: count+=1 print count
PYTHON
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
import java.io.PrintWriter; import java.util.*; public class codeforces { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int m = in.nextInt(); int q = in.nextInt(); String s = in.next(); String t = in.next(); int[] occ = new int[n]; int c = 0; while((c = s.indexOf(t, c)) >= 0) { occ[c] = 1; c += 1; } for(int i = 1; i < n; ++i) { occ[i] += occ[i - 1]; } for(int i = 0; i < q; ++i) { int begin = in.nextInt() - 1; int end = in.nextInt() - 1; end = end - m + 1; if(end < begin) { out.println(0); continue; } int ans = occ[end]; if(begin > 0) { ans -= occ[begin - 1]; } out.println(ans); } out.close(); } }
JAVA
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#include <bits/stdc++.h> using namespace std; const int maxn = 10005; int Next[maxn]; char str[maxn], th[maxn]; int change(char s[]) { int i = 0, j = 1; Next[i] = 0; while (s[j]) { if (s[i] == s[j]) { i++; Next[j] = i; j++; } else { if (i > 0) { i = Next[i - 1]; } else { Next[j] = i; j++; } } } for (int i = j - 1; i > 0; i--) { Next[i] = Next[i - 1]; } Next[0] = -1; return j; } int num[maxn] = {0}; int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); scanf("%s%s", str, th); int l = change(th); int i = 0, j = 0, cnt = 0; while (str[i]) { if (str[i] == th[j]) { if (j == l - 1) { num[i - m + 1]++; cnt++; j = Next[j]; } else { i++; j++; } } else { if (j >= 0) { j = Next[j]; } else { i++; j = 0; } } } while (k--) { int l, r; scanf("%d%d", &l, &r); l--; r--; int sum = 0; for (int i = l; i <= r; i++) { if ((r - i + 1) >= m) sum += num[i]; } printf("%d\n", sum); } return 0; }
CPP
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
from sys import stdin,stdout a,b,c=map(int,stdin.readline().split()) z=stdin.readline();z1=stdin.readline();p=[] k=[list(map(int,stdin.readline().split())) for _ in " "*c] for i in range(a-b+1): for j in range(b): if z[i+j]!=z1[j]:break else:p+=[[i+1,i+b]] o=[0]*c for i in p: for j in range(c): if k[j][0]<=i[0] and i[1]<=k[j][1]:o[j]+=1 stdout.write(' '.join(map(str,o)))
PYTHON3
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
n, m, q = map(int, input().split()) s = input() t = input() start = {} for i in range(1, n+1): start[i] = False for i in range(n-m+1): if s[i:i+m]==t: start[i+1] = True d = {} for i in range(1, n+1): for j in range(i, n+1): d[(i, j)] = 0 for i in range(1, n+1): cnt = 0 for j in range(i, n-m+2): if start[j]: cnt+=1 d[(i, j+m-1)]+=cnt for i in range(q): l, r = map(int, input().split()) print(d[(l, r)])
PYTHON3
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#!/usr/bin/env python2 """ This file is part of https://github.com/cheran-senthil/PyRival Copyright 2019 Cheran Senthilkumar <[email protected]> """ from __future__ import division, print_function import itertools import os import sys from atexit import register from collections import deque from io import BytesIO class dict(dict): """dict() -> new empty dictionary""" def items(self): """D.items() -> a set-like object providing a view on D's items""" return dict.iteritems(self) def keys(self): """D.keys() -> a set-like object providing a view on D's keys""" return dict.iterkeys(self) def values(self): """D.values() -> an object providing a view on D's values""" return dict.itervalues(self) def gcd(x, y): """greatest common divisor of x and y""" while y: x, y = y, x % y return x range = xrange filter = itertools.ifilter map = itertools.imap zip = itertools.izip sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size)) sys.stdout = BytesIO() register(lambda: os.write(1, sys.stdout.getvalue())) input = lambda: sys.stdin.readline().rstrip('\r\n') def main(): n, m, q = map(int, input().split()) if m > n: for _ in range(q): print(0) return s = input() t = deque(input()) window = deque(s[:m]) pref_sums = [0] * (n - m + 2) pref_sums[1] = 1 if window == t else 0 for i in range(m, n): window.append(s[i]) window.popleft() pref_sums[i - m + 2] = pref_sums[i - m + 1] + (1 if window == t else 0) for i in range(q): l, r = map(int, input().split()) if r - l < m - 1: print(0) else: print(pref_sums[r - m + 1] - pref_sums[l - 1]) if __name__ == '__main__': main()
PYTHON
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n, m, q; string s, t; cin >> n >> m >> q; cin >> s >> t; vector<int> v; v.clear(); if (n < m) { for (int i = 0; i < q; i++) { int a, b; cin >> a >> b; cout << "0" << endl; } return 0; } for (int i = 0; i <= s.size() - t.size(); i++) { int j; int val = i; for (j = 0; j < t.size(); j++) { if (t[j] == s[val++]) continue; else break; } if (j == t.size()) v.push_back(i); } if (v.size() == 0) { for (int i = 0; i < q; i++) { int a, b; cin >> a >> b; cout << "0" << endl; } return 0; } for (int i = 0; i < q; i++) { int a, b; cin >> a >> b; a = a - 1; b = b - 1; int count = 0; for (int p = 0; p < v.size(); p++) { if (v[p] >= a && v[p] + m - 1 <= b) count++; } cout << count << endl; } return 0; }
CPP
1016_B. Segment Occurrences
You are given two strings s and t, both consisting only of lowercase Latin letters. The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order. Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a). You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i]. Input The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively. The second line is a string s (|s| = n), consisting only of lowercase Latin letters. The third line is a string t (|t| = m), consisting only of lowercase Latin letters. Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query. Output Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i]. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Output 0 1 0 1 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Output 4 0 3 Input 3 5 2 aaa baaab 1 3 1 1 Output 0 0 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
2
8
//package Contest501div2; import java.util.Arrays; import java.util.Scanner; public class main501B { public static Scanner enter = new Scanner(System.in); public static void main(String[] args) { int n=enter.nextInt(); int m=enter.nextInt(); int q=enter.nextInt(); String s= enter.next(); String t= enter.next(); int[] mass = new int[s.length()]; int del=0; while(true){ int h= s.indexOf(t); if(h==-1){ break; } else{ mass[h+del]++; s=s.substring(h+1); del=n-s.length(); } } //System.out.println(Arrays.toString(mass)); for (int i = 0; i <q; i++) { int a=enter.nextInt(); int b= enter.nextInt(); int answer=0; for (int j = a-1; j <=b-m ; j++) { answer+=mass[j]; } System.out.println(answer); } } }
JAVA