Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | n, m, q = list(map(int, input().split()))
s = list(input())
t = list(input())
a = [list(map(int, input().split())) for i in range(q)]
if n < m:
for i in range(q):
print(0)
exit()
res = [0] * (n - m + 1)
for i in range(n - m + 1):
if s[i:(i + m)] == t:
res[i] = 1
for u in range(q):
l = a[u][0]
r = a[u][1]
ans = 0
for y in range(l - 1, r - m + 1):
# print(y)
if res[y] == 1:
ans += 1
print(ans) | PYTHON3 |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import sys
from array import array
def readline(): return sys.stdin.buffer.readline().decode('utf-8')
n, m, q = map(int, readline().split())
s, t = '*' + readline().rstrip(), readline().rstrip()
dp_r = [0]*(n+1)
dp_l = [0]*(n+1)
for i in range(m, n+1):
if s[i-m+1:i+1] == t:
dp_r[i] = 1
dp_l[i-m+1] = 1
for i in range(n):
dp_r[i+1] += dp_r[i]
dp_l[i+1] += dp_l[i]
ans = [0]*q
for i in range(q):
l, r = map(int, readline().split())
if r - l + 1 >= m:
ans[i] = dp_r[r] - dp_l[l-1]
sys.stdout.buffer.write(('\n'.join(map(str, ans))).encode('utf-8'))
| PYTHON3 |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class SegmentOccurrences {
static class MyScanner {
private BufferedReader br;
private StringTokenizer st;
MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
private static boolean[] precalculate(String s, String t) {
boolean [] match = new boolean[s.length()];
for (int i=0; i<=s.length() - t.length(); ++i) {
boolean matches = true;
for (int j=0; j<t.length(); ++j) {
if (s.charAt(i+j) != t.charAt(j)) {
matches = false;
break;
}
}
match[i] = matches;
}
return match;
}
public static void main(String[] args) {
MyScanner sc = new MyScanner();
int sLength = sc.nextInt();
int tLength = sc.nextInt();
int queries = sc.nextInt();
String s = sc.next();
String t = sc.next();
boolean[] matches = precalculate(s, t);
for (int i=0; i<queries; ++i) {
int left = sc.nextInt() - 1;
int right = sc.nextInt();
int occurrences = 0;
for (int j=left; j<=right - t.length(); ++j) {
if (matches[j]) {
occurrences++;
}
}
System.out.println(occurrences);
}
}
}
| 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 __future__ import print_function,division
import sys
if sys.version_info < (3, 0):
range = xrange
input = sys.stdin.readline
class SegmentSum:
def __init__(S,n):
sz = 1
while sz < n: sz <<= 1
S.data = [0]*(n + sz)
S.sz = sz
def sum(S,l,r):
sz = S.sz
data = S.data
summa = 0
l += sz
r += sz
while l < r:
if l%2==1:
summa += data[l]
l += 1
if r%2==1:
r -= 1
summa += data[r]
l >>= 1
r >>= 1
return summa
def inc(S,pos):
pos += S.sz
while pos > 0:
S.data[pos] += 1
pos >>= 1
n,m,q = map(int,input().split())
s = input().strip()
t = input().strip()
T = SegmentSum(n)
i = 0
while True:
i = s.find(t,i)
if i == -1: break
T.inc(i)
i += 1
for _ in range(q):
l,r = map(int,input().split())
l -= 1
r -= 1
print(T.sum(l,r+2-m))
| 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())
a=input()
b=input()
flags=[0]*(1007)
pr=[0]*(1007)
for i in range(n-m+1):
f=1
for j in range(m):
if a[i+j]!=b[j]:
f=0
flags[i]=f
pr[i+1]=pr[i]+flags[i]
for i in range(max(0,n-m+1),n):
pr[i+1]=pr[i]
for i in range(q):
l,r=map(int,input().split())
l-=1
r-=(m-1)
if r>=l:
print(pr[r]-pr[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 |
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class edu {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
ArrayList<Integer> list=new ArrayList<Integer>();
int n=in.nextInt();int a[]=new int[n];
int m=in.nextInt();
int q=in.nextInt();
String s=in.next();
String t=in.next();
for(int i=0;i+m<=n;i++)
{
if(s.substring(i,i+m).equals(t))
list.add(i+1);
}
while(q-->0)
{int c=0;
String sub=" ";
int l=in.nextInt();
int r=in.nextInt();
for(int h:list)
{
if(h>=l&&h+m-1<=r)
c++;
}
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 | #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()]
L = da[0]
R = da[1]
cnt = 0
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 | /***
* ██████╗=====███████╗====███████╗====██████╗=
* ██╔══██╗====██╔════╝====██╔════╝====██╔══██╗
* ██║==██║====█████╗======█████╗======██████╔╝
* ██║==██║====██╔══╝======██╔══╝======██╔═══╝=
* ██████╔╝====███████╗====███████╗====██║=====
* ╚═════╝=====╚══════╝====╚══════╝====╚═╝=====
* ============================================
*/
import java.io.*;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Array;
import java.util.*;
import java.math.*;
import java.lang.*;
public class AA implements Runnable {
public void run() {
InputReader sc = new InputReader();
PrintWriter out = new PrintWriter(System.out);
int i=0,j=0,k=0;
int t=0;
//t=sc.nextInt();
for(int testcase = 0;testcase < t; testcase++)
{
}
int n=sc.nextInt();
int n2=sc.nextInt();
int queries=sc.nextInt();
String first=sc.next();
String second=sc.next();
int last=-1;
//out.println(first.indexOf(second)+" ||||");
List<Integer> ind=new ArrayList<>();
while (first.indexOf(second,last+1)>=0)
{
last=first.indexOf(second,last+1);
ind.add(last+1);
}
//out.println(ind);
int arr[][]=new int[queries][2];
for (i=0;i<queries;i++)
{
arr[i][0]=sc.nextInt();
arr[i][1]=sc.nextInt();
}
/* Arrays.sort(arr, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if(o1[0] == o2[0]){
return o1[1]-o2[1];
}
return o1[0]-o2[0] ;
}
});*/
int lis=ind.size();
for (i=0;i<queries;i++)
{
j=0;
int counter=0;
while (j<lis&&ind.get(j)<arr[i][0])
{
j++;
}
while (j<lis&&ind.get(j)+n2-1<=arr[i][1])
{
counter++;
j++;
}
out.println(counter);
}
//================================================================================================================================
out.flush();
out.close();
}
//================================================================================================================================
public static int[] sa(int n,InputReader sc)
{
int inparr[]=new int[n];
for (int i=0;i<n;i++)
inparr[i]=sc.nextInt();
return inparr;
}
public static long gcd(long a,long b){
return (a%b==0l)?b:gcd(b,a%b);
}
private static long lcm(long a, long b)
{
return a * (b / gcd(a, b));
}
public int egcd(int a, int b) {
if (a == 0)
return b;
while (b != 0) {
if (a > b)
a = a - b;
else
b = b - a;
}
return a;
}
public int countChar(String str, char c)
{
int cnt = 0;
for(int i=0; i < str.length(); i++)
{ if(str.charAt(i) == c)
cnt++;
}
return cnt;
}
static int binSearch(Integer[] inparr, int number){
int left=0,right=inparr.length-1,mid=(left+right)/2,ind=0;
while(left<=right){
if(inparr[mid]<=number){
ind=mid+1;
left=mid+1;
}
else
right=mid-1;
mid=(left+right)/2;
}
return ind;
}
static int binSearch(int[] inparr, int number){
int left=0,right=inparr.length-1,mid=(left+right)/2,ind=0;
while(left<=right){
if(inparr[mid]<=number){
ind=mid+1;
left=mid+1;
}
else
right=mid-1;
mid=(left+right)/2;
}
return ind;
}
static class Pair
{
int a,b;
Pair(int aa,int bb)
{
a=aa;
b=bb;
}
String get()
{
return a+" "+b;
}
String getrev()
{
return b+" "+a;
}
}
static boolean isPrime(long n) {
if(n < 2) return false;
if(n == 2 || n == 3) return true;
if(n%2 == 0 || n%3 == 0) return false;
long sqrtN = (long)Math.sqrt(n)+1;
for(long i = 6L; i <= sqrtN; i += 6) {
if(n%(i-1) == 0 || n%(i+1) == 0) return false;
}
return true;
}
static long factorial(long n)
{
if (n == 0)
return 1;
return n*factorial(n-1);
}
//================================================================================================================================
static class InputReader
{
BufferedReader br;
StringTokenizer st;
public InputReader()
{
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;
}
}
public static void main(String args[]) throws Exception {
new Thread(null, new AA(),"Main",1<<27).start();
}
}
| 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 |
m, n, x = raw_input().split()
s = raw_input()
t = raw_input()
m, n, x = int(m), int(n), int(x)
class TreeNode(object):
def __init__(self, l, r):
self.left = None
self.right = None
self.interval = (l, r)
self.val = 0
def build(start, end):
# print start, end
node = TreeNode(start, end)
if start == end:
if s[start:start+n] == t:
node.val = 1
elif start < end:
mid = (start + end) / 2
node.left = build(start, mid)
node.right = build(mid+1, end)
node.val = node.left.val + node.right.val
# print node.val
return node
def pt(root):
print root.interval, root.val
if root.left:
pt(root.left)
if root.right:
pt(root.right)
root = build(0, m-1)
# pt(root)
def find(node, target):
if target == node.interval[0]:
return node.val
if target > node.interval[1]:
return 0
mid = sum(node.interval) / 2
if target == mid + 1:
return node.right.val
elif target < mid + 1:
return node.right.val + find(node.left, target)
else:
return find(node.right, target)
for _ in xrange(int(x)):
l, r = raw_input().split()
l, r = int(l) - 1, int(r) - len(t)
if r < l:
print 0
continue
left = find(root, l)
right = find(root, r + 1)
print left - right
| PYTHON |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import java.util.*;
import java.io.*;
public class Main
{
public static void main(String args[])
{
// int n,m;
{
Scanner sc= new Scanner(System.in);
int n= sc.nextInt();
int m= sc.nextInt();
int q= sc.nextInt();
String s=sc.next();
String t= sc.next();
int ns= s.length();
int nt= t.length();
// nt>ns case
if(nt>ns)
{
for(int i=0;i<q;i++)
{
System.out.println(0);
}
return;
}
else
{
ArrayList<Integer> occur= new ArrayList();
for(int i=0;i<ns;i++)
{
int flag=0;
for(int j=0;j<nt;j++)
{
if(i+j<ns)
{
if(s.charAt(i+j)!=t.charAt(j))
{
flag=1;
break;
}
}
else
{
flag=1;
break;
}
}
if(flag==0)
{
occur.add(i);
}
}
int nn= occur.size();
for(int i=0;i<q;i++)
{
int li= sc.nextInt();
li--;
int ri= sc.nextInt();
ri--;
int count=0;
for(int j=0;j<nn;j++)
{
if(occur.get(j)+nt-1<=ri && occur.get(j)>=li)
{
count++;
}
else if(occur.get(j)+nt-1>ri)
break;
}
System.out.println(count);
}
}
}
// {
// int n= sc.nextInt();
// int arr1[]= new int[n];
// int pref1[]= new int[n];
// int suff1[]= new int[n];
// int arr2[]= new int[n];
// int pref2[]= new int[n];
// int suff2[]= new int[n];
// for(int i=0;i<n;i++)
// {
// arr1[i]= sc.nextInt();
// if(i==0)
// pref1[i]=arr1[i];
// else
// pref1[i]=pref1[i-1]+arr1[i];
// }
// for(int i=0;i<n;i++)
// {
// arr2[i]= sc.nextInt();
// if(i==0)
// pref2[i]=arr2[i];
// else
// pref2[i]=pref2[i-1]+arr2[i];
// }
// for(int i=n-1;i>=0;i--)
// {
// if(i==n-1)
// suff1[i]=arr1[i];
// else
// suff1[i]=suff1[i+1]+arr1[i];
// }
// for(int i=n-1;i>=0;i--)
// {
// if(i==n-1)
// suff1[i]=arr1[i];
// else
// suff1[i]=suff1[i+1]+arr1[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 | n, m, q = map(int, input().split())
a = input()
b = input()
c = [0]
for i in range(1, n + 1):
# print(a[max(i - m, 0):i])
c.append(c[i - 1] + (a[max(i - m, 0):i] == b))
# print(c)
for i in range(q):
l, r = map(int, input().split())
if r - l >= m - 1:
print(max(c[r] - c[l + m - 2], 0))
else:
print(0)
| PYTHON3 |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | n,m,q = map(int, input().split())
s = input()
t =input()
res=""
if n>=m:
for i in range(n):
if (s[i:i+m]==t):
res+="1"
else:
res+="0"
for i in range(q):
c0,c1 = map(int, input().split())
if (m>n) or (c1-c0+1<m):
print (0)
else :
print (res[c0-1:c1-m+1].count("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 | n,m,q = map(int, input().split())
s = input()
t = input()
a = [0]*(n+1)
for i in range(n-m+1):
if s[i:i+m]==t: a[i] = 1
for i in range(n-1,-1,-1): a[i]+=a[i+1];
for k in range(q):
l, r = map(int, input().split())
l = l-1
r = r-m
res = a[l]-a[r+1] if l<=r else 0
print(res) | PYTHON3 |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import java.io.*;
import java.util.StringTokenizer;
public class Solution {
public static void main(String[] args) throws IOException {
FastScanner in = new FastScanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt();
int m = in.nextInt();
int q = in.nextInt();
String s = in.nextToken();
String t = in.nextToken();
int[] dp = new int[n + 1];
for (int i = 0; i < n; i++) {
if (checkSubstring(i , n, m, s, t)) dp[i]++;
dp[i + 1] += dp[i];
}
while (q-- > 0) {
int l = in.nextInt();
int r = in.nextInt();
int before;
int all;
if (l == 1) before = 0;
else before = dp[l - 2];
if (r<m) all=0;
else all=dp[r-m];
out.println(Math.max(0,all-before));
}
out.close();
}
private static boolean checkSubstring(int i, int n, int m, String s, String t) {
if (i + m > n) return false;
for (int k = 0; k < m; k++) {
if (s.charAt(i + k) != t.charAt(k)) return false;
}
return true;
}
private static class FastScanner {
BufferedReader br;
StringTokenizer stok;
FastScanner(InputStream is) {
this.br = new BufferedReader(new InputStreamReader(is));
}
String nextToken() throws IOException {
while (stok == null || !stok.hasMoreTokens()) {
String s = br.readLine();
if (s == null) return null;
stok = new StringTokenizer(s);
}
return stok.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
}
}
| 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 io, os
#input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
from sys import stdout
print = stdout.write
## reference from pajendoc
n, m, q = map(int, input().split())
s = str(input())
t = str(input())
ans = []
i = j = 0
while True:
x = s.find(t, i)
if x != -1:
for _ in range(x-i+1):
ans.append(j)
j += 1
i = x + 1
else:
ans += [j]*(n-i+2)
break
for _ in range(q):
l, r = map(int, input().split())
l -= 1
r -= m-1
print(str(ans[r] - ans[l]) if r > l else str(0))
print('\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 | import sys, math
from sys import stdin, stdout
rem = 10 ** 9 + 7
sys.setrecursionlimit(10 ** 6)
take = lambda: map(int, raw_input().split())
from bisect import bisect_right,bisect_left
n,m,q=take()
arr=list(raw_input())
new=list(raw_input())
left=[-1]
right=[-1]
for i in range(n-m+1):
#print arr[i:i+m]
if arr[i:i+m]==new:
left.append(i+1)
right.append(i+m)
right.append(10**5)
left.append(10**5)
#print left
#print right
for i in range(q):
a,b=take()
c=bisect_left(left,a)
d=bisect_right(right,b)
if d>=c:
print d-c
else:
print 0
| PYTHON |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | n,m,q=map(int,input().split())
s=input()
t=input()
x=0
dp1=[0 for i in range(n)]
while x<n:
if s[x-m+1:x+1]==t:
dp1[x]=1
else:
dp1[x]=0
x+=1
dp=[]
for i in range(n):
acum=0
dp.append([])
for j in range(i):
dp[-1].append(0)
for j in range(i,n):
if dp1[j]!=0 and j-m+1>=i:
acum+=dp1[j]
dp[-1].append(acum)
ans=""
for i in range(q):
l,r=map(int,input().split())
ans+=str(dp[l-1][r-1])+chr(10)
print(ans)
| PYTHON3 |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import java.util.Scanner;
public class Codeforces {
public static String slovo(String s, int pos, int dl) {
String rt = "";
for (int i = pos; i < pos + dl; i++) {
rt += s.charAt(i);
}
return rt;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt(), m = in.nextInt(), q = in.nextInt();
int[] a = new int[1111];
String s = in.next(), t = in.next();
for (int i = 0; i < n - m + 1; i++) {
String s1 = slovo(s, i, m);
if (s1.equals(t)) {
a[i]++;
}
}
for (int i = 1; i <= n; i++) {
a[i] += a[i-1];
}
for(int i = n; i >= 1; i--) {
a[i] = a[i-1];
}
a[0] = 0;
for (int p = 1; p <= q; p++) {
int l = in.nextInt(), r = in.nextInt();
if (r - m + 1 < 1) {
System.out.println(0);
} else {
System.out.println (( (a[r - m + 1] - a[l-1]) >= 0 ) ? (a[r - m + 1] - a[l-1]) : 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;
const int maxn = 1e3 + 10;
char str[maxn], s[maxn];
struct node {
char ch;
int val;
node() {}
node(char _ch, int _val) : ch(_ch), val(_val) {}
};
node stk[maxn];
int top;
int f[maxn];
int tr[maxn];
int len1, len2;
void update(int x, int v) {
while (x <= len1) {
tr[x] += v;
x += (x & (-x));
}
}
int get_sum(int x) {
int ans = 0;
while (x > 0) {
ans += tr[x];
x -= (x & (-x));
}
return ans;
}
int fail[maxn];
void getFail(char* P, int len) {
fail[0] = fail[1] = 0;
for (int i = 1; i < len; i++) {
int j = fail[i];
while (j && P[i] != P[j]) j = fail[j];
fail[i + 1] = (P[i] == P[j] ? j + 1 : 0);
}
}
int main() {
int m;
scanf("%d %d %d", &len1, &len2, &m);
scanf(" %s", str);
scanf(" %s", s);
getFail(s, len2);
int j = 0;
for (int i = 0; i < len1; i++) {
while (j && s[j] != str[i]) j = fail[j];
if (s[j] == str[i]) ++j;
if (j == len2) {
int x = i - len2 + 1;
update(x + 1, 1);
}
}
for (int i = 0; i < m; i++) {
int l, r;
scanf("%d %d", &l, &r);
if (r - l + 1 < len2) {
printf("0\n");
} else {
printf("%d\n", get_sum(r - len2 + 1) - get_sum(l - 1));
}
}
return 0;
}
| 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.IOException;
import java.io.InputStream;
public class B1016 {
public static void main(String[] args) throws IOException {
InputReader reader = new InputReader(System.in);
int N = reader.readInt();
int M = reader.readInt();
int Q = reader.readInt();
String S = reader.readString();
String T = reader.readString();
boolean[] match = new boolean[N];
for (int n=0; n<N; n++) {
match[n] = S.substring(n).startsWith(T);
}
StringBuilder output = new StringBuilder();
for (int q=0; q<Q; q++) {
int L = reader.readInt()-1;
int R = reader.readInt()-1;
R -= M-1;
int answer = 0;
for (int i=L; i<=R; i++) {
if (match[i]) answer++;
}
output.append(answer).append('\n');
}
System.out.println(output);
}
static final class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
private int read() throws IOException {
if (curChar >= numChars) {
curChar = 0;
numChars = stream.read(buf);
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public final int readInt() throws IOException {
return (int)readLong();
}
public final long readLong() throws IOException {
int c = read();
while (isSpaceChar(c)) {
c = read();
if (c == -1) throw new IOException();
}
boolean negative = false;
if (c == '-') {
negative = true;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return negative ? -res : res;
}
public final int[] readIntArray(int size) throws IOException {
int[] array = new int[size];
for (int i=0; i<size; i++) {
array[i] = readInt();
}
return array;
}
public final long[] readLongArray(int size) throws IOException {
long[] array = new long[size];
for (int i=0; i<size; i++) {
array[i] = readLong();
}
return array;
}
public final String readString() throws IOException {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.append((char)c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
}
| JAVA |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 1005, MAXM = 1005;
string T, P, tt;
int Next[MAXM], N, M, C;
void MakeNext(int M) {
int i = 0, j = -1;
Next[i] = -1;
while (i < M) {
if (j == -1 || P[i] == P[j])
Next[++i] = ++j;
else
j = Next[j];
}
}
int KMP(int pos, int N, int M) {
int i = pos, j = 0, ans = 0;
while (i < N) {
if (T[i] == P[j] || j == -1)
i++, j++;
else
j = Next[j];
if (j == M) {
ans++;
j = Next[j - 1];
i--;
}
}
return ans;
}
int main() {
int n, m, q;
cin >> n >> m >> q;
cin >> tt >> P;
int l, r;
while (q--) {
cin >> l >> r;
T = tt.substr(l - 1, r - l + 1);
N = r - l + 1;
M = m;
MakeNext(M);
printf("%d\n", KMP(0, N, M));
}
return 0;
}
| CPP |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(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, FastScanner in, PrintWriter out) {
int n = in.nextInt();
int m = in.nextInt();
String s = in.nextLine();
String t = in.nextLine();
int q = in.nextInt();
int[] a = new int[n + 1];
for (int i = 0; i < n; i++) {
int j = 0;
for (; j < m && i + j < n; j++) {
if (t.charAt(j) != s.charAt(i + j)) break;
}
if (j == m) a[i] = 1;
}
int[] b = new int[n + 1];
b[n] = n;
for (int i = n - 1; i >= 0; i--) {
b[i] = b[i + 1];
if (a[i] == 1) {
b[i] = i;
}
}
// out.println(Arrays.toString(b));
for (int i = 0; i < q; i++) {
int x = in.nextInt() - 1;
int y = in.nextInt() - 1;
int cnt = 0;
for (int j = b[x]; j < n && j <= y; j = b[j + 1]) {
if (j + t.length() - 1 <= y) cnt++;
}
out.println(cnt);
}
}
}
static class FastScanner {
private BufferedReader bufferedReader;
private StringTokenizer stringTokenizer;
public FastScanner(InputStream inputStream) {
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
}
public String next() {
while (stringTokenizer == null || !stringTokenizer.hasMoreElements()) {
try {
stringTokenizer = new StringTokenizer(bufferedReader.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return stringTokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public String nextLine() {
String ret = "";
try {
ret = bufferedReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return ret;
}
}
}
| JAVA |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, q;
cin >> n >> m >> q;
string s, t;
cin >> s >> t;
int ans[1000003] = {0};
for (int i = 0; i < n; i++) {
if (s.substr(i, m) == t) {
ans[i] = 1;
}
}
while (q--) {
int l, r, cnt = 0;
cin >> l >> r;
if (r - l + 1 < m) {
cout << 0 << endl;
} else {
l--;
r--;
for (int i = l; i <= r; i++) {
if (ans[i] == 1 && i + m - 1 <= r) cnt++;
}
cout << cnt << endl;
}
}
return 0;
}
| 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 | //6000 KB memory
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Main //main class
{
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;
}
}
public static void main(String args[]){
FastReader in=new FastReader();
int n=in.nextInt(),m=in.nextInt(),q=in.nextInt();
String s1=in.next(),s2=in.next();
if (m>n||!(s1.contains(s2))) {
for (int i = 0; i < q; i++) {
n=in.nextInt();
n=in.nextInt();
System.out.println("0");
}
}else{
int arr[]=new int[n+1];
for (int i = 1; i <= n; i++) {
if (i<=(n-m+1)) {
if (s1.substring(i-1, i+m-1).matches(s2)) {
arr[i]++;
}}
arr[i]+=arr[i-1];
}
for (int i = 0; i <q; i++) {
n=in.nextInt();
m=in.nextInt();
if (m-n+1<s2.length()) {
System.out.println("0");
}else
System.out.println(arr[m-s2.length()+1]-arr[n-1]);
}
}
}
} | JAVA |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
string a;
string b;
int ans[1000 + 7];
int main() {
int m, n, q;
cin >> m >> n >> q;
cin >> a >> b;
int tmp = 0;
for (int i = 0; i < m; i++) {
if (a[i] == b[0]) {
int cnt = 0;
for (int j = 0; j < n; j++) {
if (a[i + j] == b[j])
cnt++;
else
break;
}
if (cnt == n) tmp += 1;
}
ans[i + 1] = tmp;
}
for (int i = 0; i < q; i++) {
int x, y;
cin >> x >> y;
y = y - n + 1;
if (x > y)
cout << "0" << endl;
else
cout << ans[y] - ans[x - 1] << endl;
}
}
| CPP |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import java.util.*;
import java.io.*;
import java.lang.*;
import java.math.*;
public class B {
public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
// Scanner scan = new Scanner(System.in);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
// int n = Integer.parseInt(bf.readLine());
StringTokenizer st = new StringTokenizer(bf.readLine());
// int[] a = new int[n]; for(int i=0; i<n; i++) a[i] = Integer.parseInt(st.nextToken());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int q = Integer.parseInt(st.nextToken());
String s = bf.readLine();
String t = bf.readLine();
int MOD = 1000000009;
int base = 70709;
Random rand = new Random();
int[] hash_value = new int[26];
for(int i=0; i<26; i++) hash_value[i] = 2+rand.nextInt(MOD-4);
long[] hash = new long[n+1];
hash[0] = 0;
for(int i=1; i<=n; i++) {
hash[i] = (hash[i-1] + 1L*hash_value[(s.charAt(i-1)-'a')]*exp(base, i-1, MOD) % MOD) % MOD;
}
long desired = 0;
for(int i=1; i<=m; i++) {
desired = (desired + 1L*hash_value[(t.charAt(i-1)-'a')]*exp(base, i-1, MOD) % MOD)% MOD;
}
// out.println(desired);
int[] count = new int[n];
for(int i=0; i<=n-m; i++) {
long hash_Val = ((hash[i+m]-hash[i] + MOD) % MOD)*inv(exp(base, i, MOD), MOD) % MOD;
// out.println(hash_Val);
if(hash_Val == desired) count[i]++;
}
//out.println(Arrays.toString(count));
int[] pref_sum = new int[n+1];
pref_sum[0] = 0;
for(int i=1; i<n+1; i++) pref_sum[i] = pref_sum[i-1] + count[i-1];
for(int i=0; i<q; i++) {
st = new StringTokenizer(bf.readLine());
int l = Integer.parseInt(st.nextToken()) - 1;
int r = Integer.parseInt(st.nextToken()) - 1;
r = r-m+1+1;
l = l-1+1;
if((l <= r) && (0 <= l) && (r <= n))
out.println((pref_sum[r]-pref_sum[l]));
else
out.println(0);
}
// int n = scan.nextInt();
out.close(); System.exit(0);
}
public static int exp(int base, int e, int mod) {
if(e == 0) return 1;
if(e == 1) return base;
int val = exp(base, e/2, mod);
int ans = (int)(1L*val*val % mod);
if(e % 2 == 1)
ans = (int)(1L*ans*base % mod);
return ans;
}
public static int inv(int base, int mod) {
return exp(base, mod-2, mod);
}
}
| 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
n,m,q = [int(x) for x in stdin.readline().rstrip().split()]
s=stdin.readline().rstrip().split()[0]
t=stdin.readline().rstrip().split()[0]
temp=[]
for j in range (0, n-m+1):
if s[j:j+m]==t:
temp.append(1)
else:
temp.append(0)
for i in range(q):
query=[int(x) for x in stdin.readline().rstrip().split()]
if query[1]-m + 1<0:
print 0
else:
print sum(temp[query[0]-1:query[1]-m + 1])
| PYTHON |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import java.io.*;
import java.util.*;
public class CF1016B {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int q = Integer.parseInt(st.nextToken());
char[] ss = br.readLine().toCharArray();
char[] tt = br.readLine().toCharArray();
int[] kk = new int[n];
for (int i = 0; i <= n - m; i++) {
boolean match = true;
for (int j = 0; j < m; j++)
if (ss[i + j] != tt[j]) {
match = false;
break;
}
if (match)
kk[i + m - 1] = 1;
}
for (int i = 1; i < n; i++)
kk[i] += kk[i - 1];
while (q-- > 0) {
st = new StringTokenizer(br.readLine());
int l = Integer.parseInt(st.nextToken()) - 1;
int r = Integer.parseInt(st.nextToken()) - 1;
int l_ = l + m - 1;
int ans;
if (r < l_)
ans = 0;
else
ans = kk[r] - (l_ - 1 >= 0 ? kk[l_ - 1] : 0);
pw.println(ans);
}
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 | n,m,q = [int(x) for x in input().split()]
s = input()
t = input()
#precalculate all possible matches
cnt_lst = [0]
for i in range(len(s)):
val = 1 if s.startswith(t,i) else 0
cnt_lst.append(cnt_lst[-1] + val)
#print(cnt_lst)
for i in range(q):
l,r = [int(x) for x in input().split()]
x,y = l-1, r - len(t) + 1
if x < y:
print(cnt_lst[y] - cnt_lst[x])
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>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;
template <class T1, class T2>
inline void checkmin(T1 &x, T2 y) {
if (x > y) x = y;
}
template <class T1, class T2>
inline void checkmax(T1 &x, T2 y) {
if (x < y) x = y;
}
template <class T1>
inline void sort(T1 &arr) {
sort(arr.begin(), arr.end());
}
template <class T1>
inline void rsort(T1 &arr) {
sort(arr.rbegin(), arr.rend());
}
template <class T1>
inline void reverse(T1 &arr) {
reverse(arr.begin(), arr.end());
}
template <class T1>
inline void shuffle(T1 &arr) {
for (int i = -int(arr.size()); i < int(arr.size()); ++i)
swap(arr[rand() % arr.size()], arr[rand() % arr.size()]);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << fixed << setprecision(20);
srand(time(NULL));
long long n, m, q;
cin >> n >> m >> q;
string second, t;
cin >> second >> t;
vector<int> cnt(second.size());
for (int i = 0; i <= int(second.size()) - int(t.size()); ++i) {
string news = "";
for (int j = i; j < i + t.size(); ++j) news += second[j];
if (news == t) ++cnt[i];
}
for (int i = 1; i < cnt.size(); ++i) cnt[i] += cnt[i - 1];
for (int i = 0; i < q; ++i) {
int u, v;
cin >> u >> v;
--u;
v -= int(t.size());
if (v < u)
cout << 0 << '\n';
else {
if (u == 0)
cout << cnt[v] << '\n';
else
cout << cnt[v] - cnt[u - 1] << '\n';
}
}
return 0;
}
| 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 MaxN = 100005;
int BIT[MaxN];
void update(int pos) {
int i, j, p, q;
for (i = pos; i < MaxN; i += i & (-i)) {
BIT[i] += 1;
}
}
int query(int pos) {
int i, j, p = 0, q;
for (i = pos; i > 0; i -= i & (-i)) {
p += BIT[i];
}
return p;
}
int main() {
cin.tie(0), ios_base::sync_with_stdio(0);
int N, M, K, i, j, p, q;
bool proof;
cin >> N >> M >> K;
string a, b;
cin >> a >> b;
for (i = 0; i <= N - M; i++) {
proof = 1;
for (j = i; j < i + M; j++) {
if (a[j] != b[j - i]) {
proof = 0;
continue;
}
}
if (proof) {
update(i + 1);
}
}
for (i = 0; i < K; i++) {
cin >> p >> q;
if (q - M - p + 1 < 0)
cout << "0\n";
else
cout << query(q - M + 1) - query(p - 1) << "\n";
}
}
| CPP |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
char s[2000], t[2000];
int f[2000];
int main() {
int n, m, q;
scanf("%d%d%d", &n, &m, &q);
scanf("%s", s + 1);
scanf("%s", t + 1);
for (int i = 1; i <= n - m + 1; i++) {
int flag = 1;
for (int j = 1; j <= m; j++)
if (s[i + j - 1] != t[j]) {
flag = 0;
break;
}
f[i] = f[i - 1] + flag;
}
for (int i = 1; i <= q; i++) {
int l, r;
scanf("%d%d", &l, &r);
if (r - l + 1 < m)
printf("0\n");
else
printf("%d\n", f[r - m + 1] - f[l - 1]);
}
}
| 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
range = xrange
input = raw_input
n,m,q = [int(x) for x in input().split()]
s = input()
t = input()
is_start = [False]*n
for i in range(n):
j = 0
while j<m and i+j<n and s[i+j]==t[j]:
j+=1
if j==m:
is_start[i]=True
A = [0]
for val in is_start:
if val:
A.append(A[-1]+1)
else:
A.append(A[-1])
inp = [int(x) for line in sys.stdin for x in line.split()]
out = []
for i in range(q):
l,r = inp[2*i],inp[2*i+1]
l-=1
r-=m-1
if l<r:
out.append(str(A[r]-A[l]))
else:
out.append('0')
print '\n'.join(out)
| PYTHON |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
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);
Task solver = new Task();
solver.solve(1, in, out);
out.close();
}
static class Task {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int m = in.nextInt();
int q = in.nextInt();
String s = in.next();
String t = in.next();
List<Integer> occurs = new ArrayList<>();
for (int i = 0; i + t.length() <= s.length(); i++) {
int ind = s.indexOf(t, i);
if (ind != -1) {
occurs.add(ind);
i = ind;
} else {
break;
}
}
if (occurs.isEmpty()) {
for (int i = 0; i < q; i++) {
out.println(0);
}
return;
}
for (int i = 0; i < q; i++) {
int l = in.nextInt() - 1;
int r = in.nextInt() - 1;
int count = 0;
for (int j = 0; j < occurs.size(); j++) {
if (occurs.get(j) < l) {
continue;
} else if (occurs.get(j) + t.length() - 1 <= r) {
count++;
} else if (occurs.get(j) + t.length() - 1 > r) {
break;
}
}
/*
* for (int j = l; j <= r - t.length() + 1; j++) { if (occurs.contains(j)) { count++; } }
*/
out.println(count);
}
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public Long nextLong() {
return Long.parseLong(next());
}
public 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=map(int,raw_input().split())
s=raw_input()
t=raw_input()
pre=[0]*(n+1)
for i in xrange(n-m+1):
j=0
k=i
pre[i+1]=pre[i]
while(j<m):
if t[j]==s[k]:
j+=1
k+=1
else: break
if j==m: pre[i+1]+=1
for i in xrange(q):
l,r=map(int,raw_input().split())
if r-l+1<m:print 0
else: print pre[r-m+1]-pre[l-1]
| PYTHON |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | n,m,q=map(int,input().split())
s=input()
s1=input()
st=[]
en=[]
l=[]
for i in range(n):
st.append(0)
en.append(0)
for i in range(n):
if s[i:i+len(s1)] ==s1:
l.append(1)
st[i]=1
en[i+len(s1)-1]=1
else:
l.append(0)
sums=0
sume=0
sa=[]
se=[]
a=[]
for i in range(n):
sums=sums+st[i]
sume=sume+en[i]
sa.append(sums)
se.append(sume)
#print(sa,se)
for i in range(q):
a1,b1=map(int,input().split())
#print(se[b1-1],sa[a1-2],b1-1)
if(a1==1):
print(se[b1-1]-0)
else:
if(se[b1-1]-sa[a1-2]<0):
print(0)
else:
print(se[b1-1]-sa[a1-2])
| PYTHON3 |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
MyScan in = new MyScan(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, MyScan in, PrintWriter out) {
int n = in.nextInt();
int m = in.nextInt();
int q = in.nextInt();
String t1 = in.next();
String t2 = in.next();
int[] ans = new int[Math.max(n - m + 1, 0)];
for (int l = 0; l < n - m + 1; l++) {
ans[l] = t2.equals(t1.substring(l, l + t2.length())) ? 1 : 0;
}
while (q-- > 0) {
int fr = in.nextInt() - 1;
int to = in.nextInt() - t2.length();
int r = 0;
for (int s = fr; s <= to; s++) {
r += ans[s];
}
out.println(r);
}
}
}
static class MyScan {
private final InputStream in;
private byte[] inbuf = new byte[1024];
public int lenbuf = 0;
public int ptrbuf = 0;
public MyScan(InputStream in) {
this.in = in;
}
private int readByte() {
if (lenbuf == -1) throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = in.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0) return -1;
}
return inbuf[ptrbuf++];
}
public boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
public int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b)) ;
return b;
}
public String next() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public int nextInt() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
}
}
| 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] = input().split()
n=int(n)
m=int(m)
q=int(q)
s=input()
t=input()
a=[0 for i in range(n + 5)]
for i in range(0,n-m+1):
if s[i:i+m]==t:
a[i + 1]=1
for i in range(1,n + 3):
a[i]+=a[i-1]
for i in range(q):
[l,r]=input().split()
l=int(l)
r=int(r)
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 |
def mi():
return map(int, input().split())
n,m,q = mi()
s = list(input())
t = list(input())
pre1 = [0]*n
pre2 = [0]*n
if n>=m:
for i in range(n-m+1):
if (s[i:i+m]==t):
pre1[i+m-1] = 1
pre2[i] = 1
for i in range(1,n):
pre1[i]+=pre1[i-1]
pre2[i]+=pre2[i-1]
pre1.insert(0,0)
pre2.insert(0,0)
while q:
q-=1
l,r = mi()
s1, s2 = 0,0
#for i in range(l, r+1):
#s1+=pre1[i]
#s2+=pre2[i]
if m>n:
print (0)
continue
if r<m or r-l+1<m or n-l+1<m:
print (0)
continue
s1 = pre1[r]-pre1[l-1+m-1]
s2 = pre2[r-m+1]-pre2[l-1]
if s1>0 and s2>0:
print (min(s1,s2))
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 sys
def get_array(): return list(map(int, sys.stdin.readline().split()))
def get_ints(): return map(int, sys.stdin.readline().split())
def input(): return sys.stdin.readline().strip('\n')
n , m , q = get_ints()
s = input()
p = input()
ans = []
i = 0
count = 0
while True:
si = s.find(p,i)
if si != -1:
ans += [count]*(si-i+1)
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 | from collections import Counter
def main():
n, m, q = [int(c) for c in input().split()]
s = input()
t = input()
ok = []
pr = [0]
for i in range(0, n):
ok.append(1 if t == s[i:i+m] else 0)
pr.append(pr[-1] + ok[-1])
# print('[' + ', '.join(list(s)) + ']')
# print(ok)
# print(pr)
res = []
for i in range(q):
li, ri = [int(c) for c in input().split()]
res.append(0 if ri-li+1 < m else pr[ri-m+1] - pr[li-1])
for e in res:
print(e)
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 | def main():
n, m, q = map(int, input().split())
s, t = input(), input()
start, ps = [0] * n, [0] * (n+1)
for i in range(n):
curr, left = 0, i
while left < n and s[left] == t[curr]:
curr += 1
left += 1
if curr == m:
start[i] = 1
break
for i in range(n):
ps[i+1] = ps[i] + start[i]
for i in range(q):
l, r = map(int, input().split())
print(0) if r - l + 1 < m else print(ps[r-m+1]-ps[l-1])
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 | R=lambda:map(int,input().split())
n,m,q=R()
s,t=input(),input()
a=[0]
b=0
for i in range(n):
if s[i:i+m]==t:b+=1
a+=[b]
for _ in[0]*q:l,r=R();print(a[max(l-1,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 | def mmhaxor(n,m,q,s,t):
a = [0]*(10000)
for i in range(n):
a[i+2] = a[i+1]+(s[i:i+m]==t)
for i in range(q):
ans = 0
l,r = map(int,input().split())
print(a[max(r-m+2,l)]-a[l])
n,m,q = map(int,input().split())
s =input()
t =input()
mmhaxor(n,m,q,s,t) | PYTHON3 |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import sys
n,m,q = map(int,raw_input().split())
s = list(raw_input())
t = list(raw_input())
if n<m:
for i in range(q):
print 0
sys.exit()
arr = [0 for x in range(n+1)]
for i in range(n-m+1):
if s[i:i+m]==t:
arr[i+1] = 1
arr[i+1] += arr[i]
for i in range(n-m+1,n):
arr[i+1] += arr[i]
for i in range(q):
l,r = map(int,raw_input().split())
if r < m:
print 0
continue
print max(0,arr[r-m+1]-arr[l-1])
'''
5 1 5
aaaaa
a
1 5
''' | 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()
dp = [[0 for i in range(n)] for i in range(n)]
for left in range(n):
for right in range(left + m - 1, n):
if s[right - m + 1:right+ 1] == t:
dp[left][right] = dp[left][right - 1] + 1
else:
dp[left][right] = dp[left][right - 1]
for i in range(q):
left, right = map(int, input().split())
print(dp[left- 1][right - 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.util.*;
import java.util.regex.*;
import java.io.*;
public class Codeforces{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// BufferedReader br = new BufferedReader(new FileReader(new File("D:\\Shivang\\Java\\JavaFile.txt")));
String[] s1 = input(br).split(" ");
int n = Integer.parseInt(s1[0]);
int m = Integer.parseInt(s1[1]);
int q = Integer.parseInt(s1[2]);
String sup = input(br);
String sub = input(br);
NavigableSet<Integer> start = new TreeSet<>();
NavigableSet<Integer> end = new TreeSet<>();
HashMap<Integer, Integer> startHashMap = new HashMap<>();
HashMap<Integer, Integer> endHashMap = new HashMap<>();
int curIndex = 0;
for(int i = 0; i <= n - m; i++){
if(sub.equals(sup.substring(i, i + m))){
start.add(i + 1);
end.add(i + m);
startHashMap.put(i + 1, curIndex);
endHashMap.put(i + m, curIndex);
curIndex++;
}
}
// System.out.println(start);
// System.out.println(end);
// System.out.println(startHashMap);
// System.out.println(endHashMap);
while(q-- > 0){
String[] s2 = input(br).split(" ");
int left = Integer.parseInt(s2[0]);
int right = Integer.parseInt(s2[1]);
int curStart = (start.ceiling(left) == null)? -1: start.ceiling(left);
int curEnd = (end.floor(right) == null)? -1: end.floor(right);
// System.out.println(curStart + " " + curEnd);
if(curEnd == -1 || curStart == -1){
System.out.println(0);
continue;
}
int ansS = startHashMap.get(curStart);
int ansE = endHashMap.get(curEnd);
int finalAns = ansE - ansS + 1;
if(finalAns < 0) finalAns = 0;
System.out.println(finalAns);
}
br.close();
}
private static String input(BufferedReader br) throws IOException{
// BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
while((line = br.readLine()) != null){
if(line.trim().length() == 0) continue;
return line;
}
return "";
}
} | 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.*;
/**
* @author baito
*/
@SuppressWarnings("unchecked")
public class Main
{
static StringBuilder sb = new StringBuilder();
static FastScanner sc = new FastScanner(System.in);
static int INF = 123456789;
static int MINF = -123456789;
static long LINF = 123456789123456789L;
static long MLINF = -123456789123456789L;
static long MOD = 1000000007;
static int[] y4 = {0, 1, 0, -1};
static int[] x4 = {1, 0, -1, 0};
static int[] y8 = {0, 1, 0, -1, -1, 1, 1, -1};
static int[] x8 = {1, 0, -1, 0, 1, -1, 1, -1};
static long[] Fa;//factorial
static boolean[] isPrime;
static int[] primes;
static char[][] map;
static int N, M, K;
static long[] A;
public static void main(String[] args)
{
//longを忘れるなオーバーフローするぞ
int N = sc.nextInt();
int M = sc.nextInt();
int Q = sc.nextInt();
String s = sc.next();
String t = sc.next();
int[] srui = new int[N];
int[] erui = new int[N];
for (int i = 0; i + M <= N; i++)
{
if (s.substring(i, i + M).equals(t))
{
srui[i]++;
erui[i + M-1]++;
}
}
for (int i = 1; i < N; i++)
{
srui[i] += srui[i - 1];
erui[i] += erui[i - 1];
}
for (int q = 0; q < Q; q++)
{
int l = sc.nextInt() - 1;
int r = sc.nextInt() - 1;
long res = upper0(erui[r] - (l > 0 ? srui[l - 1] : 0));
if (r - l + 1 < M)
{
System.out.println("0");
continue;
}
System.out.println(res);
}
}
public static long sqrt(long v)
{
long res = (long) Math.sqrt(v);
while (res * res > v) res--;
return res;
}
public static int upper0(int a)
{
if (a < 0) return 0;
return a;
}
public static long upper0(long a)
{
if (a < 0) return 0;
return a;
}
public static Integer[] toIntegerArray(int[] ar)
{
Integer[] res = new Integer[ar.length];
for (int i = 0; i < ar.length; i++)
{
res[i] = ar[i];
}
return res;
}
//k個の次の組み合わせをビットで返す 大きさに上限はない 110110 -> 111001
public static int nextCombSizeK(int comb, int k)
{
int x = comb & -comb; //最下位の1
int y = comb + x; //連続した下の1を繰り上がらせる
return ((comb & ~y) / x >> 1) | y;
}
public static int keta(long num)
{
int res = 0;
while (num > 0)
{
num /= 10;
res++;
}
return res;
}
public static long getHashKey(int a, int b)
{
return (long) a << 32 | b;
}
public static boolean isOutofIndex(int x, int y)
{
if (x < 0 || y < 0) return true;
if (map[0].length <= x || map.length <= y) return true;
return false;
}
public static void setPrimes()
{
int n = 100001;
isPrime = new boolean[n];
List<Integer> prs = new ArrayList<>();
Arrays.fill(isPrime, true);
isPrime[0] = isPrime[1] = false;
for (int i = 2; i * i <= n; i++)
{
if (!isPrime[i]) continue;
prs.add(i);
for (int j = i * 2; j < n; j += i)
{
isPrime[j] = false;
}
}
primes = new int[prs.size()];
for (int i = 0; i < prs.size(); i++)
primes[i] = prs.get(i);
}
public static void revSort(int[] a)
{
Arrays.sort(a);
reverse(a);
}
public static void revSort(long[] a)
{
Arrays.sort(a);
reverse(a);
}
public static int[][] copy(int[][] ar)
{
int[][] nr = new int[ar.length][ar[0].length];
for (int i = 0; i < ar.length; i++)
for (int j = 0; j < ar[0].length; j++)
nr[i][j] = ar[i][j];
return nr;
}
/**
* <h1>指定した値以上の先頭のインデクスを返す</h1>
* <p>配列要素が0のときは、0が返る。</p>
*
* @return<b>int</b> : 探索した値以上で、先頭になるインデクス
* 値が無ければ、挿入できる最小のインデックス
*/
public static int lowerBound(final int[] arr, final int value)
{
int low = 0;
int high = arr.length;
int mid;
while (low < high)
{
mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策)
if (arr[mid] < value)
{
low = mid + 1;
}
else
{
high = mid;
}
}
return low;
}
/**
* <h1>指定した値より大きい先頭のインデクスを返す</h1>
* <p>配列要素が0のときは、0が返る。</p>
*
* @return<b>int</b> : 探索した値より上で、先頭になるインデクス
* 値が無ければ、挿入できる最小のインデックス
*/
public static int upperBound(final int[] arr, final int value)
{
int low = 0;
int high = arr.length;
int mid;
while (low < high)
{
mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策)
if (arr[mid] <= value)
{
low = mid + 1;
}
else
{
high = mid;
}
}
return low;
}
/**
* <h1>指定した値以上の先頭のインデクスを返す</h1>
* <p>配列要素が0のときは、0が返る。</p>
*
* @return<b>int</b> : 探索した値以上で、先頭になるインデクス
* 値がなければ挿入できる最小のインデックス
*/
public static long lowerBound(final long[] arr, final long value)
{
int low = 0;
int high = arr.length;
int mid;
while (low < high)
{
mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策)
if (arr[mid] < value)
{
low = mid + 1;
}
else
{
high = mid;
}
}
return low;
}
/**
* <h1>指定した値より大きい先頭のインデクスを返す</h1>
* <p>配列要素が0のときは、0が返る。</p>
*
* @return<b>int</b> : 探索した値より上で、先頭になるインデクス
* 値がなければ挿入できる最小のインデックス
*/
public static long upperBound(final long[] arr, final long value)
{
int low = 0;
int high = arr.length;
int mid;
while (low < high)
{
mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策)
if (arr[mid] <= value)
{
low = mid + 1;
}
else
{
high = mid;
}
}
return low;
}
//次の順列に書き換える、最大値ならfalseを返す
public static boolean nextPermutation(int A[])
{
int len = A.length;
int pos = len - 2;
for (; pos >= 0; pos--)
{
if (A[pos] < A[pos + 1]) break;
}
if (pos == -1) return false;
//posより大きい最小の数を二分探索
int ok = pos + 1;
int ng = len;
while (Math.abs(ng - ok) > 1)
{
int mid = (ok + ng) / 2;
if (A[mid] > A[pos]) ok = mid;
else ng = mid;
}
swap(A, pos, ok);
reverse(A, pos + 1, len - 1);
return true;
}
//次の順列に書き換える、最小値ならfalseを返す
public static boolean prevPermutation(int A[])
{
int len = A.length;
int pos = len - 2;
for (; pos >= 0; pos--)
{
if (A[pos] > A[pos + 1]) break;
}
if (pos == -1) return false;
//posより小さい最大の数を二分探索
int ok = pos + 1;
int ng = len;
while (Math.abs(ng - ok) > 1)
{
int mid = (ok + ng) / 2;
if (A[mid] < A[pos]) ok = mid;
else ng = mid;
}
swap(A, pos, ok);
reverse(A, pos + 1, len - 1);
return true;
}
//↓nCrをmod計算するために必要。 ***factorial(N)を呼ぶ必要がある***
static long ncr(int n, int r)
{
if (n < r) return 0;
else if (r == 0) return 1;
factorial(n);
return Fa[n] / (Fa[n - r] * Fa[r]);
}
static long ncr2(int a, int b)
{
if (b == 0) return 1;
else if (a < b) return 0;
long res = 1;
for (int i = 0; i < b; i++)
{
res *= a - i;
res /= i + 1;
}
return res;
}
static long ncrdp(int n, int r)
{
if (n < r) return 0;
long[][] dp = new long[n + 1][r + 1];
for (int ni = 0; ni < n + 1; ni++)
{
dp[ni][0] = dp[ni][ni] = 1;
for (int ri = 1; ri < ni; ri++)
{
dp[ni][ri] = dp[ni - 1][ri - 1] + dp[ni - 1][ri];
}
}
return dp[n][r];
}
static long modNcr(int n, int r)
{
if (n < r) return 0;
long result = Fa[n];
result = result * modInv(Fa[n - r]) % MOD;
result = result * modInv(Fa[r]) % MOD;
return result;
}
public static long modSum(long... lar)
{
long res = 0;
for (long l : lar)
res = (res + l % MOD) % MOD;
if (res < 0) res += MOD;
res %= MOD;
return res;
}
public static long modDiff(long a, long b)
{
long res = a % MOD - b % MOD;
if (res < 0) res += MOD;
res %= MOD;
return res;
}
public static long modMul(long... lar)
{
long res = 1;
for (long l : lar)
res = (res * l % MOD) % MOD;
if (res < 0) res += MOD;
res %= MOD;
return res;
}
public static long modDiv(long a, long b)
{
long x = a % MOD;
long y = b % MOD;
long res = (x * modInv(y)) % MOD;
return res;
}
static long modInv(long n)
{
return modPow(n, MOD - 2);
}
static void factorial(int n)
{
Fa = new long[n + 1];
Fa[0] = Fa[1] = 1;
for (int i = 2; i <= n; i++)
{
Fa[i] = (Fa[i - 1] * i) % MOD;
}
}
static long modPow(long x, long n)
{
long res = 1L;
while (n > 0)
{
if ((n & 1) == 1)
{
res = res * x % MOD;
}
x = x * x % MOD;
n >>= 1;
}
return res;
}
//↑nCrをmod計算するために必要
static int gcd(int n, int r)
{
return r == 0 ? n : gcd(r, n % r);
}
static long gcd(long n, long r)
{
return r == 0 ? n : gcd(r, n % r);
}
static <T> void swap(T[] x, int i, int j)
{
T t = x[i];
x[i] = x[j];
x[j] = t;
}
static void swap(int[] x, int i, int j)
{
int t = x[i];
x[i] = x[j];
x[j] = t;
}
public static void reverse(int[] x)
{
int l = 0;
int r = x.length - 1;
while (l < r)
{
int temp = x[l];
x[l] = x[r];
x[r] = temp;
l++;
r--;
}
}
public static void reverse(long[] x)
{
int l = 0;
int r = x.length - 1;
while (l < r)
{
long temp = x[l];
x[l] = x[r];
x[r] = temp;
l++;
r--;
}
}
public static void reverse(char[] x)
{
int l = 0;
int r = x.length - 1;
while (l < r)
{
char temp = x[l];
x[l] = x[r];
x[r] = temp;
l++;
r--;
}
}
public static void reverse(int[] x, int s, int e)
{
int l = s;
int r = e;
while (l < r)
{
int temp = x[l];
x[l] = x[r];
x[r] = temp;
l++;
r--;
}
}
static int length(int a)
{
int cou = 0;
while (a != 0)
{
a /= 10;
cou++;
}
return cou;
}
static int length(long a)
{
int cou = 0;
while (a != 0)
{
a /= 10;
cou++;
}
return cou;
}
static int cou(boolean[] a)
{
int res = 0;
for (boolean b : a)
{
if (b) res++;
}
return res;
}
static int cou(String s, char c)
{
int res = 0;
for (char ci : s.toCharArray())
{
if (ci == c) res++;
}
return res;
}
static int countC2(char[][] a, char c)
{
int co = 0;
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
if (a[i][j] == c) co++;
return co;
}
static int countI(int[] a, int key)
{
int co = 0;
for (int i = 0; i < a.length; i++)
if (a[i] == key) co++;
return co;
}
static int countI(int[][] a, int key)
{
int co = 0;
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
if (a[i][j] == key) co++;
return co;
}
static void fill(int[][] a, int v)
{
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
a[i][j] = v;
}
static void fill(long[][] a, long v)
{
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
a[i][j] = v;
}
static void fill(int[][][] a, int v)
{
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
for (int k = 0; k < a[0][0].length; k++)
a[i][j][k] = v;
}
static int max(int... a)
{
int res = Integer.MIN_VALUE;
for (int i : a)
{
res = Math.max(res, i);
}
return res;
}
static long min(long... a)
{
long res = Long.MAX_VALUE;
for (long i : a)
{
res = Math.min(res, i);
}
return res;
}
static int max(int[][] ar)
{
int res = Integer.MIN_VALUE;
for (int i[] : ar)
res = Math.max(res, max(i));
return res;
}
static int min(int... a)
{
int res = Integer.MAX_VALUE;
for (int i : a)
{
res = Math.min(res, i);
}
return res;
}
static int min(int[][] ar)
{
int res = Integer.MAX_VALUE;
for (int i[] : ar)
res = Math.min(res, min(i));
return res;
}
static int sum(int[] a)
{
int cou = 0;
for (int i : a)
cou += i;
return cou;
}
static long sum(long[] a)
{
long cou = 0;
for (long i : a)
cou += i;
return cou;
}
static int abs(int a)
{
return Math.abs(a);
}
static class FastScanner
{
private BufferedReader reader = null;
private StringTokenizer tokenizer = null;
public FastScanner(InputStream in)
{
reader = new BufferedReader(new InputStreamReader(in));
tokenizer = null;
}
public String next()
{
if (tokenizer == null || !tokenizer.hasMoreTokens())
{
try
{
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e)
{
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
/*public String nextChar(){
return (char)next()[0];
}*/
public String nextLine()
{
if (tokenizer == null || !tokenizer.hasMoreTokens())
{
try
{
return reader.readLine();
} catch (IOException e)
{
throw new RuntimeException(e);
}
}
return tokenizer.nextToken("\n");
}
public long nextLong()
{
return Long.parseLong(next());
}
public int nextInt()
{
return Integer.parseInt(next());
}
public double nextDouble()
{
return Double.parseDouble(next());
}
public int[] nextIntArray(int n)
{
int[] a = new int[n];
for (int i = 0; i < n; i++)
{
a[i] = nextInt();
}
return a;
}
public int[] nextIntArrayOneIndex(int n)
{
int[] a = new int[n + 1];
for (int i = 1; i < n + 1; i++)
{
a[i] = nextInt();
}
return a;
}
public int[] nextIntArrayDec(int n)
{
int[] a = new int[n];
for (int i = 0; i < n; i++)
{
a[i] = nextInt() - 1;
}
return a;
}
public int[][] nextIntArray2(int h, int w)
{
int[][] a = new int[h][w];
for (int hi = 0; hi < h; hi++)
{
for (int wi = 0; wi < w; wi++)
{
a[hi][wi] = nextInt();
}
}
return a;
}
public int[][] nextIntArray2Dec(int h, int w)
{
int[][] a = new int[h][w];
for (int hi = 0; hi < h; hi++)
{
for (int wi = 0; wi < w; wi++)
{
a[hi][wi] = nextInt() - 1;
}
}
return a;
}
//複数の配列を受け取る
public void nextIntArrays2ar(int[] a, int[] b)
{
for (int i = 0; i < a.length; i++)
{
a[i] = sc.nextInt();
b[i] = sc.nextInt();
}
}
public void nextIntArrays2arDec(int[] a, int[] b)
{
for (int i = 0; i < a.length; i++)
{
a[i] = sc.nextInt() - 1;
b[i] = sc.nextInt() - 1;
}
}
//複数の配列を受け取る
public void nextIntArrays3ar(int[] a, int[] b, int[] c)
{
for (int i = 0; i < a.length; i++)
{
a[i] = sc.nextInt();
b[i] = sc.nextInt();
c[i] = sc.nextInt();
}
}
//複数の配列を受け取る
public void nextIntArrays3arDecLeft2(int[] a, int[] b, int[] c)
{
for (int i = 0; i < a.length; i++)
{
a[i] = sc.nextInt() - 1;
b[i] = sc.nextInt() - 1;
c[i] = sc.nextInt();
}
}
public Integer[] nextIntegerArray(int n)
{
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
{
a[i] = nextInt();
}
return a;
}
public char[] nextCharArray(int n)
{
char[] a = next().toCharArray();
return a;
}
public char[][] nextCharArray2(int h, int w)
{
char[][] a = new char[h][w];
for (int i = 0; i < h; i++)
{
a[i] = next().toCharArray();
}
return a;
}
//スペースが入っている場合
public char[][] nextCharArray2s(int h, int w)
{
char[][] a = new char[h][w];
for (int i = 0; i < h; i++)
{
a[i] = nextLine().replace(" ", "").toCharArray();
}
return a;
}
public char[][] nextWrapCharArray2(int h, int w, char c)
{
char[][] a = new char[h + 2][w + 2];
//char c = '*';
int i;
for (i = 0; i < w + 2; i++)
a[0][i] = c;
for (i = 1; i < h + 1; i++)
{
a[i] = (c + next() + c).toCharArray();
}
for (i = 0; i < w + 2; i++)
a[h + 1][i] = c;
return a;
}
//スペースが入ってる時用
public char[][] nextWrapCharArray2s(int h, int w, char c)
{
char[][] a = new char[h + 2][w + 2];
//char c = '*';
int i;
for (i = 0; i < w + 2; i++)
a[0][i] = c;
for (i = 1; i < h + 1; i++)
{
a[i] = (c + nextLine().replace(" ", "") + c).toCharArray();
}
for (i = 0; i < w + 2; i++)
a[h + 1][i] = c;
return a;
}
public long[] nextLongArray(int n)
{
long[] a = new long[n];
for (int i = 0; i < n; i++)
{
a[i] = nextLong();
}
return a;
}
public long[][] nextLongArray2(int h, int w)
{
long[][] a = new long[h][w];
for (int hi = 0; hi < h; hi++)
{
for (int wi = 0; wi < w; wi++)
{
a[hi][wi] = nextLong();
}
}
return a;
}
}
}
| JAVA |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | n, m, q = map(int, input().split())
s = input()
t = input()
gas = ['LOL']
otv = []
c = m - 2
for i in range(n):
if s[i:i+m] == t:
gas.append(1)
else:
gas.append(0)
for i in range(q):
l, r = map(int, input().split())
if r - m >= 0:
otv.append(str(sum(gas[l:r - c])))
else:
otv.append('0')
print('\n'.join(otv)) | PYTHON3 |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | n, m, q = map(int, input().split())
s = input()
t = input()
pr = [0] * (1000 + 7)
ok = [0] * (1000 + 7)
pr[0] = 0
for i in range(n - m + 1):
fl = 1
for j in range(m):
if(s[i+j] != t[j]):
fl = 0
ok[i] = fl
pr[i+1] = pr[i] + ok[i]
for i in range(max(0, n - m + 1), n):
pr[i+1] = pr[i]
while q > 0:
l, r = map(int, input().split())
l -= 1
r -= m - 1
if(r >= l):
print(pr[r] - pr[l])
else:print(0)
q -= 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 | Input = lambda: map(int, input().split())
n, m, q = Input()
s = input()
t = input()
pre = [ 0 for i in range(n + 5)]
for i in range(0, n - m + 1):
if s[i:i+m] == t:
pre[i + 1] = 1
for i in range(1, n + 3):
pre[i] += pre[i - 1]
for i in range(q):
l, r = Input()
if r - l + 1 < m:
print(0)
continue
print(pre[r - m + 1] - pre[l - 1])
| PYTHON3 |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import java.util.Scanner;
public class SegmentOccurrence {
static class Query {
int l;
int r;
public Query(int l, int r) {
this.l = l;
this.r = r;
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int q = in.nextInt();
String s = in.next();
String t = in.next();
Query[] queries = new Query[q];
for (int i = 0; i < q; i++) {
queries[i] = new Query(in.nextInt(), in.nextInt());
}
int[] posCount = new int[s.length()];
for (int start = 0; start + t.length() <= s.length(); start++) {
boolean allMatch = true;
for (int i = 0; i < t.length(); i++) {
if (s.charAt(start + i) != t.charAt(i)) {
allMatch = false;
break;
}
}
if (allMatch) {
posCount[start] = 1;
}
}
for (Query query : queries) {
int count = 0;
for (int i = query.l - 1; i + t.length() <= query.r; i++) {
count += posCount[i];
}
System.out.println(count);
}
}
}
| JAVA |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int i, j, n, m, l, r, q, nr;
string s;
string t;
cin >> n >> m >> q;
cin >> s >> t;
int cnt[1001] = {0};
for (int i = 0; i <= n - m; i++) {
if (s.substr(i, m) == t) {
cnt[i] = 1;
}
}
l = 0;
r = 0;
for (i = 1; i <= q; i++) {
cin >> l >> r;
nr = 0;
for (j = l - 1; j <= r - m; j++) {
if (cnt[j]) nr++;
}
cout << nr << '\n';
}
return 0;
}
| 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 bisect
n,m,q=map(int,input().split())
s=input()
t=input()
def find_substring(substring, string):
indices = []
index = -1 # Begin at -1 so
while True:
index = string.find(substring, index + 1)
if index == -1:
break
indices.append(index+1)
return indices
l=find_substring(t,s)
ans=[]
d=0
f=0
while(q):
a,b=map(int,input().split())
d=0
if b-a+1>=len(t):
b=b-len(t)+1
d=bisect.bisect_left(l,a)
f=bisect.bisect_right(l,b)
ans.append(str(f-d))
else:
ans.append("0")
q=q-1
print("\n".join(ans))
| PYTHON3 |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
long long const MOD = 1e9 + 7;
long long const N = 1e3 + 10;
long long ara[N + 1];
long long bra[N + 1];
int main() {
(ios_base::sync_with_stdio(false), cin.tie(NULL));
long long n, m, q;
cin >> n >> m >> q;
string str, s;
cin >> str >> s;
for (long long i = 0; i < n - m + 1; i++) {
long long j = 0, pre = i;
while (j < m) {
if (str[i] == s[j]) {
i++;
j++;
} else
break;
}
if (j == m) {
bra[pre + 1]++;
i--;
bra[i]--;
ara[pre + 1]++;
}
i = pre;
}
for (long long i = 1; i <= n + 10; i++) {
ara[i] += ara[i - 1];
bra[i] += bra[i - 1];
}
while (q--) {
long long a, b;
cin >> a >> b;
b = b - m + 1;
cout << max(0ll, ara[max(b, 0ll)] - ara[a - 1]) << endl;
}
}
| 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>
#pragma GCC optimize("O3")
using namespace std;
const long long MAXN = 1e3 + 10;
long long n, m, q;
string a, b;
long long pref[MAXN];
inline void precalc() {
for (long long i = 0; i < n - m + 1; ++i) {
bool flag = true;
for (long long j = i; j < i + m; ++j) {
if (a[j] != b[j - i]) {
flag = false;
break;
}
}
if (i == 0) {
pref[i] = flag;
continue;
}
pref[i] = pref[i - 1] + flag;
}
}
inline long long get(long long i) {
if (i < 0) return 0;
return pref[i];
}
inline long long get_ans(long long L, long long R) {
if (R - m + 1 < 0 || L + m - 1 > R) return 0;
return pref[R - m + 1] - get(L - 1);
}
signed main() {
ios::sync_with_stdio(0);
cin >> n >> m >> q;
cin >> a >> b;
precalc();
for (long long i = 0; i < q; ++i) {
long long L, R;
cin >> L >> R;
--L, --R;
cout << get_ans(L, R) << "\n";
}
}
| 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 | a1 = list(map(int, input().split(' ')[:3]))
bigString = input()
littleString = input()
all = 0
left = [0]*len(bigString)
right = [0]*len(bigString)
leftCount = 0
rightCount = 0
for i in range(len(bigString)):
left[i] = leftCount
if bigString.find(littleString,i,i + len(littleString)) != -1:
leftCount += 1
for i in range(len(bigString) - 1, -1, -1):
#print(i)
right[i] = rightCount
if bigString.find(littleString,i - len(littleString) + 1, i + 1) != -1:
rightCount += 1
for i in range(len(bigString)):
if bigString.find(littleString,i,i + len(littleString)) != -1:
all += 1
#print(left)
#print(right)
#print(all, "all")
for s in range(a1[2]):
a2 = list(map(int, input().split(' ')[:2]))
a2[0] -= 1
a2[1] -= 1
if a2[1] - a2[0] < len(littleString) - 1:
print(0)
else:
result = all - right[a2[1]] - left[a2[0]]
print(result)
| PYTHON3 |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class SO1016Bv2 {
public static void main(String[] args) throws IOException {
BufferedReader inputs = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer threeInts = new StringTokenizer(inputs.readLine());
int sLength = Integer.parseInt(threeInts.nextToken());
int tLength = Integer.parseInt(threeInts.nextToken());
int queries = Integer.parseInt(threeInts.nextToken());
String superString = inputs.readLine();
String checkString = inputs.readLine();
int subLow; //start index of the substring.
int subHigh; //end index of the substring.
StringTokenizer queryHandler;
int curOccurrences = 0; //Occurrences of checkString.
int[] occurrences = new int[sLength]; //Counts if there is an occurrence of checkString at a specified index..
//Calculates if at a given index there is an occurrence of checkString.
for(int i = 0; i < sLength - (tLength - 1); i++) {
if(superString.substring(i, i + tLength).equals(checkString)) {
occurrences[i] = 1;
}
}
//Takes care of the queries.
for(int i = 0; i < queries; i++) {
queryHandler = new StringTokenizer(inputs.readLine());
subLow = Integer.parseInt(queryHandler.nextToken());
subHigh = Integer.parseInt(queryHandler.nextToken());
for(int j = subLow - 1; j < subHigh - (tLength - 1); j++) {
if(occurrences[j] == 1) { curOccurrences++; }
}
System.out.println(curOccurrences);
curOccurrences = 0;
}
inputs.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.*;
public class CodeforcesEdu48 {
public static Scanner scn = new Scanner (System.in);
public static void main(String[] args) {
// Q1();
// Q2();
Q2n();
}
private static void Q2n() {
int n = scn.nextInt();
int m = scn.nextInt();
int qn = scn.nextInt();
String s = scn.next();
String t = scn.next();
int[][] q = new int[qn][2];
for(int i =0;i<qn;i++) {
q[i][0] = scn.nextInt()-1;
q[i][1] = scn.nextInt()-1;
}
if(n<m) {
for(int i = 0;i<qn;i++) {
System.out.println(0);
}
return;
}
for(int i = 0;i<qn;i++) {
int l = q[i][0];
int r = q[i][1];
int ans = KMPSearch(t, s.substring(l, r+1));
if(ans==-1) {
System.out.println(0);
}else {
System.out.println(ans);
}
}
}
static int KMPSearch(String pat, String txt)
{
int count = 0;
int M = pat.length();
int N = txt.length();
int lps[] = new int[M];
int j = 0;
computeLPSArray(pat,M,lps);
int i = 0;
while (i < N)
{
if (pat.charAt(j) == txt.charAt(i))
{
j++;
i++;
}
if (j == M)
{
int ans = (i-j);
j = lps[j-1];
count++;
}
else if (i < N && pat.charAt(j) != txt.charAt(i))
{
if (j != 0)
j = lps[j-1];
else
i = i+1;
}
}
if(count!=0)return count;
return -1;
}
static void computeLPSArray(String pat, int M, int lps[])
{
int len = 0;
int i = 1;
lps[0] = 0;
while (i < M)
{
if (pat.charAt(i) == pat.charAt(len))
{
len++;
lps[i] = len;
i++;
}
else
{
if (len != 0)
{
len = lps[len-1];
}
else
{
lps[i] = len;
i++;
}
}
}
}
private static void Q2() {
int n = scn.nextInt();
int m = scn.nextInt();
int qn = scn.nextInt();
String s = scn.next();
String t = scn.next();
int[][] q = new int[qn][2];
for(int i =0;i<qn;i++) {
q[i][0] = scn.nextInt()-1;
q[i][1] = scn.nextInt()-1;
}
if(n<m) {
for(int i = 0;i<qn;i++) {
System.out.println(0);
}
return;
}
int[] a = new int[n];
int st =0;
int i = 0;
int last = 0;
int count = 0;
while(i<n) {
int pos = s.indexOf(t,i);
if(pos==-1) {
for(int j =last+1;j<n;j++) {
a[j] = count;
}
break;
}else {
count++;
int nlast = pos+t.length()-1;
a[pos+t.length()-1] = count;
for(int j =last+1;j<nlast;j++) {
a[j] = count-1;
}
last = nlast;
i = pos+1;
}
}
for(int j = 0;j<qn;j++) {
int l = q[j][0];
int r = q[j][1];
if(r-l>=t.length())
System.out.println(a[r]-a[l]);
else {
int ans = a[r]-a[l];
if(ans>0)System.out.println(ans-1);
else System.out.println(0);
}
}
}
private static void Q1() {
int n = scn.nextInt();
int m = scn.nextInt();
int[]arr = new int[n];
for(int i = 0;i<n;i++) {
arr[i] = scn.nextInt();
}
int left = m;
int[] sol = new int[n];
long pg = 1;
for(int i = 0;i<n;i++) {
if(arr[i]<left) {
left-=arr[i];
sol[i] = 0;
}else {
int np = (arr[i]-left)/m;
sol[i] = np+1;
// pg+=np;
left = m- (arr[i]-left)%m ;
}
System.out.print(sol[i]+" ");
}
System.out.println();
}
}
| 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 add_self(int& a, int b) {
a += b;
if (a >= (int)1e9 + 7) a -= (int)1e9 + 7;
}
void solve() {
int n, m, q;
cin >> n >> m >> q;
string s, t;
cin >> s >> t;
vector<int> a(n + 1, 0);
int cnt = 0;
for (int i = 0; i <= n - m; i++) {
bool ok = true;
int k = 0;
for (int j = i; j < i + m; j++) {
if (s[j] != t[k++]) {
ok = false;
break;
}
}
if (ok) {
cnt++;
}
a[i + 1] = cnt;
}
for (int i = n - m + 1; i <= n; i++) a[i] = cnt;
while (q--) {
int l, r;
cin >> l >> r;
int t = r - m + 1;
cout << (a[t] - a[l - 1]) * (r - l + 1 >= m) << endl;
}
}
int main() {
ios::sync_with_stdio(false);
int q = 1;
for (int i = 1; i <= q; i++) {
solve();
}
}
| 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)
{
FastReader reader = new FastReader();
PrintWriter writer = new PrintWriter(System.out);
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 | def count_overlapping_substrings(haystack, needle):
count = 0
i = -1
while True:
i = haystack.find(needle, i + 1)
if i == -1:
return count
left.append(i)
right.append(i + len(needle) - 1)
count += 1
def bin_search(lst, x):
lower_bound = 0
upper_bound = len(lst) - 1
while lower_bound != upper_bound:
compared_value = (lower_bound + upper_bound) // 2 # Целочисленный тип в Python имеет неограниченную длину
if x == lst[compared_value]:
return compared_value
elif x < lst[compared_value]:
upper_bound = compared_value
else:
lower_bound = compared_value + 1
return lower_bound
n, m, q = map(int, input().split())
s1 = input()
s2 = input()
right = []
left = []
count_overlapping_substrings(s1, s2)
for i in range(q):
l, r = map(int, input().split())
if right == []:
print(0)
continue
a = bin_search(left, l - 1)
b = bin_search(right, r - 1)
if left[a] < l - 1:
a += 1
if right[b] > r - 1:
b -= 1
if (a > b):
print(0)
else:
print(b - a + 1)
| PYTHON3 |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | from sys import stdin
from math import *
line = stdin.readline().rstrip().split()
n = int(line[0])
m = int(line[1])
q = int(line[2])
s = stdin.readline().rstrip().split()[0]
t = stdin.readline().rstrip().split()[0]
bools = [0] * (n - m + 1 + 1)
accum = 0
for i in range(n - m, -1, -1):
if s[i:i+m] == t:
accum += 1
bools[i] = accum
for i in range(q):
numbers = list(map(int, stdin.readline().rstrip().split()))
l = numbers[0] - 1
r = numbers[1] - 1 - m + 1 + 1
if r > l:
print(bools[l] - bools[r])
else:
print(0) | 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 mxn = 1e3 + 5, seed = 131, seed2 = 31, MOD = 1e9 + 9;
int n, m, q;
string s, t;
long long p[mxn], pp[mxn], a[mxn], aa[mxn], b[mxn], bb[mxn];
inline long long strHash(int l, int r) {
return (long long)((1LL * a[r] - 1LL * a[l - 1] * p[r - l + 1] % MOD) + MOD) %
MOD;
}
int main() {
cin >> n >> m >> q >> s >> t;
p[0] = 1;
for (int i = 1; i <= max(n, m); i++)
p[i] = ((p[i - 1] * seed % MOD) + MOD) % MOD;
for (int i = 1; i <= n; i++)
a[i] = ((a[i - 1] * seed + s[i - 1]) % MOD + MOD) % MOD;
for (int i = 1; i <= m; i++)
b[i] = ((b[i - 1] * seed + t[i - 1]) % MOD + MOD) % MOD;
for (int i = 1, L, R, ans; i <= q; i++) {
cin >> L >> R;
ans = 0;
for (int j = L; j + m - 1 <= R; j++) {
if (strHash(j, j + m - 1) == b[m]) ans++;
}
cout << ans << "\n";
}
}
| 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.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int q = sc.nextInt();
String s = sc.next();
String t = sc.next();
if(n >= m){
int[] occurence = new int[n - m + 1];
for(int i = 0; i < n - m + 1; i ++){
boolean same = true;
for(int j = i; j < i + m; j ++){
if(s.charAt(j) != t.charAt(j - i)){
same = false;
break;
}
}
occurence[i] = same ? 1 : 0;
}
int[] sum_occurences = new int[n - m + 2];
sum_occurences[1] = occurence[0];
for(int i = 1; i < n - m + 1; i ++){
sum_occurences[i + 1] = sum_occurences[i] + occurence[i];
}
for(int i = 0; i < q; i ++) {
int l = sc.nextInt();
int r = sc.nextInt();
if (r - l < m - 1) {
System.out.println(0);
} else {
System.out.println(sum_occurences[r - m + 1] - sum_occurences[l - 1]);
}
}
} else {
for (int i = 0; i < q; i++) {
int l = sc.nextInt();
int r = sc.nextInt();
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 | import sys
input = sys.stdin.readline
class BIT():
def __init__(self, n):
self.size = n
self.bit = [0] * (n + 1)
def _sum(self, i):
s = 0
while i > 0:
s += self.bit[i]
i -= i & -i
return s
def add(self, i, val):
i += 1
while i <= self.size:
self.bit[i] += val
i += i & -i
def get_sum(self, l, r):
return self._sum(r) - self._sum(l)
def z_algo(s):
res = [0] * len(s)
res[0] = len(s)
i = 1
j = 0
while i < len(s):
while i + j < len(s) and s[j] == s[i + j]:
j += 1
res[i] = j
if j == 0:
i += 1
continue
k = 1
while i + k < len(s) and k + res[k] < j:
res[i + k] = res[k]
k += 1
i += k
j -= k
return res
n, m, q = map(int, input().split())
s = input()[:-1]
t = input()[:-1]
query = [list(map(int, input().split())) for i in range(q)]
ss = t + s
tmp = z_algo(ss)
ans = [0] * n
for i in range(m, n + m):
if tmp[i] >= len(t):
ans[i - m] = 1
bit = BIT(n)
for i in range(n):
bit.add(i, ans[i])
for l, r in query:
l -= 1
if l > r - len(t) + 1:
print(0)
else:
print(bit.get_sum(l, r - len(t) + 1)) | PYTHON3 |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int match[1009];
int prefix[1009];
string s, t;
bool isMatch(int a) {
for (int i = 0; i < t.size(); i++) {
if (i + a >= s.size() || s[i + a] != t[i]) {
return false;
}
}
return true;
}
int main() {
ios_base::sync_with_stdio(0);
int n, m;
cin >> n >> m;
int q;
cin >> q;
cin >> s >> t;
for (int i = 0; i < s.size(); i++) {
match[i] = isMatch(i);
}
prefix[0] = match[0];
for (int i = 1; i < s.size(); i++) {
prefix[i] = prefix[i - 1] + match[i];
}
cout << endl;
for (int i = 0; i < q; i++) {
int p, q;
cin >> p >> q;
q -= t.size() - 2;
q--;
p--;
q--;
p--;
int ans;
if (q < 0 || q <= p) {
ans = 0;
} else if (p == -1) {
ans = prefix[q];
} else {
ans = prefix[q] - prefix[p];
}
cout << ans << endl;
}
}
| CPP |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
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() - 1;
int m = in.nextInt() - 1;
int q = in.nextInt();
char[] s = in.nextCharArray();
char[] t = in.nextCharArray();
int[][] a = new int[n + 1][n + 1];
for (int i = 0; i < n - m + 1; i++) {
for (int j = m + i; j <= n; j++) {
if (j - i == m)
a[i][j] = func(s, t, j);
else
a[i][j] = a[i][j - 1] + func(s, t, j);
}
}
for (int i = 0; i < q; i++) {
out.println(a[in.nextInt() - 1][in.nextInt() - 1]);
}
}
public int func(char[] s, char[] t, int j) {
int len = t.length;
for (int a = len - 1; a >= 0; a--) {
if (s[j--] != t[a])
return 0;
}
return 1;
}
}
static class InputReader {
BufferedReader br;
StringTokenizer st;
public InputReader(InputStream stream) {
br = new BufferedReader(new InputStreamReader(stream));
st = null;
}
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());
}
public char[] nextCharArray() {
return next().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 collections, atexit, math, sys, bisect
sys.setrecursionlimit(1000000)
def getIntList():
return list(map(int, input().split()))
try :
#raise ModuleNotFoundError
import numpy
def dprint(*args, **kwargs):
print(*args, **kwargs, file=sys.stderr)
dprint('debug mode')
except ModuleNotFoundError:
def dprint(*args, **kwargs):
pass
inId = 0
outId = 0
if inId>0:
dprint('use input', inId)
sys.stdin = open('input'+ str(inId) + '.txt', 'r') #标准输出重定向至文件
if outId>0:
dprint('use output', outId)
sys.stdout = open('stdout'+ str(outId) + '.txt', 'w') #标准输出重定向至文件
atexit.register(lambda :sys.stdout.close()) #idle 中不会执行 atexit
N, M, Q = getIntList()
s1 = input()
s2 = input()
tot = 0
zt = [0]
for i in range(N):
if s1[i:i+M] == s2:
tot+=1
zt.append(tot)
dprint(zt)
for i in range(Q):
a,b = getIntList()
b0 = b- M+1
if b0<a:
print(0)
else:
print(zt[b0] - zt[a-1])
| PYTHON3 |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | R=lambda:map(int,input().split())
n,m,q=R()
s,t=input(),input()
a=[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-1,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 | # https://codeforces.com/contest/1016/problem/B
n , m , q=map(int,input().split())
goals = ''
s , t = input() , input()
for _ in range(n - m + 1):
if (s[_:_ + m] == t):goals += '1'
else:goals += '0'
for _ in range(q):
arguments = input().split()
l = int(arguments[0])
r = int(arguments[1])
if r - l + 1 >= m: print(goals[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 | N, M, Q = map(int, raw_input().split())
s = raw_input()[:N]
t = raw_input()[:M]
psum = [0 for _ in range(len(s) + 1)]
for i in range(len(s)):
psum[i+1] = psum[i]
if t == s[i:i+len(t)]:
psum[i+1] += 1
for q in range(Q):
a, b = map(int, raw_input().split())
a -= 1
b -= len(t) - 1
b = max(b, 0)
print max(0, psum[b] - psum[a])
| PYTHON |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import java.io.*;
import java.util.*;
public class CF_segmentoccurences {
public static void main (String [] args) throws IOException {
//PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
//Scanner sc = new Scanner(System.in);//new File("input.txt"));
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(f.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int q = Integer.parseInt(st.nextToken());
String s = f.readLine();
String t = f.readLine();
//HashSet<Integer> b = new HashSet<>();
ArrayList<Integer> a = new ArrayList<>();
int x = -1;
loop: while(true){
x = s.indexOf(t, x+1); //throws error out of bounds? +1??
//System.out.println("abc: "+x);
if(x == -1)
{
break loop;
}
a.add(x+1);
}
int count = 0;
String[] retvals = new String[q];
int curr = 0;
for(int i = 0; i < q; i++){
count = 0;
st = new StringTokenizer(f.readLine());
int min = Integer.parseInt(st.nextToken());
int max = Integer.parseInt(st.nextToken());
for(int j = 0; j < a.size(); j++){
curr = a.get(j);
if(curr >= min){
if(curr+m-1 <= max){
count++;
}
}
}
// System.out.println(count);
retvals[i] = Integer.toString(count);
}
for(int i = 0; i < q; i++){
System.out.println(retvals[i]);
}
//System.out.print(retval);
// out.println(retval);
// out.close(); // close the output file
// sc.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 | n, m, q = map(int, input().split())
s = input()
t = input()
a=[]
b=[]
c=[0]*n
for i in range(n):
if s[i: i+m]==t:
b.append([i, i+m])
x=0
y=0
for i in range(n):
c[i]=y
if x<len(b) and i == b[x][0]:
x+=1
y+=1
x=0
y=0
d=[0]*n
for i in range(n):
if x<len(b) and i+1 == b[x][1]:
x+=1
y+=1
d[i]=y
for i in range(q):
x = list(map(int, input().split()))
y = d[x[1]-1]-c[x[0]-1]
if y<0:
y=0
print(y) | PYTHON3 |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
sc.nextInt();
sc.nextInt();
int q = sc.nextInt();
String s = sc.next();
String t = sc.next();
int[] l = new int[q];
int[] r = new int[q];
for (int i = 0; i < q; i++) {
l[i] = sc.nextInt();
r[i] = sc.nextInt();
}
System.out.println(solve(s, t, l, r));
sc.close();
}
static String solve(String s, String t, int[] l, int[] r) {
int[] beginIndices = IntStream.range(0, s.length()).filter(i -> s.startsWith(t, i)).toArray();
return IntStream.range(0, l.length)
.mapToObj(i -> String.valueOf(Arrays.stream(beginIndices)
.filter(beginIndex -> beginIndex >= l[i] - 1 && beginIndex + t.length() <= r[i]).count()))
.collect(Collectors.joining("\n"));
}
}
| JAVA |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m, q, t = 0;
string a, b, s = "";
cin >> n >> m >> q >> a >> b;
vector<pair<int, int> > v;
while (t + b.size() - 1 < a.size()) {
if (a.substr(t, b.size()) == b) {
v.push_back({t + 1, t + b.size()});
}
t++;
}
while (q--) {
int l, r;
cin >> l >> r;
int c = 0;
for (int i = 0; i < v.size(); i++) {
if (l <= v[i].first && r >= v[i].second) c++;
}
cout << c << '\n';
}
}
| CPP |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
string s, t;
int ans[100009];
int lps[100009];
int main() {
int n, m, q;
cin >> n >> m >> q;
cin >> s >> t;
int sz = t.size();
int sz1 = s.size();
for (int i = 1; i <= (sz1 + 1 - sz); i++) {
int flag = 0;
string sb = s.substr(i - 1, sz);
if (sb == t) ans[i]++;
}
for (int i = 1; i <= sz1; i++) ans[i] += ans[i - 1];
while (q--) {
int x, y;
cin >> x >> y;
if (y - x + 1 < sz)
cout << 0 << endl;
else {
int mx = y + 1 - sz;
int val;
cout << ans[mx] - ans[x - 1] << endl;
}
}
return 0;
}
| CPP |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | n,m,q=map(int,input().split())
s=input()
t=input()
st=[0]*n
en=[0]*n
for i in range(n):
if(s[i:i+m]==t):
st[i]=1
en[i+m-1]=1
if(i>0):
st[i]+=st[i-1]
en[i]+=en[i-1]
st.insert(0,0)
for i in range(q):
l,r=map(int,input().split())
l-=1
r-=1
print(max(0,en[r]-st[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.util.*;
import java.io.*;
public class B48 {
public static void main(String[] args) {
MyScanner sc = new MyScanner();
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
int n = sc.nextInt(); int m = sc.nextInt(); int q = sc.nextInt();
String s = sc.next();
String t = sc.next();
int [] ok = new int[n];
for (int i = 0; i < n; i++) {
int match = 0;
for (int j = i; j < Math.min(n, i + m); j++) {
if (s.charAt(j) == t.charAt(j - i)) match++;
}
if (match == m) ok[i] = 1;
}
int [] pref = new int[n + 1];
for (int i = 1; i <= n; i++) pref[i] = pref[i - 1] + ok[i - 1];
for (int i = 0; i < q; i++) {
int l = sc.nextInt(); int r = sc.nextInt();
int sub = pref[l - 1];
int other = r - (m - 1);
if (other < l - 1) {
out.println(0);
} else {
out.println(pref[other] - sub);
}
}
out.close();
}
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | 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 solve(ist):
_ = ist.next_int()
_ = ist.next_int()
q = ist.next_int()
s = ist.next()
t = ist.next()
counts = [0 for _ in xrange(len(s) + 1)]
for i in xrange(1, len(s) + 1):
counts[i] = counts[i - 1]
if s[i - 1: i - 1 + len(t)] == t:
counts[i] += 1
queries = [(ist.next_int(), ist.next_int()) for _ in xrange(q)]
for l, r in queries:
if r - l + 1 < len(t):
ans = 0
else:
ans = counts[r - len(t) + 1] - counts[l - 1]
print ans
# -----------------------------------------------------------------------------
class InputStream(object):
@staticmethod # use as decorator
def input_generator(fd):
for line in fd:
for t in line.split():
yield t
def __init__(self, fd):
self.stream = InputStream.input_generator(fd)
def __iter__(self):
return self
def next(self):
return next(self.stream)
def next_int(self):
return int(self.next())
def next_float(self):
return float(self.next())
def main():
if len(sys.argv) > 1:
fd = open(sys.argv[1], "r")
else:
fd = sys.stdin
ist = InputStream(fd)
while True:
try:
solve(ist)
except StopIteration:
break
if __name__ == "__main__":
main()
| PYTHON |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigDecimal;
public class ER48B {
public static void main (String[] args) throws java.lang.Exception {
InputReader in = new InputReader(System.in);
PrintWriter w = new PrintWriter(System.out);
int n = in.nextInt(), m = in.nextInt(), q = in.nextInt();
char[] ca = in.next().toCharArray(), cb = in.next().toCharArray();
int[] occ = new int[n + 1];
for (int i = 0; i < n; i++) {
boolean ok = true;
if (i + m > n)
break;
for (int j = 0; j < m && ok; j++) {
if (ca[i + j] != cb[j]) {
ok = false;
}
}
if (ok)
occ[i + 1]++;
}
for (int i = 1; i <= n; i++)
occ[i] += occ[i - 1];
while (q-- > 0) {
int l = in.nextInt(),r = in.nextInt();
if (r - l + 1 < m)
w.println(0);
else {
w.println(occ[r - m + 1] - occ[l - 1]);
}
}
w.close();
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new UnknownError();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new UnknownError();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int peek() {
if (numChars == -1)
return -1;
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
return -1;
}
if (numChars <= 0)
return -1;
}
return buf[curChar];
}
public void skip(int x) {
while (x-- > 0)
read();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public String nextString() {
return next();
}
public String next() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
StringBuffer buf = new StringBuffer();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r')
buf.appendCodePoint(c);
c = read();
}
return buf.toString();
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public boolean hasNext() {
int value;
while (isSpaceChar(value = peek()) && value != -1)
read();
return value != -1;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
} | JAVA |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | def substr(str, start, length):
return str[start : (length + start)]
def main():
n, m, q = map(int, input().split())
s = input()
t = input()
a = [0] * n
for i in range(n - m + 1):
if substr(s, i, m) == t:
a[i] = 1
ans = []
for i in range(q):
l, r = map(int, input().split())
if (r - l + 1) < m:
ans.append(0)
continue
ans.append(a[l-1:r-m+1].count(1))
print('\n'.join(map(str, ans)))
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.ArrayList;
import java.util.Scanner;
// http://codeforces.com/contest/1016/problem/B
public class SegmantOccur {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int lenS = in.nextInt();
int lenT = in.nextInt();
int q = in.nextInt();
String strS = in.next();
String strT = in.next();
ArrayList<Integer> list = new ArrayList<>();
matchSrtings(list, strS, strT);
for (int i = 0; i < q; i++) {
int a = in.nextInt();
int b = in.nextInt();
int res = solve( a, b , list , lenT);
System.out.println(res);
}
}
static int solve(int a , int b, ArrayList<Integer> list, int patternLen) {
a--;
b--;
if(b - a < patternLen -1) {
return 0;
}
int count= 0;
for (int i = 0; i < list.size(); i++) {
int from = (int) list.get(i);
int to = from + patternLen - 1;
if(a <= from && b >= to) {
count++;
}
}
return count;
}
static void matchSrtings(ArrayList list , String str , String pattern) {
int patLen = pattern.length();
int index = patLen-1;
while(index < str.length()) {
int patIndex = pattern.length() - 1;
if(str.charAt(index) == pattern.charAt(patIndex)) {
int indString = index-1;
patIndex--;
for (int i = indString; i > indString-patLen+1; i--) {
if(str.charAt(i) != pattern.charAt(patIndex)) {
patIndex = -3;
break;
}
patIndex--;
}
if(patIndex != -3) {
list.add(index-patLen+1);
}
index += 1;
}else{
index += 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 | a,b,n = map(int,input().split())
s = input()
t = input()
pre = [0]*100860
def pre_cnt(s1,s2):
for i in range(0,len(s1)-len(s2)+1):
if s1[i:i+len(s2)]==s2:
pre[i+1]=pre[i]+1
else: pre[i+1] = pre[i]
pre_cnt(s,t)
while n!=0:
n-=1
a,b = map(int,input().split())
ans = pre[b-len(t)+1]-pre[a-1]
if a-1>b-len(t)+1:
ans = 0
print(ans)
| PYTHON3 |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import java.util.Scanner;
public class B {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[] pos = new int[n+2];
int q = sc.nextInt();
String s = sc.nextLine();
s = sc.nextLine();
String k = sc.nextLine();
positions(pos,s,k);
for (int i = 0; i <q ; i++) {
int l = sc.nextInt();
int r = sc.nextInt();
int sum=0;
for (int j = l; j <=r-m+1 ; j++) {
sum+=pos[j];
}
System.out.println(sum);
}
}
public static void positions(int[] arr, String s,String sub){
int index = 0;
int num=0;
while (s.indexOf(sub,index)>-1){
index = s.indexOf(sub,index)+1;
arr[index]=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 | import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class SegmentOccurrences {
static String P, S;
static Scanner scanner = new Scanner(System.in);
static int n, m, q;
static int a, b, length;
public static void main(String[] args) {
n = scanner.nextInt();
m = scanner.nextInt();
q = scanner.nextInt();
S = scanner.next();
P = scanner.next();
long occurrences;
List<Integer> list = KMP(S, P);
for(int i = 0; i < q; ++i){
a = scanner.nextInt() - 1;
b = scanner.nextInt();
length = b - a;
if(b - a < P.length()){
System.out.println(0);
continue;
}
occurrences = list.stream().filter(x -> (x >= a) && x <= (b - P.length())).count();
System.out.println(occurrences);
}
}
static List<Integer> KMP(String T, String P){
String S = P + '$' + T;
int[] prefix = prefix(S);
List<Integer> result = new LinkedList<>();
for(int i = P.length() + 1; i < S.length(); ++i)
if(prefix[i] == P.length())
result.add(i - 2 * P.length());
return result;
}
static int[] prefix(String P)
{
int[] prefix = new int[P.length()];
int border = 0; prefix[0] = 0;
for(int i = 1; i < P.length(); ++i){
while(border > 0 && P.charAt(i) != P.charAt(border))
border = prefix[border - 1];
if(P.charAt(i) == P.charAt(border))
border = border + 1;
else
border = 0;
prefix[i] = border;
}
return prefix;
}
} | JAVA |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n, m, q, i, j, l, r, a[5000];
string s, t;
cin >> n >> m >> q;
cin >> s >> t;
if (m > n) goto label;
if (s.compare(0, m, t) == 0) {
a[0] = 1;
} else
a[0] = 0;
for (j = 1; j <= n - m + 1; j++) {
if (s.compare(j, m, t) == 0) {
a[j] = a[j - 1] + 1;
} else {
a[j] = a[j - 1];
}
}
for (j = n - m + 2; j < n; j++) {
a[j] = a[n - m + 1];
}
label:
for (i = 0; i < q; i++) {
cin >> l >> r;
l--;
r--;
if (r - l < m - 1 || m > n)
cout << l - l << endl;
else if (l == 0)
cout << a[r - m + 1] << endl;
else
cout << a[r - m + 1] - a[l - 1] << endl;
}
return 0;
}
| 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.util.Map.Entry;
import java.util.concurrent.ThreadLocalRandom;
public class Main {
final static int c = 1000000009;
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
int n = console.nextInt(), m = console.nextInt(), q = console.nextInt();
console.nextLine();
String s = console.nextLine(), t = console.nextLine();
if(n < m) {
for(int i = 0; i < q; i++) {
System.out.println(0);
}
return;
}
boolean[] occ = new boolean[n];
for(int i = 0; i < n - m + 1; i++) {
if(s.substring(i, i + m).equals(t)) occ[i] = true;
}
// System.out.println(Arrays.toString(occ));
for(int i = 0; i < q; i++) {
int l = console.nextInt(), r = console.nextInt();
// if(r - l + 1 < t.length()) {
// System.out.println(0);
// continue;
// }
int res = 0;
for(int j = l - 1; j <= r - m; j++) {
if(occ[j]) res++;
}
System.out.println(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 java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
public class B {
static int N,M,Q;
public static void main(String[] args) {
JS in = new JS();
PrintWriter out = new PrintWriter(System.out);
N=in.nextInt();
M=in.nextInt();
Q = in.nextInt();
char s[] = in.next().toCharArray();
char t[] = in.next().toCharArray();
ArrayList<Integer> tInds = search(s, t, KMP_Construct(t));
//System.out.println(tInds.toString());
for(int qq = 0; qq < Q; qq++) {
int l = in.nextInt()-1;
int r = in.nextInt()-1;
int res = 0;
for(Integer ii : tInds) {
if(ii <= r && ii >= l && ii-M+1 >= l) res++;
}
out.println(res);
}
out.close();
}
// Given a pattern string w, return the prefix array. //%
// p[i] indicates the length of the longest prefix //%
// that is also a substring ending at i. O(|w|) //%
static int[] KMP_Construct(char[] w) {
int n = w.length + 1, k = 0; int[] p = new int[n]; p[1] = 0;
for(int i = 2; i < n; ++i) {
while(k > 0 && w[k] != w[i-1]) k = p[k];
if(w[k] == w[i-1]) k++;
p[i] = k;
}
return p;
}
// Given the prefix array of a pattern, search the //%
// text for all occurrences of the pattern. O(|s|) //%
static ArrayList<Integer> search(char[] s, char[] w, int[] p) {
int k = 0; ArrayList<Integer> locs = new ArrayList<>();
for(int i = 0; i < s.length; ++i) {
while(k > 0 && w[k] != s[i]) k = p[k];
if(w[k] == s[i]) k++;
if(k == w.length) { locs.add(i); k = p[k]; }
}
return locs;
}
static class JS{
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 JS() {
in = new BufferedInputStream(System.in, BS);
}
public JS(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() {
while(c!='.'&&c!='-'&&(c <'0' || c>'9')) c = nextChar();
boolean neg = c=='-';
if(neg)c=nextChar();
boolean fl = c=='.';
double cur = nextLong();
if(fl) return neg ? -cur/num : cur/num;
if(c == '.') {
double next = nextLong();
return neg ? -cur-next/num : cur+next/num;
}
else return neg ? -cur : cur;
}
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;
}
}
}
}
| 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 max(int a, int b) { return (a > b) ? a : b; }
int min(int a, int b) { return (a < b) ? a : b; }
int main() {
int n, m, k;
cin >> n >> m >> k;
string s, t;
cin >> s;
cin >> t;
unordered_map<char, int> umap;
vector<int> skips;
for (int i = 0; i < t.size(); i++) {
if (umap.find(t[i]) == umap.end()) {
umap[t[i]] = i;
skips.push_back(i + 1);
} else {
skips.push_back(i - umap[t[i]]);
umap[t[i]] = i;
}
}
vector<int> counter(n);
int count = 0, x = 0;
for (int j = 0; j < n && m + j - 1 < n;) {
bool ans = 0;
for (int x = 0; x < m && m + j - 1 < n; x++) {
if (t[x] != s[j + x]) {
ans = 0;
if (x != 0)
j += skips[x - 1];
else
j += 1;
break;
} else {
ans = 1;
}
}
if (ans == 1) {
count++;
counter[j + m - 1] = count;
j += skips[m - 1];
}
}
for (int i = 1; i < n; i++) {
if (counter[i - 1] != 0 && counter[i] == 0) counter[i] = counter[i - 1];
}
for (int i = 0; i < k; i++) {
int l, r;
cin >> l >> r;
if (l - 1 + m - 2 >= 0)
cout << counter[r - 1] - counter[min(r - 1, l - 1 + m - 2)] << endl;
else
cout << counter[r - 1] << endl;
}
return 0;
}
| 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;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Test {
static class BIT {
private int[] array;
private int total;
public BIT(int[] ar) {
this.total = ar.length;
int i = 1;
while (i < this.total) {
i = i * 2;
}
array = new int[i];
}
public BIT(int N) {
this.total = N;
int i = 1;
while (i < this.total) {
i = i * 2;
}
array = new int[i];
}
public void update(int index, int value) {
index = index;
while (index < total) {
array[index] += value;
index += index & (-index);
}
}
public long sum(int index) {
int sum = 0;
index = index;
while (index > 0) {
sum += array[index];
index -= index & (-index);
}
return sum;
}
}
// 1000 3 4
// fofofofofofofof
// fof
// JAVA program for implementation of KMP pattern
// searching algorithm
static class KMP_String_Matching {
List<Integer> list = new ArrayList<Integer>();
void KMPSearch(String pat, String txt) {
int M = pat.length();
int N = txt.length();
// create lps[] that will hold the longest
// prefix suffix values for pattern
int lps[] = new int[M];
int j = 0; // index for pat[]
// Preprocess the pattern (calculate lps[]
// array)
computeLPSArray(pat, M, lps);
int i = 0; // index for txt[]
while (i < N) {
if (pat.charAt(j) == txt.charAt(i)) {
j++;
i++;
}
if (j == M) {
list.add((i - j));
// System.out
// .println("Found pattern " + "at index " + (i - j));
j = lps[j - 1];
}
// mismatch after j matches
else if (i < N && pat.charAt(j) != txt.charAt(i)) {
// Do not match lps[0..lps[j-1]] characters,
// they will match anyway
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
}
void computeLPSArray(String pat, int M, int lps[]) {
// length of the previous longest prefix suffix
int len = 0;
int i = 1;
lps[0] = 0; // lps[0] is always 0
// the loop calculates lps[i] for i = 1 to M-1
while (i < M) {
if (pat.charAt(i) == pat.charAt(len)) {
len++;
lps[i] = len;
i++;
} else // (pat[i] != pat[len])
{
// This is tricky. Consider the example.
// AAACAAAA and i = 7. The idea is similar
// to search step.
if (len != 0) {
len = lps[len - 1];
// Also, note that we do not increment
// i here
} else // if (len == 0)
{
lps[i] = len;
i++;
}
}
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String readLine = br.readLine();
Scanner sc = new Scanner(readLine);
int n = sc.nextInt();
int m = sc.nextInt();
int q = sc.nextInt();
String s = br.readLine();
String t = br.readLine();
// Check all occurrences
BIT tree1 = new BIT(n + 2);
KMP_String_Matching kmp_String_Matching = new KMP_String_Matching();
kmp_String_Matching.KMPSearch(t, s);
int patternLength = t.length();
List<Integer> list = kmp_String_Matching.list;
for (int i : list) {
tree1.update(i + 1, 1);
}
while (q-- > 0) {
String[] split = br.readLine().split(" ");
int start = Integer.parseInt(split[0].trim());
int end = Integer.parseInt(split[1].trim()) - patternLength + 1;
if (end >= start) {
long temp1 = tree1.sum(end);
long temp2 = tree1.sum(start - 1);
long answer = temp1 - temp2;
System.out.println(answer);
} 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 | import sys
def mapi(): return map(int,input().split())
def maps(): return map(str,input().split())
#--------------------------------------------------
n,m,q = mapi()
s = input().strip()
t = input().strip()
i = 0
j = m
mp = {}
pf = [0]
for i in range(n-m+1):
if s[i:i+m]==t:
pf.append(pf[-1]+1)
else:
pf.append(pf[-1])
for i in range(max(0,n-m+1),n):
pf.append(pf[-1])
#print(pf,len(pf))
for _ in range(q):
l,r = mapi()
l=l-1
r = r-m+1
if l<=r:
print(pf[r]-pf[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 | n,m,q=map(int,input().split())
s=input()
t=input()
tt=[0]*1111
for i in range(n-m+1):
j=0
while j<m and s[i+j]==t[j]: j+=1
if j>=m: tt[i+1]=1
for i in range(1,n+1): tt[i]+=tt[i-1]
for i in range(q):
a,b=map(int,input().split())
if b-a+1<m:
print(0)
continue
print(tt[b-m+1]-tt[a-1])
'''
//////////////// ////// /////// // /////// // // //
//// // /// /// /// /// // /// /// //// //
//// //// /// /// /// /// // ///////// //// ///////
//// ///// /// /// /// /// // /// /// //// // //
////////////// /////////// /////////// ////// /// /// // // // //
'''
| PYTHON3 |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | 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 masterbios
*/
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 {
static final int q = 101;
static final int d = 256;
static int res[];
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int m = in.nextInt();
int q = in.nextInt();
String text = in.next();
String pattern = in.next();
res = new int[n];
RabinKarp(text, pattern, out);
for (int j = 0; j < q; j++) {
int x = in.nextInt();
int y = in.nextInt();
x--;
y--;
int count = 0;
for (int i = x; i <= y; i++) {
if (res[i] == 1 && i + m - 1 <= y) count += 1;
}
out.println(count);
}
}
public static void RabinKarp(String text, String pattern, PrintWriter out) {
int pl = pattern.length();
int tl = text.length();
int ph = 0;
int th = 0;
int i, j;
int h = 1;
int count = 0;
if (pl > tl) {
return;
}
if (pl == tl) {
if (pattern.equals(text)) {
res[0] = 1;
return;
}
}
for (i = 0; i < pattern.length() - 1; i++) {
h = (h * d) % q;
}
for (i = 0; i < pl; i++) {
ph = (d * ph + pattern.charAt(i)) % q;
th = (d * th + text.charAt(i)) % q;
}
for (i = 0; i <= tl - pl; i++) {
if (ph == th) {
for (j = 0; j < pl; j++) {
if (text.charAt(j + i) != pattern.charAt(j)) break;
}
if (j == pl) res[i] = 1;
}
if (i < tl - pl) {
th = (d * (th - text.charAt(i) * h) + text.charAt(i + pl)) % q;
if (th < 0) th += q;
}
}
}
}
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.hasMoreElements()) {
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 | n,m,q=map(int,input().split())
s=input()
t=input()
ans=[0]*n
for i in range(n-m+1):
if s[i:i+m]==t:
ans[i]=1
# print(ans)
for i in range(q):
x,y=map(int,input().split())
if y-x+1<m:
print(0)
else:
print(sum(ans[x-1:y-m+1])) | PYTHON3 |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | def start(s1,s2):
l=[0]*len(s1)
for i in range(len(s1)):
if(s1[i:i+len(s2)]==s2):
l[i]=1
return l
def c(a,b,l,m):
r,n=0,b-a+1
if(b-a+1<m):
return 0
else:
return sum(l[a:b-m+2])
n,m,q=map(int,raw_input().split())
s1=raw_input()
s2=raw_input()
l=start(s1,s2)
#print l
for i in range(q):
a,b=map(int,raw_input().split())
print c(a-1,b-1,l,len(s2)) | PYTHON |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import java.util.*;
import java.math.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
//--------------->>>>IF YOU ARE HERE FOR QUICKSORT HACK THEN SORRY NO HACK FOR YOU<<<-------------------
public class a{
static int[] count,count1,count2;
static Node[] nodes;
static long[] arr;
static int[] dp;
static char[] ch,ch1;
static long[] darr,farr;
static char[][] mat,mat1;
static long x,h;
static long maxl;
static double dec;
static long mx = (long)1e9+7;
static String s;
static long minl;
static int start_row;
static int start_col;
static int end_row;
static int end_col;
static long mod = 998244353;
// static int minl = -1;
// static long n;
static int n,n1,n2,q;
static long a;
static long b;
static long c;
static long d;
static long y,z;
static int m;
static long k;
static FastScanner sc;
static String[] str,str1;
static Set<Integer> set,set1,set2;
static SortedSet<Long> ss;
static List<Integer> list,list1,list2,list3;
static PriorityQueue<Long> pq,pq1;
static LinkedList<Integer> ll;
static Map<Integer,List<Integer>> map,map1;
static StringBuilder sb,sb1,sb2;
static int index;
static int[] dx = {0,-1,0,1,-1,1,-1,1};
static int[] dy = {-1,0,1,0,-1,-1,1,1};
// public static void solve(){
// FastScanner sc = new FastScanner();
// // int t = sc.nextInt();
// int t = 1;
// for(int tt = 0 ; tt < t ; tt++){
// int n = sc.nextInt();
// int m = sc.nextInt();
// int prev = 0;
// for(int i = 0 ; i < n ; i++){
// int s = sc.nextInt();
// int e = sc.nextInt();
// if(s > prev){
// System.out.println("NO");
// return;
// }
// prev = Math.max(prev,e);
// }
// if(prev == m)
// System.out.println("YES");
// else
// System.out.println("NO");
// // System.out.println(sb);
// }
// }
//--------------->>>>IF YOU ARE HERE FOR QUICKSORT HACK THEN SORRY NO HACK FOR YOU<<<-------------------
public static void solve(){
int len = 0;
int[] kmp = new int[m];
kmp[0] = 0;
int i = 1;
while(i < m){
if(ch1[i] == ch1[len]){
len += 1;
kmp[i] = len;
i += 1;
}
else{
if(len == 0){
kmp[i] = 0;
i += 1;
}
else
len = kmp[len-1];
}
}
for(int j = 0 ; j < q ; j++){
len = 0;
int matches = 0;
int l = sc.nextInt()-1;
int r = sc.nextInt();
for(i = l ; i < r ; i++){
while((len > 0) && (ch[i] != ch1[len])){
len = kmp[len-1];
}
if(ch[i] == ch1[len]){
len += 1;
}
if(len == m){
matches += 1;
len = kmp[m-1];
}
}
System.out.println(matches);
}
}
public static void main(String[] args) {
sc = new FastScanner();
// Scanner sc = new Scanner(System.in);
// int t = sc.nextInt();
int t = 1;
// int l = 1;
while(t > 0){
// n = sc.nextInt();
// n = sc.nextLong();
// a = sc.nextLong();
// b = sc.nextLong();
// c = sc.nextLong();
// d = sc.nextLong();
// x = sc.nextLong();
// y = sc.nextLong();
// n = sc.nextLong();
n = sc.nextInt();
// n1 = sc.nextInt();
m = sc.nextInt();
q = sc.nextInt();
// k = sc.nextLong();
// s = sc.next();
ch = sc.next().toCharArray();
ch1 = sc.next().toCharArray();
// arr = new long[n];
// for(int i = 0 ; i < n ; i++){
// arr[i] = sc.nextLong();
// }
// ch = sc.next().toCharArray();
// m = n;
// darr = new long[m];
// for(int i = 0 ; i < m ; i++){
// darr[i] = sc.nextLong();
// }
// farr = new int[n];
// for(int i = 0; i < n ; i++){
// farr[i] = sc.nextInt();
// }
// mat = new int[n][n];
// for(int i = 0 ; i < n ; i++){
// for(int j = 0 ; j < n ; j++){
// mat[i][j] = sc.nextInt();
// }
// }
// mat = new char[n][m];
// for(int i = 0 ; i < n ; i++){
// String s = sc.next();
// for(int j = 0 ; j < m ; j++){
// mat[i][j] = s.charAt(j);
// }
// }
// str = new String[n];
// for(int i = 0 ; i < n ; i++)
// str[i] = sc.next();
// nodes = new Node[n];
// for(int i = 0 ; i < n ;i++)
// nodes[i] = new Node(sc.nextInt(),sc.nextInt());
// System.out.println(solve()?"YES":"NO");
solve();
// System.out.println(solve());
t -= 1;
}
}
public static int log(long n){
if(n == 0 || n == 1)
return 0;
if(n == 2)
return 1;
double num = Math.log(n);
double den = Math.log(2);
if(den == 0)
return 0;
return (int)(num/den);
}
public static boolean isPrime(long n) {
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n%2 == 0 || n%3 == 0)
return false;
for (int i=5; i*i<=n; i=i+6)
if (n%i == 0 || n%(i+2) == 0)
return false;
return true;
}
public static long gcd(long a,long b){
if(b%a == 0){
return a;
}
return gcd(b%a,a);
}
// public static void swap(int i,int j){
// long temp = arr[j];
// arr[j] = arr[i];
// arr[i] = temp;
// }
static final Random random=new Random();
static void ruffleSort(long[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n);
long temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static class Node{
Integer first;
Integer second;
Node(Integer f,Integer s){
this.first = f;
this.second = s;
}
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
} | JAVA |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const long long mod[] = {(long long)1e9 + 7, (long long)1e9 + 9,
(long long)1e9 + 21, (long long)1e9 + 33};
const int maxn = 2e3 + 5;
const int Nmod = 3;
string a, b;
int i, j, n, m, q;
long long Pow[Nmod][maxn];
static long long Hash[Nmod][maxn], Hash1[Nmod][maxn], Key[Nmod];
static int dp[maxn][maxn];
long long gethash(int i, int j, int type) {
return (Hash[type][j] - Hash[type][i - 1] * Pow[type][j - i + 1] +
(long long)mod[type] * (long long)mod[type]) %
mod[type];
}
void init() {
cin >> n >> m >> q;
cin >> a;
a = " " + a;
for (int i = 0; i < Nmod; ++i) {
Pow[i][0] = 1;
for (int j = 1; j <= n; ++j) Pow[i][j] = (Pow[i][j - 1] * 31) % mod[i];
}
for (int i = 0; i < Nmod; ++i) {
Hash[i][0] = 0;
for (int j = 1; j <= n; ++j)
Hash[i][j] = (Hash[i][j - 1] * 31 + a[j] - 'a') % mod[i];
}
cin >> b;
b = " " + b;
for (int i = 0; i < Nmod; ++i) {
for (int j = 1; j <= m; ++j) {
Key[i] = (Key[i] * 31 + b[j] - 'a') % mod[i];
}
}
for (int t = (1); t <= (n); ++t) {
for (int i = t; i <= n - m + 1; ++i) {
int ok = 1;
for (int j = 0; j < Nmod; ++j) {
ok = min(ok, (int)(gethash(i, i + m - 1, j) == Key[j]));
}
dp[t][i + m - 1] = dp[t][i + m - 2] + ok;
}
}
for (int i = (1); i <= (q); ++i) {
int x, y;
cin >> x >> y;
cout << dp[x][y] << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
init();
return 0;
}
| CPP |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 ≤ i ≤ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 ≤ n, m ≤ 10^3, 1 ≤ q ≤ 10^5) — the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 ≤ l_i ≤ r_i ≤ n) — the arguments for the i-th query.
Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
int main() {
ios_base::sync_with_stdio(false);
int n, m, q, l, r, tlen, cnt;
char s[N], t[N];
bool match[N] = {0};
cin >> n >> m >> q >> s >> t;
tlen = strlen(t);
for (int i = 0; s[i]; ++i) {
match[i] = !strncmp(s + i, t, tlen);
}
while (q-- > 0) {
cin >> l >> r;
--l;
--r;
cnt = 0;
for (int i = l; i <= r - tlen + 1; ++i) {
cnt += match[i];
}
cout << cnt << "\n";
}
return 0;
}
| 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;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int q = Integer.parseInt(st.nextToken());
String s = br.readLine();
String t = br.readLine();
List<Segment> containingSegments = findSubstringSegments(s, t);
Segment[] segments = new Segment[q];
for (int i = 0; i < q; ++i) {
st = new StringTokenizer(br.readLine());
segments[i] = new Segment(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
}
for (int i = 0; i < q; ++i) {
System.out.println(numberOfContainingSegments(containingSegments, segments[i]));
}
}
private static int numberOfContainingSegments(List<Segment> containingSegments, Segment segment) {
int result = 0;
for (int i = 0; i < containingSegments.size(); ++i) {
if (segment.start <= containingSegments.get(i).start
&& segment.end >= containingSegments.get(i).end)
result++;
}
return result;
}
private static List<Segment> findSubstringSegments(String s, String t) {
List<Segment> segments = new ArrayList<>();
if (s.length() < t.length())
return segments;
for (int i = 0; i < s.length(); ++i) {
boolean contains = true;
for (int j = 0; j < t.length(); ++j) {
if (s.length() > i + j && s.charAt(i + j) != t.charAt(j)) {
contains = false;
break;
}
}
if (contains) {
segments.add(new Segment(i + 1, i + t.length()));
}
}
return segments;
}
}
class Segment {
int start;
int end;
public Segment(int start, int end) {
this.start = start;
this.end = end;
}
}
| JAVA |