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
YAML Metadata Warning: empty or missing yaml metadata in repo card (https://huggingface.co/docs/hub/datasets-cards)

HF-datasets version of Deepmind's code_contests dataset, notably used for AlphaGo. 1 row per solution, no test data or incorrect solutions included (only name/source/description/solution/language/difficulty)

Downloads last month
262
Edit dataset card