Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int n, m, q, sum[1001];
string s1, s2;
int main() {
cin >> n >> m >> q >> s1 >> s2;
for (int i = 1; i <= n; i += 1) {
sum[i] += sum[i - 1];
if (i > n - m + 1) {
continue;
}
bool f = true;
for (int j = i; j < i + m; j += 1) {
if (s1[j - 1] != s2[j - i]) {
f = false;
break;
}
}
if (f) {
sum[i + m - 1] += 1;
}
}
for (int i = 0; i < q; i += 1) {
int a, b;
cin >> a >> b;
if (a + m - 2 > b) {
cout << 0 << endl;
continue;
}
cout << sum[b] - sum[a + m - 2] << endl;
}
}
| CPP |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import java.util.*;
public class B48{
public static void main(String[] args){
Scanner inp=new Scanner(System.in);
int n=inp.nextInt();
int m=inp.nextInt();
int q=inp.nextInt();
String s=inp.next();
String t=inp.next();
if (n<m){
for(int i=0;i<q;i++){
System.out.println(0);
}
System.exit(0);
}
int[] ans=new int[n-m+1];
for(int i=0; i<n-m+1;i++){
if (s.substring(i,i+m).equals(t)){
ans[i]=1;
}
else{
ans[i]=0;
}}
for(int i=0;i<q;i++){
int a=inp.nextInt();
int b=inp.nextInt();
int pri=0;
for(int ii=a-1;ii<b+1-m;ii++){
pri+=ans[ii];
}
System.out.println(pri);
}
}
} | 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 = list(map(int,input().split()))
a = list(input())
b = list(input())
s = 0
starts = []
arr = []
for i in range(len(a) - len(b)+1):
if a[i:i+len(b)] == b:
arr.append(1)
s+=1
else:
arr.append(0)
starts.append(s)
for i in range(q):
l,r = list(map(int,input().split()))
if (r-l+1 < len(b)):
print(0)
continue
print(starts[r-len(b)] - starts[l-1] + arr[l-1]) | PYTHON3 |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import java.io.*;
import java.util.*;
import java.text.*;
import java.lang.*;
import java.math.BigInteger;
import java.util.regex.*;
public class Myclass {
/*public static ArrayList a[]=new ArrayList[200001];
static boolean visited[]=new boolean [200001];
static long value[];
static long maxi[];
static long dp[]=new long[200001];
static long ans;
static void dfs(pair n,int p)
{
visited[n.x]=true;
dp[n.x]=Math.max(dp[p]+n.y,n.y);
if(dp[n.x]>value[n.x])
{
ans++;
return;
}
for(int i=0;i<a[n.x].size();i++)
{
pair y=(pair) a[n.x].get(i);
if(!visited[y.x])
{
dfs(y,n.x);
}
}
}*/
public static void main(String[] args)
{
InputReader in = new InputReader(System.in);
PrintWriter pw = new PrintWriter(System.out);
int n=in.nextInt();
int m=in.nextInt();
int q=in.nextInt();
String s=in.nextLine();
String t=in.nextLine();
int dp[][]=new int[n+1][n+1];
TreeSet<Integer>tr=new TreeSet<>();
for(int i=0;i<=n-m;i++) {
String temp=s.substring(i, i+m);
if(temp.equals(t))
{
tr.add(i);
}
}
for(int i=1;i<=n;i++) {
for(int j=i;j<=n;j++) {
if(tr.contains(j-m) && i<=j-m+1)
{
dp[i][j]=dp[i][j-1]+1;
}
else
dp[i][j]=dp[i][j-1];
}
//pw.println(Arrays.toString(dp[i]));
}
for(int i=0;i<q;i++) {
int l=in.nextInt();
int r=in.nextInt();
pw.println(dp[l][r]);
}
pw.flush();
pw.close();
}
static class InputReader
{
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int snext()
{
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars)
{
curChar = 0;
try
{
snumChars = stream.read(buf);
}
catch (IOException e)
{
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = snext();
}
int res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = snext();
}
long res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n)
{
int a[] = new int[n];
for (int i = 0; i < n; i++)
{
a[i] = nextInt();
}
return a;
}
static class tri implements Comparable<tri> {
int p, c, l;
tri(int p, int c, int l) {
this.p = p;
this.c = c;
this.l = l;
}
public int compareTo(tri o) {
return Integer.compare(l, o.l);
}
}
public String readString()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine()
{
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c)
{
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
}
public static long mod = 1000000007;
public static int d;
public static int p;
public static int q;
public static int[] suffle(int[] a,Random gen)
{
int n = a.length;
for(int i=0;i<n;i++)
{
int ind = gen.nextInt(n-i)+i;
int temp = a[ind];
a[ind] = a[i];
a[i] = temp;
}
return a;
}
public static void swap(int a, int b){
int temp = a;
a = b;
b = temp;
}
public static HashSet<Integer> primeFactorization(int n)
{
HashSet<Integer> a =new HashSet<Integer>();
for(int i=2;i*i<=n;i++)
{
while(n%i==0)
{
a.add(i);
n/=i;
}
}
if(n!=1)
a.add(n);
return a;
}
public static void sieve(boolean[] isPrime,int n)
{
for(int i=1;i<n;i++)
isPrime[i] = true;
isPrime[0] = false;
isPrime[1] = false;
for(int i=2;i*i<n;i++)
{
if(isPrime[i] == true)
{
for(int j=(2*i);j<n;j+=i)
isPrime[j] = false;
}
}
}
public static int GCD(int a,int b)
{
if(b==0)
return a;
else
return GCD(b,a%b);
}
public static long GCD(long a,long b)
{
if(b==0)
return a;
else
return GCD(b,a%b);
}
public static void extendedEuclid(int A,int B)
{
if(B==0)
{
d = A;
p = 1 ;
q = 0;
}
else
{
extendedEuclid(B, A%B);
int temp = p;
p = q;
q = temp - (A/B)*q;
}
}
public static long LCM(long a,long b)
{
return (a*b)/GCD(a,b);
}
public static int LCM(int a,int b)
{
return (a*b)/GCD(a,b);
}
public static int binaryExponentiation(int x,int n)
{
int result=1;
while(n>0)
{
if(n % 2 ==1)
result=result * x;
x=x*x;
n=n/2;
}
return result;
}
public static long binaryExponentiation(long x,long n)
{
long result=1;
while(n>0)
{
if(n % 2 ==1)
result=result * x;
x=x*x;
n=n/2;
}
return result;
}
public static int modularExponentiation(int x,int n,int M)
{
int result=1;
while(n>0)
{
if(n % 2 ==1)
result=(result * x)%M;
x=(x%M*x)%M;
n=n/2;
}
return result;
}
public static long modularExponentiation(long x,long n,long M)
{
long result=1;
while(n>0)
{
if(n % 2 ==1)
result=(result%M * x%M)%M;
x=(x%M * x%M)%M;
n=n/2;
}
return result;
}
public static long modInverse(int A,int M)
{
return modularExponentiation(A,M-2,M);
}
public static long modInverse(long A,long M)
{
return modularExponentiation(A,M-2,M);
}
public static boolean isPrime(int n)
{
if (n <= 1) return false;
if (n <= 3) return true;
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[] shuffle(long[] a, Random gen){
for(int i = 0, n = a.length;i < n;i++){
int ind = gen.nextInt(n-i)+i;
long d = a[i];
a[i] = a[ind];
a[ind] = d;
}
return a;
}
static class pair implements Comparable<pair>
{
Integer x;
Integer y;
pair(int a,int m)
{
this.x=a;
this.y=m;
}
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
public int compareTo(pair o) {
int result = x.compareTo(o.x);
if(result==0)
result = y.compareTo(o.y);
return result;
}
public String toString()
{
return x+" "+y;
}
public boolean equals(Object o)
{
if (o instanceof pair)
{
pair p = (pair)o;
return p.x == x && p.y == y ;
}
return false;
}
public int hashCode()
{
return new Long(x).hashCode()*31 + new Long(y).hashCode();
}
}
static class triplet
{
Long x;
Long y;
Integer z;
triplet(long x,long y,int z)
{
this.x=x;
this.y=y;
this.z=z;
}
}
static class jodi
{
Integer x;
Integer y;
jodi(int x,int y)
{
this.x=x;
this.y=y;
}
}
} | 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 | #input
n, m, q = map(int, input().split())
s = input()
t = input()
#pre-process
dp = [0]*(n+1) #dp[x] : number of occurrences of t in s[0:x]
found_so_far = 0
for i in range(n-m+1):
if s[i:i+m] == t:
found_so_far += 1
dp[i+m] = found_so_far
#process query
for _ in range(q):
a, b = map(int, input().split())
if b - a + 1 < m:
print(0)
else:
print(dp[b] - dp[a+m-2])
| PYTHON3 |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int n, m, q;
char s[1005], t[1005];
int sum[1005];
int main() {
scanf("%d%d%d", &n, &m, &q);
scanf(" %s %s", s + 1, t + 1);
for (int i = 1; i <= n; i++) {
int good = 1;
for (int j = 1; j <= m; j++) {
if (s[i + j - 1] != t[j]) good = 0;
}
sum[i] = sum[i - 1] + good;
}
while (q--) {
int l, r;
scanf("%d%d", &l, &r);
r = max(l - 1, r - m + 1);
printf("%d\n", sum[r] - sum[l - 1]);
}
}
| CPP |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import java.util.*;
import java.io.*;
import java.text.*;
import java.math.*;
import static java.lang.Integer.*;
import static java.lang.Double.*;
import java.lang.Math.*;
public class segment_occurrences {
public static void main(String[] args) throws Exception {
new segment_occurrences().run();
}
public void run() throws Exception {
FastIO file = new FastIO();
int n = file.nextInt();
int m = file.nextInt();
int q = file.nextInt();
String s = file.next();
String t = file.next();
ArrayList<Integer> a = new ArrayList<>();
for (int i = 0; i <= n - m; i++) {
if (s.substring(i, i + m).equals(t)) {
a.add(i);
a.add(i + m - 1);
}
}
while (q-->0) {
int aa = file.nextInt() - 1;
int bb = file.nextInt() - 1;
int count = 0;
for (int i = 0; i < a.size(); i+=2) {
if (a.get(i) >= aa && a.get(i) <= bb && a.get(i + 1) >= aa && a.get(i + 1) <= bb) count++;
}
System.out.println(count);
}
}
public static class FastIO {
BufferedReader br;
StringTokenizer st;
public FastIO() {
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 long pow(long n, long p, long mod) {
if (p == 0)
return 1;
if (p == 1)
return n % mod;
if (p % 2 == 0) {
long temp = pow(n, p / 2, mod);
return (temp * temp) % mod;
} else {
long temp = pow(n, (p - 1) / 2, mod);
temp = (temp * temp) % mod;
return (temp * n) % mod;
}
}
public static long pow(long n, long p) {
if (p == 0)
return 1;
if (p == 1)
return n;
if (p % 2 == 0) {
long temp = pow(n, p / 2);
return (temp * temp);
} else {
long temp = pow(n, (p - 1) / 2);
temp = (temp * temp);
return (temp * n);
}
}
public static long gcd(long x, long y) {
if (x == 0)
return y;
else
return gcd(y % x, x);
}
public static boolean isPrime(int n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
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;
}
}
| 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 dp[2000];
char str[2000], tr[2000];
int main() {
int i, j, k, l, m, n, q;
scanf("%d%d%d", &n, &m, &q);
scanf("%s", str + 1);
scanf("%s", tr + 1);
for (int i = 1; str[i]; i++) {
for (j = 1, k = i; tr[j] && str[k]; j++, k++) {
if (tr[j] != str[k]) break;
}
dp[i] += dp[i - 1];
if (j <= m) continue;
dp[i]++;
}
while (q--) {
scanf("%d%d", &l, &k);
if ((k - l + 1) < m) {
printf("0\n");
continue;
}
int v = k - m + 1;
int ans = dp[v] - dp[l - 1];
cout << ans << endl;
}
}
| CPP |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1001;
int a[maxn], cnt = 0;
char s1[maxn], s2[maxn];
int main() {
int n, m, q;
scanf("%d%d%d", &n, &m, &q);
cin >> s1 >> s2;
for (int i = 0; i < n; i++) {
if (s1[i] == s2[0]) {
bool fa = 1;
int now = i;
for (int j = 1; j < m; j++) {
now++;
if (s2[j] != s1[now]) {
fa = 0;
break;
}
}
if (fa == 1) {
a[cnt++] = i + 1;
}
}
}
while (q--) {
int u, v;
scanf("%d%d", &u, &v);
int sum = 0;
for (int i = 0; i < cnt; i++) {
if (u <= a[i] && v >= a[i] + m - 1) sum++;
}
printf("%d\n", sum);
}
return 0;
}
| CPP |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import sys
n,m,q=map(int,sys.stdin.readline().split())
s=input()
t=input()
i=0
j=0
p=[0]*(n+1)
res = [i for i in range(len(s)) if s.startswith(t, i)]
for i in range(len(res)):
p[res[i]+1]=1
for i in range(2,n+1):
p[i]+=p[i-1]
#print(p)
for _ in range(q):
l,r=map(int,sys.stdin.readline().split())
r=r-m+1
ans=p[max(r,0)]-p[l-1]
print(max(ans,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 os, sys
from io import BytesIO, IOBase
def main():
n, m, q = rints()
s, t, cum = rstr(), rstr(), []
for i in range(n - m + 1):
if s[i:i + m] == t:
cum.append((i + 1, i + m))
for i in range(q):
l, r = rints()
ans = 0
for l1, r1 in cum:
if l <= l1 and r >= r1:
ans += 1
print(ans)
input = lambda: sys.stdin.readline().rstrip("\r\n")
rstr = lambda: input().strip()
rstrs = lambda: [str(x) for x in input().split()]
rstr_2d = lambda n: [rstr() for _ in range(n)]
rint = lambda: int(input())
rints = lambda: [int(x) for x in input().split()]
rint_2d = lambda n: [rint() for _ in range(n)]
rints_2d = lambda n: [rints() for _ in range(n)]
ceil1 = lambda a, b: (a + b - 1) // b
if __name__ == '__main__':
main()
| PYTHON3 |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | n,m,q=map(int,input().split())
s=input()
t=input()
k=[0]*(n+1)
for i in range(1,n+1):
k[i]=k[i-1]
if s[i-1:i+m-1]==t:k[i]+=1
for i in range(q):
l,r=map(int,input().split())
print(k[max(l-1,r-m+1)]-k[l-1])
| PYTHON3 |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | n,m, q = map(int, input().split())
s = str(input())
t = str(input())
def is_in(index):
if index + m > n:
return False
for j in range(m):
if not t[j] == s[index + j]:
return False
return True
precalc_ar_2 = [0 for i in range(n +1)]
in_was = [ False for i in range(n)]
for i in range(n):
if is_in(i):
in_was[i] = True
val = 0
for i in range(1, n + 1):
if in_was[i - 1]:
val +=1
precalc_ar_2[i] = val
for i in range(q):
l, r = map(int,input().split())
if r- m + 1 < 0 or r- m + 1 < l - 1:
print(0)
else:
print(precalc_ar_2[r - m +1] - precalc_ar_2[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 | [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]
# print(a)
for i in range(q):
[l,r]=input().split()
l=int(l)
r=int(r)
# r-=1
# l-=1
# if r-m+1>=0 and r-l +1 >=m :
if (r - l + 1 < m):
print(0)
else:
print(a[r-m+1]-a[l-1]) | PYTHON3 |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
long long z[3000019], arr[3000019], pre[3000019];
void fnc(string s) {
memset(z, 0, sizeof(z));
long long l = 0, r = 0;
long long n = s.size();
for (long long i = 1; i <= n; i++) {
if (i <= r) z[i] = min(r - i + 1, z[i - l]);
while (i + z[i] <= n && s[z[i]] == s[i + z[i]]) z[i]++;
if (i + z[i] - 1 > r) {
l = i;
r = i + z[i] - 1;
}
}
z[0] = n;
return;
}
signed main() {
std::ios::sync_with_stdio(false);
long long n, m, q, i, l, r;
string s, t, str;
cin >> n >> m >> q;
cin >> s >> t;
str = t + "$" + s;
fnc(str);
for (i = m + 1; i <= n + m; i++) arr[i - m] = (z[i] == m);
for (i = 1; i <= n; i++) pre[i] = arr[i] + pre[i - 1];
while (q--) {
cin >> l >> r;
if (r - l + 1 < m) {
cout << 0 << endl;
continue;
}
cout << pre[r - m + 1] - pre[l - 1] << endl;
}
return 0;
}
| CPP |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main(int argc, const char* argv[]) {
int64_t n, m, q;
string s, t;
cin >> n >> m >> q >> s >> t;
vector<pair<int, int>> ins;
for (int i = 0; i < s.size(); ++i) {
if (i + t.size() - 1 < s.size() && s.substr(i, t.size()) == t) {
ins.push_back(make_pair(i, i + t.size() - 1));
}
}
for (int i = 0; i < q; ++i) {
int64_t l, r, cnt = 0;
cin >> l >> r;
--l;
--r;
for (auto now : ins) {
if (now.first >= l && now.second <= r) {
++cnt;
}
}
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.util.Scanner;
public class Contest {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = Integer.valueOf(in.next());
int m = Integer.valueOf(in.next());
int q = Integer.valueOf(in.next());
String s = in.next();
String t = in.next();
int[] tInS = new int[n];
for (int i = 0; i < Math.max(n - m + 1, 0); i++) {
if (s.startsWith(t, i)) {
tInS[i]++;
}
}
for (int i = 0; i < q; i++) {
int l = Integer.valueOf(in.next());
int r = Integer.valueOf(in.next());
int res = 0;
for (int j = l - 1; j < Math.max(r - m + 1, l - 1); j++) {
res += tInS[j];
}
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.*;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Scanner;
public class Main {
static int N=1010;
static int[] num=new int[N];
static int[] sum=new int[N];
static int n,m,k,q,tot,root,ans,ed;
static StreamTokenizer in=new StreamTokenizer(new BufferedReader((new InputStreamReader(System.in))));
static int nextInt() throws IOException{
in.nextToken();
return (int)in.nval;
}
static String next() throws IOException{
in.nextToken();
return in.sval;
}
static PrintWriter out =new PrintWriter(new OutputStreamWriter(System.out));
public static void main(String args[])throws IOException{
n=nextInt();
m=nextInt();
q=nextInt();
String ss=next();
String tt=next();
char[] s=ss.toCharArray();
char[] t=tt.toCharArray();
for(int i=0;i<=n-m;i++){
boolean can=true;
for(int j=0;j<m;j++){
if(s[i+j]!=t[j]){
can=false;
break;
}
}
if(can){
num[i+1]=1;
}
}
for(int i=1;i<=n;i++){
sum[i]=sum[i-1]+num[i];
}
for(int i=1;i<=q;i++){
int l=nextInt();
int r=nextInt();
if(r-m+1>l-1)out.println(sum[r-m+1]-sum[l-1]);
else out.println(0);
out.flush();
}
}
} | JAVA |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int N = in.nextInt();
int M = in.nextInt();
int q = in.nextInt();
char[] s = in.next().toCharArray();
char[] t = in.next().toCharArray();
boolean[] good = new boolean[N];
for (int i = 0; i < N; i++) {
good[i] = isSubstring(s, t, i);
}
while (q-- > 0) {
int li = in.nextInt() - 1;
int ri = in.nextInt() - 1;
long cnt = 0;
for (int i = li; i <= ri - t.length + 1; i++) {
if (good[i]) {
cnt++;
}
}
out.println(cnt);
}
}
boolean isSubstring(char[] s, char[] t, int start) {
if (start + t.length - 1 >= s.length) {
return false;
}
for (int i = start; i < start + t.length; i++) {
if (s[i] != t[i - start]) {
return false;
}
}
return true;
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| JAVA |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, q;
cin >> n >> m >> q;
string s, t;
cin >> s >> t;
vector<int> I;
for (int i = 0; i < n - m + 1; i++) {
if (t == s.substr(i, m)) I.push_back(i);
}
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
l--;
r--;
int ans = 0;
for (int j = 0; j < I.size(); j++) {
if (I[j] < l)
continue;
else if (I[j] + m - 1 > r)
break;
else
ans++;
}
cout << ans << endl;
}
return 0;
}
| CPP |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | from sys import stdin, stdout
ti = lambda : stdin.readline().strip()
ma = lambda fxn, ti : map(fxn, ti.split())
ol = lambda arr : stdout.write(' '.join(str(i) for i in arr) + '\n')
os = lambda i : stdout.write(str(i) + '\n')
olws = lambda arr : stdout.write(''.join(str(i) for i in arr) + '\n')
import math
n, m, q = ma(int, ti())
s = ti()
t = ti()
ans = [False] * n
for i in range(1, n-m+2):
if s[i-1:i+m-1] == t:
ans[i-1] = True
for i in range(q):
l, r = ma(int, ti())
temp = 0
while l<=r-m+1:
if ans[l-1]:
temp += 1
l += 1
os(temp) | PYTHON |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
void solve() {
int n, m, q;
cin >> n >> m >> q;
string s, t;
cin >> s >> t;
vector<int> v;
for (int i = 0; i < int((s).size()); i++) {
int j;
for (j = 0; j < int((t).size()) && (i + j) < int((s).size()); j++) {
if (s[j + i] != t[j]) break;
}
if (j == t.size()) v.push_back(i);
}
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
l--;
r--;
r = r - (int)t.size() + 1;
if (l > r) {
cout << 0 << endl;
continue;
}
int ind1 = lower_bound((v).begin(), (v).end(), l) - v.begin();
int ind2 = upper_bound((v).begin(), (v).end(), r) - v.begin();
cout << (ind2 - ind1) << endl;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int kickstart = 0;
int test = 1;
for (int i = 0; i < test; i++) {
if (kickstart)
cout << "Case #" << i + 1 << ": ", solve(), cout << endl;
else
solve();
}
return 0;
}
| CPP |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws UnsupportedEncodingException, IOException {
Reader.init(System.in);
StringBuilder out = new StringBuilder();
int n = Reader.nextInt(), m = Reader.nextInt(), q = Reader.nextInt();
if(m > n) {
while(q-- > 0)
out.append("0\n");
PrintWriter pw = new PrintWriter(System.out);
pw.print(out);
pw.close();
return;
}
String s = Reader.nextLine(), t = Reader.nextLine();
int[] sum = new int[n + 1];
for(int i = 0, j; i < n; i++) {
for(j = 0; j < m; j++)
if(i + j >= n || s.charAt(i + j) != t.charAt(j))
break;
if(j == m)
sum[i + 1]++;
}
for(int i = 1; i < sum.length; i++)
sum[i] += sum[i - 1];
int l, r, c, j;
while(q-- > 0) {
l = Reader.nextInt();
r = Reader.nextInt();
c = sum[r] - sum[l - 1];
for(j = r - m + 2; j <= r; j++) {
if(j < l)
continue;
if(sum[j] > sum[j - 1])
c--;
}
out.append(c).append('\n');
}
PrintWriter pw = new PrintWriter(System.out);
pw.print(out);
pw.close();
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
static void init(InputStream input) throws UnsupportedEncodingException {
reader = new BufferedReader(
new InputStreamReader(input, "UTF-8"));
tokenizer = new StringTokenizer("");
}
static void init(String url) throws FileNotFoundException {
reader = new BufferedReader(new FileReader(url));
tokenizer = new StringTokenizer("");
}
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine());
}
return tokenizer.nextToken();
}
static String nextLine() throws IOException {
return reader.readLine();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
} | JAVA |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int n, m, q, dp[1010][1010];
char s[1010], t[1010];
bool used[1010];
int main() {
scanf("%d%d%d", &n, &m, &q);
scanf("%s", s + 1);
scanf("%s", t);
for (int i = 1; i + m - 1 <= n; i++) {
bool f = true;
for (int j = 0; j < m; j++) {
if (s[i + j] != t[j]) {
f = false;
break;
}
}
used[i] = f;
}
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j++) {
dp[i][j] = max(dp[i][j], dp[i][j - 1]);
if (j - m + 1 >= i && used[j - m + 1]) {
dp[i][j]++;
}
}
}
while (q--) {
int l, r;
scanf("%d%d", &l, &r);
printf("%d\n", dp[l][r]);
}
return 0;
}
| CPP |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | from sys import stdin, stdout
from itertools import repeat
def main():
n, m, q = map(int, stdin.readline().split())
s = stdin.readline().strip()
t = stdin.readline().strip()
dat = map(int, stdin.read().split(), repeat(10, 2 * q))
f = [-1]
for i, c in enumerate(t):
j = f[-1]
while j != -1 and c != t[j]:
j = f[j]
f.append(j + 1)
a = [0] * (n + 1)
p = 0
for i, c in enumerate(s):
while p != -1 and c != t[p]:
p = f[p]
p += 1
if p == m:
a[i+1] = a[i] + 1
p = f[p]
else:
a[i+1] = a[i]
ans = [0] * q
for i in xrange(q):
l, r = dat[i*2] - 1, dat[i*2+1]
if r - l >= m:
ans[i] = a[r] - a[l+m-1]
stdout.write('\n'.join(map(str, ans)))
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 | n, m, q = [int(x) for x in raw_input().split()]
s = raw_input()
t = raw_input()
idx = list()
if s[:m] == t:
idx.append(1)
else:
idx.append(0)
for i in xrange(1,n-m+1):
if s[i:i+m] == t:
idx.append(idx[i-1]+1)
else:
idx.append(idx[i-1])
for i in xrange(q):
l, r = [int(x)-1 for x in raw_input().split()]
if r-l+1 < m:
print 0
elif r-m+1 >= n-m+1:
print 0
else:
k = idx[l-1] if l > 0 else 0
print idx[r-m+1] - k
| PYTHON |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, q;
cin >> n >> m >> q;
string s, t;
cin >> s >> t;
vector<bool> occ(n);
for (int i = 0; i < n; i++) {
if (s.substr(i, m) == t) {
occ[i] = 1;
}
}
vector<int> prefixOcc(n + 1);
prefixOcc[0] = 0;
for (int i = 1; i <= n; i++) {
prefixOcc[i] = prefixOcc[i - 1] + occ[i - 1];
}
while (q--) {
int left, right;
cin >> left >> right;
left--;
right--;
if (right - left + 1 < m or right - m + 1 < 0) {
cout << '0' << endl;
;
continue;
}
int prefLeft = prefixOcc[left];
int prefRight = prefixOcc[right - m + 2];
cout << prefRight - prefLeft << 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 | 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])
# Made By Mostafa_Khaled | 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 SegmentOccurrences
{
public static void main(String[] p)
{
int n,m,q,i,j;
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
m=sc.nextInt();
q=sc.nextInt();
int[] a=new int[n];
String s=sc.next();
String s1=sc.next();
HashSet<Integer> hs=new HashSet<>();
for(i=0;i<(n-m+1);i++)
{
hs.add(s.indexOf(s1,i));
}
for(int g:hs)
{
if(g>=0)
a[g]++;
}
for(i=0;i<q;i++)
{
long count=0;
int L=sc.nextInt();
int R=sc.nextInt();
if(hs.size()!=0)
{
for(j=(L-1);j<=(R-1);j++)
{
if((R-j)>=m && a[j]==1)
count++;
}
}
System.out.println(count);
}
}
} | JAVA |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
string s, t;
long long m, n, q, dp[1010][1010];
int main() {
cin >> n >> m >> q;
cin >> s >> t;
for (int i = 0; i <= n - m; i++) {
bool b = false;
for (int j = i; j < i + m; j++) {
if (s[j] != t[j - i]) b = true;
}
if (b == false) {
dp[i][i + m] = 1;
}
}
for (int i = 0; i <= n; i++) {
for (int j = 0; j < n; j++) {
if (i >= m) {
dp[j][j + i] = dp[j][j + i] + dp[j][j + i - 1] + dp[j + 1][j + i] -
dp[j + 1][j + i - 1];
}
}
}
for (int i = 0, a, b; i < q; i++) {
cin >> a >> b;
cout << dp[a - 1][b] << endl;
}
}
| CPP |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
long long MOD = 1e9 + 7;
vector<long long> build(string s) {
int n = s.size();
vector<long long> kmp(n, 0);
for (int i = 1; i < n; i++) {
int j = kmp[i - 1];
while (j > 0 && s[i] != s[j]) {
j = kmp[j - 1];
}
if (s[i] == s[j]) j++;
kmp[i] = j;
}
return kmp;
}
void icchhipadey() {
long long n, m, q;
cin >> n >> m >> q;
string s1, s2;
cin >> s1 >> s2;
while (q--) {
long long l, r;
cin >> l >> r;
string s = s1.substr(l - 1, r - l + 1);
long long ind = 0;
long long cnt = 0;
string temp = s2 + "$" + s;
vector<long long> kmp = build(temp);
for (auto x : kmp) {
if (x == m) {
cnt++;
}
}
cout << cnt << endl;
}
}
int main() { icchhipadey(); }
| CPP |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import java.util.Scanner;
public class B48Ecr{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int q = sc.nextInt();
String s = sc.next();
String s1 = sc.next();
int l = 0,r = 0,z = 0;
int a[] = new int[n];
for(int i = 0;i < n - m + 1;i++){
String sub = s.substring(i,i + m);
if(sub.equals(s1)){
a[z] = i + 1;
z++;
}
}
for(int i = 0;i < q;i++){
l = sc.nextInt();
r = sc.nextInt();
int count = 0;
for(int j = 0;j < z;j++){
if((a[j]) >= l && (a[j] + m - 1) <= r){
count++;
}
}
System.out.print(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 |
import java.util.*;
import java.io.*;
import java.math.*;
public class Main
{
private static final Comparator<? super Integer> Comparator = null;
static LinkedList<Integer> adj[];
static ArrayList<Integer> adj1[];
static int[] color,visited1;
static boolean b[],visited[],possible;
static int level[];
static Map<Integer,HashSet<Integer>> s;
static int totalnodes,colored;
static int count[];
static long sum[];
static int nodes;
static long ans=0;
static long[] as=new long[10001];
static long c1=0,c2=0;
static int[] a,d,k;
static int max=100000000;
static long MOD = 1000000007,sm=0,m=Long.MIN_VALUE;
static boolean[] prime=new boolean[1000005];
static int[] levl;
static int[] eat;
static int price[];
static int res[],par[],co[];
static int result=0;
static int[] root,size,du,dv;
static long p=Long.MAX_VALUE;
static int start,end,r=0;
static boolean[] vis1,vis2;
static int to;
static HashMap<Pair,Integer> hs;
static boolean ns;
static String st,t;
static long n;
// --------------------My Code Starts Here----------------------
public static void main(String[] args) throws IOException
{
in=new InputReader(System.in);
w=new PrintWriter(System.out);
int n=ni(),m=ni(),q=ni();
char[] s=ns().toCharArray();
char[] t=ns().toCharArray();
long[] dp=new long[n+1];
for(int i=0;i<=n-m;i++)
{
boolean ans=true;
for(int j=0;j<m;j++)
{
if(s[i+j]!=t[j])
{
ans=false;
break;
}
}
if(ans)
dp[i+m]++;
}
for(int i=1;i<=n;i++)
dp[i]=dp[i]+dp[i-1];
while(q-->0)
{
int l=ni(),r=ni();
if(l+m-2<=r)
w.println(dp[r]-dp[l+m-2]);
else
w.println(0);
}
w.close();
}
// --------------------My Code Ends Here------------------------
static class Pair implements Comparable<Pair>
{
Long a;
Long b;
Long dif;
Pair(long a,long b)
{
this.a=a;
this.b=b;
//this.l=l;
this.dif=a-b;
}
public int compareTo(Pair p)
{
return Long.compare(p.dif,this.dif);
}
}
public static void swap(int[] a,int l,int r)
{
int t=a[l];
a[l]=a[r];
a[r]=t;
}
public static void swap(long[] a,int l,int r)
{
long t=a[l];
a[l]=a[r];
a[r]=t;
}
public static void sort(long[] a)
{
ArrayList<Long> al=new ArrayList<Long>();
for(int i=0;i<a.length;i++)
al.add(a[i]);
Collections.sort(al);
for(int i=0;i<a.length;i++)
a[i]=al.get(i);
}
public static long pow(long a, long b, long c) {
if (b == 0)
return 1;
long p = pow(a, b / 2, c);
p = (p * p) % c;
return (b % 2 == 0) ? p : (a * p) % c;
}
/*
* PriorityQueue<Integer> pq = new PriorityQueue<Integer>(new Comparator<Integer>()
{
public int compare(Integer o1, Integer o2)
{
return Intege
r.compare(o2,o1);
}
});
*
*
*/
public static void shuffle(long[] a,int n)
{
Random r=new Random();
for(int i=n-1;i>0;i--)
{
int j=r.nextInt(i);
swap(a,j,i);
}
}
public static void buildgraph(int n)
{
adj=new LinkedList[n+1];
visited=new boolean[n];
level=new int[n];
par=new int[n];
for(int i=0;i<=n;i++)
{
adj[i]=new LinkedList<Integer>();
}
}
/*public static long kruskal(Pair[] p)
{
long ans=0;
int w=0,x=0,y=0;
for(int i=0;i<p.length;i++)
{
w=p[i].w;
x=p[i].x;
y=p[i].y;
if(root(x)!=root(y))
{
ans+=w;
union(x,y);
}
}
return ans;
}*/
public static int root(int i)
{
while(root[i]!=i)
{
root[i]=root[root[i]];
i=root[i];
}
return i;
}
public static void init(int n)
{
root=new int[n+1];
for(int i=1;i<=n;i++)
root[i]=i;
}
public static void union(int a,int b)
{
int root_a=root(a);
int root_b=root(b);
root[root_a]=root_b;
// size[root_b]+=size[root_a];
}
public static boolean isPrime(long n)
{
if (n <= 1) return false;
if (n <= 3) return true;
if (n%2 == 0 || n%3 == 0)
return false;
for (long i=5; i*i<=n; i=i+6)
if (n%i == 0 || n%(i+2) == 0)
return false;
return true;
}
public static String ns()
{
return in.nextLine();
}
public static int ni()
{
return in.nextInt();
}
public static long nl()
{
return in.nextLong();
}
public static int[] na(int n)
{
int[] a=new int[n];
for(int i=0;i<n;i++)
a[i]=ni();
return a;
}
public static long[] nla(int n)
{
long[] a=new long[n];
for(int i=0;i<n;i++)
a[i]=nl();
return a;
}
public static void sieve()
{
int n=prime.length;
for(int i=0;i<n;i++)
prime[i] = true;
for(int p = 2; p*p <=n; p++)
{
if(prime[p])
{
for(int i = p*2; i <n; i += p)
prime[i] = false;
}
}
}
public static String rev(String s)
{
StringBuilder sb=new StringBuilder(s);
sb.reverse();
return sb.toString();
}
/* static long lcm(long a, long b)
{
return a * (b / gcd(a, b));
}*/
static int gcd(int a, int b)
{
while (b > 0)
{
int temp = b;
b = a % b; // % is remainder
a = temp;
}
return a;
}
static InputReader in;
static PrintWriter w;
static class InputReader
{
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int snext()
{
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars)
{
curChar = 0;
try
{
snumChars = stream.read(buf);
}
catch (IOException e)
{
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = snext();
}
int res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = snext();
}
long res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
}
while (!isSpaceChar(c));
return res * sgn;
}
public String readString()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = snext();
}
while (!isSpaceChar(c));
return res.toString();
}
public String nextLine()
{
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = snext();
}
while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c)
{
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
}
} | JAVA |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | from itertools import accumulate
from sys import stdin
# all_in = list(el.rstrip('\n') for el in stdin.readlines())
all_in = list(map(lambda x: x.rstrip('\n'), stdin.readlines()))
n, m, q = map(int, all_in[0].split())
s = all_in[1]
t = all_in[2]
l_r = [tuple(map(int, el.split())) for el in all_in[3:]]
in_ = [int(t == s[i: i + m]) for i in range(n - m + 1)]
acc = [0] + list(accumulate(in_))
ans = list()
for a, b in l_r:
if b - a + 1 < m:
ans.append(0)
continue
ans.append(acc[max(0, b - m + 1)] - acc[a - 1])
print('\n'.join(map(str, ans)))
| PYTHON3 |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 20;
int sum[maxn];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m, q;
cin >> n >> m >> q;
string s, t;
cin >> s >> t;
for (int i = 0; i < n; i++)
sum[i + 1] = sum[i] + (i + m <= n && s.substr(i, m) == t);
while (q--) {
int l, r;
cin >> l >> r;
r -= m;
l--;
if (r < l)
cout << 0 << endl;
else
cout << sum[r + 1] - sum[l] << 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 re
from bisect import bisect_left as bl
from bisect import bisect_right as br
n, m, q = [int(x) for x in raw_input().split()]
s = str(raw_input())
t = str(raw_input())
a = [i.start() for i in re.finditer('(?=' + t + ')', s)]
# b = list(map(lambda x: int(x) + m, a))
for _ in xrange(q):
l, r = [int(x) for x in raw_input().split()]
if r - l + 1 < m:
print 0
continue
l -= 1
r -= 1
i = bl(a, l)
j = br(a, r-m+1)
print j - i
| 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 | ######### ## ## ## #### ##### ## # ## # ##
# # # # # # # # # # # # # # # # # # #
# # # # ### # # # # # # # # # # # #
# ##### # # # # ### # # # # # # # # #####
# # # # # # # # # # # # # # # # # #
######### # # # # ##### # ##### # ## # ## # #
"""
PPPPPPP RRRRRRR OOOO VV VV EEEEEEEEEE
PPPPPPPP RRRRRRRR OOOOOO VV VV EE
PPPPPPPPP RRRRRRRRR OOOOOOOO VV VV EE
PPPPPPPP RRRRRRRR OOOOOOOO VV VV EEEEEE
PPPPPPP RRRRRRR OOOOOOOO VV VV EEEEEEE
PP RRRR OOOOOOOO VV VV EEEEEE
PP RR RR OOOOOOOO VV VV EE
PP RR RR OOOOOO VV VV EE
PP RR RR OOOO VVVV EEEEEEEEEE
"""
"""
Perfection is achieved not when there is nothing more to add, but rather when there is nothing more to take away.
"""
import sys
input = sys.stdin.readline
# from bisect import bisect_left as lower_bound;
# from bisect import bisect_right as upper_bound;
# from math import ceil, factorial;
def ceil(x):
if x != int(x):
x = int(x) + 1
return x
def factorial(x, m):
val = 1
while x>0:
val = (val * x) % m
x -= 1
return val
def fact(x):
val = 1
while x > 0:
val *= x
x -= 1
return val
# swap_array function
def swaparr(arr, a,b):
temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
## gcd function
def gcd(a,b):
if b == 0:
return a;
return gcd(b, a % b);
## lcm function
def lcm(a, b):
return (a * b) // gcd(a, b)
## nCr function efficient using Binomial Cofficient
def nCr(n, k):
if k > n:
return 0
if(k > n - k):
k = n - k
res = 1
for i in range(k):
res = res * (n - i)
res = res / (i + 1)
return int(res)
## upper bound function code -- such that e in a[:i] e < x;
def upper_bound(a, x, lo=0, hi = None):
if hi == None:
hi = len(a);
while lo < hi:
mid = (lo+hi)//2;
if a[mid] < x:
lo = mid+1;
else:
hi = mid;
return lo;
## prime factorization
def primefs(n):
## if n == 1 ## calculating primes
primes = {}
while(n%2 == 0 and n > 0):
primes[2] = primes.get(2, 0) + 1
n = n//2
for i in range(3, int(n**0.5)+2, 2):
while(n%i == 0 and n > 0):
primes[i] = primes.get(i, 0) + 1
n = n//i
if n > 2:
primes[n] = primes.get(n, 0) + 1
## prime factoriazation of n is stored in dictionary
## primes and can be accesed. O(sqrt n)
return primes
## MODULAR EXPONENTIATION FUNCTION
def power(x, y, p):
res = 1
x = x % p
if (x == 0) :
return 0
while (y > 0) :
if ((y & 1) == 1) :
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
## DISJOINT SET UNINON FUNCTIONS
def swap(a,b):
temp = a
a = b
b = temp
return a,b;
# find function with path compression included (recursive)
# def find(x, link):
# if link[x] == x:
# return x
# link[x] = find(link[x], link);
# return link[x];
# find function with path compression (ITERATIVE)
def find(x, link):
p = x;
while( p != link[p]):
p = link[p];
while( x != p):
nex = link[x];
link[x] = p;
x = nex;
return p;
# the union function which makes union(x,y)
# of two nodes x and y
def union(x, y, link, size):
x = find(x, link)
y = find(y, link)
if size[x] < size[y]:
x,y = swap(x,y)
if x != y:
size[x] += size[y]
link[y] = x
## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES
def sieve(n):
prime = [True for i in range(n+1)]
prime[0], prime[1] = False, False
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
return prime
# Euler's Toitent Function phi
def phi(n) :
result = n
p = 2
while(p * p<= n) :
if (n % p == 0) :
while (n % p == 0) :
n = n // p
result = result * (1.0 - (1.0 / (float) (p)))
p = p + 1
if (n > 1) :
result = result * (1.0 - (1.0 / (float)(n)))
return (int)(result)
def is_prime(n):
if n == 0:
return False
if n == 1:
return True
for i in range(2, int(n ** (1 / 2)) + 1):
if not n % i:
return False
return True
#### PRIME FACTORIZATION IN O(log n) using Sieve ####
MAXN = int(1e5 + 5)
def spf_sieve():
spf[1] = 1;
for i in range(2, MAXN):
spf[i] = i;
for i in range(4, MAXN, 2):
spf[i] = 2;
for i in range(3, ceil(MAXN ** 0.5), 2):
if spf[i] == i:
for j in range(i*i, MAXN, i):
if spf[j] == j:
spf[j] = i;
## function for storing smallest prime factors (spf) in the array
################## un-comment below 2 lines when using factorization #################
spf = [0 for i in range(MAXN)]
# spf_sieve();
def factoriazation(x):
res = []
for i in range(2, int(x ** 0.5) + 1):
while x % i == 0:
res.append(i)
x //= i
if x != 1:
res.append(x)
return res
## this function is useful for multiple queries only, o/w use
## primefs function above. complexity O(log n)
def factors(n):
res = []
for i in range(1, int(n ** 0.5) + 1):
if n % i == 0:
res.append(i)
res.append(n // i)
return list(set(res))
## taking integer array input
def int_array():
return list(map(int, input().strip().split()));
def float_array():
return list(map(float, input().strip().split()));
## taking string array input
def str_array():
return input().strip().split();
#defining a couple constants
MOD = int(1e9)+7;
CMOD = 998244353;
INF = float('inf'); NINF = -float('inf');
################### ---------------- TEMPLATE ENDS HERE ---------------- ###################
from itertools import permutations
import math
from bisect import bisect_left
def solve():
n, m, q = map(int, input().split())
string = input().rstrip()
substring = input().rstrip()
sequence = [0]
freq = 0
for i in range(n):
if string[i:i + m] == substring:
freq += 1
sequence.append(freq)
# print(sequence)
for i in range(q):
l, r = map(int, input().split())
print(sequence[max(l - 1, r - m + 1)] - sequence[l - 1])
if __name__ == '__main__':
for _ in range(1):
solve()
# fin_time = datetime.now()
# print("Execution time (for loop): ", (fin_time-init_time))
| PYTHON3 |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 |
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
public class B {
public static void main(String[] srgs) {
Scanner nik = new Scanner(System.in);
int n = nik.nextInt();
int m = nik.nextInt();
int q = nik.nextInt();
String s1 = nik.next();
String s2 = nik.next();
StringBuilder st = new StringBuilder();
int[] a = new int[s1.length() + 1];
for (int i = 0; i + s2.length() <= s1.length(); i++) {
String temp = s1.substring(i, i + s2.length());
if (temp.equals(s2)) {
a[i]++;
//a[i + s2.length()]--;
}
}
// for (int i = 1; i < a.length; i++) {
// a[i] += a[i - 1];
// }
// for (int val : a) {
// System.out.print(val + " ");
// }
// System.out.println();
while (q-- > 0) {
int sum = 0;
int l = nik.nextInt() - 1;
int r = nik.nextInt() ;
for (int i = l; i + s2.length() <= r; i++) {
sum += a[i];
}
st.append(sum + "\n");
}
System.out.println(st);
}
} | 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 | /* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s[]=br.readLine().split(" ");
int n=Integer.parseInt(s[0]);
int m=Integer.parseInt(s[1]);
int q=Integer.parseInt(s[2]);
String a=br.readLine();
String b=br.readLine();
boolean check[]=new boolean[n];
int []pa = new int[n];
for(int i=0;i<n-m+1;i++){
if(b.equals(a.substring(i,i+m)))
{check[i]=true;
}
}
StringBuilder ans=new StringBuilder();
while(q-->0){
s=br.readLine().split(" ");
int l=Integer.parseInt(s[0])-1;
int r=Integer.parseInt(s[1])-1;
int cnt=0;
for(int i=l;i<=r-m+1;i++){
if(check[i]) cnt++;
}
ans.append(cnt+"\n");
}
System.out.print(ans);
}
} | 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,t=input(),input()
a=[0,0]
b=0
for i in range(n):
b+=s[i:i+m]==t
a+=[b]
for _ in[0]*q:
l,r=map(int,input().split())
print(a[max(l,r-m+2)]-a[l])
| PYTHON3 |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n, m, q;
cin >> n >> m >> q;
string s;
cin >> s;
string t;
cin >> t;
string str1 = s;
size_t pos1;
vector<long long> arr;
pos1 = str1.find(t);
while (pos1 != string::npos) {
arr.push_back(pos1);
pos1 = str1.find(t, pos1 + 1);
}
while (q--) {
long long l, r;
cin >> l >> r;
long long count = 0;
for (long long i = 0; i < arr.size(); i++) {
if (arr[i] >= (l - 1) && (r - 1) >= (arr[i] + m - 1)) {
count++;
}
}
cout << count << endl;
}
return 0;
}
| CPP |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | n,m,q = map(int,raw_input().split())
s = raw_input()
p = raw_input()
re = [0]*n
for i in xrange(0,n,1):
if i <n-m+1:
if s[i:i+m]==p:
re[i]=re[i-1]+1
else:
re[i]=re[i-1]
else:
re[i]=re[i-1]
re = [0]+re
#print re
for _ in xrange(q):
a,b=map(int,raw_input().split())
print re[max(a-1,b-m+1)]-re[a-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 | '''input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
'''
n, m, q = [int(i) for i in input().split(" ")]
s = [i for i in input()]
t = [i for i in input()]
k = [0] * (n + 1)
for i in range(1, n + 1):
k[i] = k[i - 1]
if s[i - 1 : i + m - 1] == t:
k[i] += 1
for i in range(q):
l, r = [int(i) for i in input().split(" ")]
print(k[max(l - 1, r - m + 1)] - k[l - 1]) | PYTHON3 |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | def f(l, r):
b = r - m + 1
if b < l:
ans = 0
else:
ans = a[l]
if b != n-1:
ans -= a[b + 1]
return ans
n, m, q = map(int, raw_input().split())
s = raw_input()
t = raw_input()
e = [0] * n
for i in range(n - m + 1):
if s[i:i + m] == t:
e[i] = 1
a = [0] * n
a[n-1] = e[n-1]
for i in range(n - 2, -1, -1):
a[i] = a[i + 1] + e[i]
for i in range(q):
l, r = map(int, raw_input().split())
l -= 1
r -= 1
print f(l ,r)
| PYTHON |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | from bisect import insort,bisect_right,bisect_left
from sys import stdout, stdin, setrecursionlimit
from heapq import heappush, heappop, heapify
from io import BytesIO, IOBase
from collections import *
from itertools import *
from random import *
from string import *
from queue import *
from math import *
from re import *
from os import *
# sqrt,ceil,floor,factorial,gcd,log2,log10,comb
####################################---fast-input-output----#########################################
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = read(self._fd, max(fstat(self._fd).st_size, 8192))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = read(self._fd, max(fstat(self._fd).st_size, 8192))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
stdin, stdout = IOWrapper(stdin), IOWrapper(stdout)
graph, mod, szzz = {}, 10**9 + 7, lambda: sorted(zzz())
def getStr(): return input()
def getInt(): return int(input())
def listStr(): return list(input())
def getStrs(): return input().split()
def isInt(s): return '0' <= s[0] <= '9'
def input(): return stdin.readline().strip()
def zzz(): return [int(i) for i in input().split()]
def output(answer, end='\n'): stdout.write(str(answer) + end)
def lcd(xnum1, xnum2): return (xnum1 * xnum2 // gcd(xnum1, xnum2))
def getPrimes(N = 10**5):
SN = int(sqrt(N))
sieve = [i for i in range(N+1)]
sieve[1] = 0
for i in sieve:
if i > SN:
break
if i == 0:
continue
for j in range(2*i, N+1, i):
sieve[j] = 0
prime = [i for i in range(N+1) if sieve[i] != 0]
return prime
def primeFactor(n,prime=getPrimes()):
lst = []
mx=int(sqrt(n))+1
for i in prime:
if i>mx:break
while n%i==0:
lst.append(i)
n//=i
if n>1:
lst.append(n)
return lst
dx = [-1, 1, 0, 0, 1, -1, 1, -1]
dy = [0, 0, 1, -1, 1, -1, -1, 1]
daysInMounth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
#################################################---Some Rule For Me To Follow---#################################
"""
--instants of Reading problem continuously try to understand them.
--If you Know some-one , Then you probably don't know him !
--Try & again try, maybe you're just one statement away!
"""
##################################################---START-CODING---###############################################
n,m,q=zzz()
arr = getStr()
target = getStr()
ans=[0]*(max(n,m)+2)
for i in range(n):
if arr[i:i+m]==target:
ans[i+1]+=1
ans[i+1]+=ans[i]
# print(ans)
for i in range(q):
l,r=zzz()
print(max(0,ans[max(0,r-m+1)]-ans[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.*;
import java.io.*;
public class Contest{
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 str1 = br.readLine();
String str2 = br.readLine();
int[] psa = new int[n+1];
int cnt = 0;
if (m>n||(n==m&&!str1.equals(str2))) {
for (int i=0; i<q; i++) {
System.out.println(0);
}
return;
}else {
for (int j=0; j<n; j++) {
String sub = str1.substring(j, j+m>n ? n : j+m);
if (sub.equals(str2)) {
cnt++;
}
psa[j+1] = cnt;
}
}
for (int i=0; i<q; i++) {
st = new StringTokenizer(br.readLine());
int l = Integer.parseInt(st.nextToken())-1;
int r = Integer.parseInt(st.nextToken());
if (r-m+1<=0) System.out.println(0);
else {
int ans = psa[r-m+1]-psa[l];
if (ans<0) ans = 0;
System.out.println(ans);
}
}
}
} | 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 pref_func(s):
n = len(s)
f = list()
f.append(0)
for i in range(1,n):
j = f[i-1]
while j>0 and s[i] != s[j]:
j = f[j-1]
if s[i] == s[j]:
j += 1
f.append(j)
return f
n,m,q = list(map(int,input().split(' ')))
s = input()
t = input()
f = pref_func(t+'$'+s)[m+1:]
y = 0
suf = list()
for i in range(n-1,-1,-1):
suf.append(y)
if f[i] == m:
y += 1
suf.reverse()
for i in range(n):
if f[i] == m:
f[i] = 0
f[i-m+1] = 1
else:
f[i] = 0
pre = list()
y = 0
for i in range(n):
pre.append(y)
if f[i] == 1:
y += 1
mmm = f.count(1)
for i in range(q):
l,r = list(map(int,input().split(' ')))
print(max(mmm - pre[l-1] - suf[r-1],0))
| PYTHON3 |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int64_t MOD = 1e9 + 7;
const int64_t N = 2e6;
bool isQuery = false;
int64_t power(int64_t x, int64_t n) {
if (n == 0)
return 1;
else if (n % 2 == 0)
return power(x, n / 2) * power(x, n / 2);
else
return x * power(x, n / 2) * power(x, n / 2);
}
int64_t powerMod(int64_t x, int64_t n, int64_t mod) {
if (n == 0)
return 1;
else if (n % 2 == 0)
return (powerMod(x, n / 2, mod) * powerMod(x, n / 2, mod)) % mod;
else
return (x * powerMod(x, n / 2, mod) * powerMod(x, n / 2, mod)) % mod;
}
void solve() {
int64_t n, m, q;
cin >> n >> m >> q;
string s, t;
cin >> s >> t;
vector<int64_t> dp(n, 0), pref(n, -1), suff(n, -1), id;
int64_t ans = 0, p = -1;
for (int64_t i = 0, j; i < n; i++) {
for (j = 0; j < m && i + j < n; j++) {
if (s[i + j] != t[j]) break;
}
if (j == m)
pref[i + m - 1] = i + m - 1, id.push_back(i + m - 1), dp[i + m - 1] = 1;
}
for (int64_t i = 1; i < n; i++)
if (pref[i] == -1) pref[i] = pref[i - 1];
for (int64_t i = n - 1, j; i >= 0; i--) {
for (j = m - 1; j >= 0 && i - j >= 0; j--) {
if (s[i - (m - 1 - j)] != t[j]) break;
}
if (j == -1) suff[i - m + 1] = i;
}
for (int64_t i = n - 2; i >= 0; i--)
if (suff[i] == -1) suff[i] = suff[i + 1];
for (int64_t i = 1; i < n; i++) dp[i] += dp[i - 1];
while (q--) {
int64_t l, r;
cin >> l >> r;
l--, r--;
if (pref[r] == -1 || suff[l] == -1 | pref[r] < suff[l])
cout << 0;
else if (suff[l] > 0)
cout << dp[pref[r]] - dp[suff[l] - 1];
else
cout << dp[pref[r]];
cout << endl;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int64_t t = 1;
if (isQuery) cin >> t;
while (t--) {
solve();
cout << endl;
}
}
| CPP |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import itertools as it
n, m, q = map(int, input().split())
s, t = input(), input()
p = [int(s[i: i + len(t)] == t) for i in range(0, n - len(t) + 1)] + [0] * (len(t) - 1)
p = [0] + list(it.accumulate(p))
for _ in range(q):
left, right = map(int, input().split())
if right - left + 1 < len(t):
print(0)
else:
print(p[right - len(t) + 1] - p[left - 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 = [int(i) for i in input().split()]
s = list(input())
t = list(input())
lt = len(t)
ls = len(s)
start = []
for i in range(ls - lt +1):
j = i
count = 0
while s[j] == t[count]:
j += 1
count += 1
if count >= lt:
break
if count == lt:
start.append(i + 1)
start += [n + 1]
for i in range(q):
l, r = [int(j) for j in input().split()]
r -= lt - 1
if l > r:
print(0)
continue
f = False
for j in range(len(start)):
if (f == False) and (start[j] >= l):
f = True
begin = j
if (f) and (start[j] > r):
end = j
break
print(end - begin)
| 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.math.BigInteger;
import java.util.*;
public class Main implements Runnable {
int maxn = (int)1e5+111;
int n,m,k;
long a[] = new long[maxn];
void solve() throws Exception {
int n = in.nextInt();
int m = in.nextInt();
int q = in.nextInt();
String s = in.nextToken();
String t = in.nextToken();
String forkmp = t+"#"+s;
int kmp[] = buildPrefix(forkmp.toCharArray());
int prefSum[] = new int[maxn];
for (int i=m; i<kmp.length; i++) {
if (kmp[i]==m) {
prefSum[i] = prefSum[i-1] + 1;
} else {
prefSum[i] = prefSum[i-1];
}
}
int val = m+1;
for (int i=1; i<=q; i++) {
int left = in.nextInt() - 1 + val + (m-2);
int right = in.nextInt() - 1 + val;
if (right>=left) {
out.println(prefSum[right]-prefSum[left]);
} else {
out.println(0);
}
}
}
public int[] buildPrefix(char c[]) {
int arr[] = new int[c.length];
for (int i=1; i<c.length; i++) {
int j = arr[i-1];
while (j>0 && c[i]!=c[j]) {
j = arr[j-1];
}
if (c[i]==c[j]) j++;
arr[i] = j;
}
return arr;
}
class Pair {
int x, y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
String fileInName = "";
boolean file = false;
boolean isAcmp = false;
static Throwable throwable;
public static void main (String [] args) throws Throwable {
Thread thread = new Thread(null, new Main(), "", (1 << 26));
thread.start();
thread.join();
thread.run();
if (throwable != null)
throw throwable;
}
FastReader in;
PrintWriter out;
public void run() {
String fileIn = "absum.in";
String fileOut = "absum.out";
try {
if (isAcmp) {
if (file) {
in = new FastReader(new BufferedReader(new FileReader(fileIn)));
out = new PrintWriter (fileOut);
} else {
in = new FastReader(new BufferedReader(new InputStreamReader(System.in)));
out = new PrintWriter(System.out);
}
} else if (file) {
in = new FastReader(new BufferedReader(new FileReader(fileInName+".in")));
out = new PrintWriter (fileInName + ".out");
} else {
in = new FastReader(new BufferedReader(new InputStreamReader(System.in)));
out = new PrintWriter(System.out);
}
solve();
} catch(Exception e) {
throwable = e;
} finally {
out.close();
}
}
}
class FastReader {
BufferedReader bf;
StringTokenizer tk = null;
public FastReader(BufferedReader bf) {
this.bf = bf;
}
public String nextToken () throws Exception {
if (tk==null || !tk.hasMoreTokens()) {
tk = new StringTokenizer(bf.readLine());
}
if (!tk.hasMoreTokens()) return nextToken();
else
return tk.nextToken();
}
public int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}
public long nextLong() throws Exception {
return Long.parseLong(nextToken());
}
public double nextDouble() throws Exception {
return Double.parseDouble(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 | #include <bits/stdc++.h>
using namespace std;
int st[1001];
int main() {
int n, m, q;
cin >> n >> m >> q;
string s;
cin >> s;
string t;
cin >> t;
st[0] = 0;
for (int i = 1; i <= n - m + 1; i++) {
bool b = true;
for (int j = i; j < i + m; j++) {
if (s.at(j - 1) != t.at(j - i)) {
b = false;
break;
}
}
if (b) {
st[i] = st[i - 1] + 1;
} else {
st[i] = st[i - 1];
}
}
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
if (r - l < m - 1) {
cout << 0 << endl;
} else {
cout << st[r - m + 1] - st[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.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author lewin
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
BSegmentOccurrences solver = new BSegmentOccurrences();
solver.solve(1, in, out);
out.close();
}
static class BSegmentOccurrences {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt(), m = in.nextInt(), q = in.nextInt();
String s = in.next(), t = in.next();
List<Integer> occ = ZFunction.findAll(s, t);
int[] count = new int[n + 1];
for (int x : occ) count[x + 1]++;
for (int i = 1; i <= n; i++) count[i] += count[i - 1];
while (q-- > 0) {
int l = in.nextInt(), r = Math.max(0, in.nextInt() - m + 1);
out.println(Math.max(0, count[r] - count[l - 1]));
}
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
writer.close();
}
public void println(int i) {
writer.println(i);
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1 << 16];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (this.numChars == -1) {
throw new InputMismatchException();
} else {
if (this.curChar >= this.numChars) {
this.curChar = 0;
try {
this.numChars = this.stream.read(this.buf);
} catch (IOException var2) {
throw new InputMismatchException();
}
if (this.numChars <= 0) {
return -1;
}
}
return this.buf[this.curChar++];
}
}
public int nextInt() {
int c;
for (c = this.read(); isSpaceChar(c); c = this.read()) {
;
}
byte sgn = 1;
if (c == 45) {
sgn = -1;
c = this.read();
}
int res = 0;
while (c >= 48 && c <= 57) {
res *= 10;
res += c - 48;
c = this.read();
if (isSpaceChar(c)) {
return res * sgn;
}
}
throw new InputMismatchException();
}
public String next() {
int c;
while (isSpaceChar(c = this.read())) {
;
}
StringBuilder result = new StringBuilder();
result.appendCodePoint(c);
while (!isSpaceChar(c = this.read())) {
result.appendCodePoint(c);
}
return result.toString();
}
public static boolean isSpaceChar(int c) {
return c == 32 || c == 10 || c == 13 || c == 9 || c == -1;
}
}
static class ZFunction {
public static int[] zFunction(String s) {
int[] z = new int[s.length()];
for (int i = 1, l = 0, r = 0; i < z.length; ++i) {
if (i <= r)
z[i] = Math.min(r - i + 1, z[i - l]);
while (i + z[i] < z.length && s.charAt(z[i]) == s.charAt(i + z[i]))
++z[i];
if (r < i + z[i] - 1) {
l = i;
r = i + z[i] - 1;
}
}
return z;
}
public static List<Integer> findAll(String s, String pattern) {
int[] z = zFunction(pattern + "\0" + s);
List<Integer> res = new ArrayList<>();
for (int i = pattern.length() + 1; i < z.length; i++)
if (z[i] == pattern.length())
res.add(i - pattern.length() - 1);
return res;
}
}
}
| JAVA |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | n, m, q = map(int, input().split())
x = [0 for i in range(n+1)]
b = input()
s = input()
for i in range(n-m+1):
f = True
for j in range(i, i+m):
if b[j] != s[j-i]:
f = False
break
if f:
x[i+m] = 1
for i in range(1, n+1):
x[i] = x[i] + x[i-1]
x += [x[-1] for i in range(m)]
for _ in range(q):
l, r = map(int, input().split())
print(max(x[r]-x[l+m-2], 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
out = sys.stdout
n, m, q = map(int,input().split())
s = input()
t = input()
res = []
for i in range(n-m+1):
if s[i:i+m] == t:
e = (i, i+m-1)
res.append(e)
for i in range(q):
li, ri = map(int, input().split())
k = 0
for t, p in res:
if t >= (li - 1) and p <= (ri - 1):
k += 1
out.write(str(k)+"\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 java.util.*;
import java.lang.*;
import java.io.*;
/*
*
* Comments Here
*
*/
public class E49_B
{
static BufferedReader br;
static BufferedWriter bw;
static StringTokenizer st;
public static void main(String[] args) throws java.lang.Exception
{
br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(System.out));
/*/
File file = new File("src/in.txt");
try {
br = new BufferedReader(FileReader(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
/**/
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int q = Integer.parseInt(st.nextToken());
char[] s = br.readLine().toCharArray();
char[] t = br.readLine().toCharArray();
boolean[] isSub = new boolean[n];
outer: for(int i = 0; i < (n-m+1); ++i) {
for(int j = 0; j < m; ++j) {
if(s[i+j] != t[j])
continue outer;
}
isSub[i] = true;
}
// number of substrings in range [0,i]
int[] prefixSum = new int[n+1];
for(int i = 1; i <=n; ++i) {
prefixSum[i] = prefixSum[i-1];
if(isSub[i-1])
++prefixSum[i];
}
// System.out.println(Arrays.toString(isSub));
// System.out.println(Arrays.toString(prefixSum));
for(int i = 0; i < q; ++i) {
st = new StringTokenizer(br.readLine());
int l = Integer.parseInt(st.nextToken())-1;
int r = Integer.parseInt(st.nextToken())-m+1;
l = Math.max(l, 0);
if (r <= l)
r = l;
//bw.write(r + " " + l + "\n");
int ans = prefixSum[r]-prefixSum[l];
bw.write(ans + "\n");
}
br.close();
bw.close();
}
}
| JAVA |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
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) ara[pre + 1]++;
i = pre;
}
for (long long i = 1; i <= n + 10; i++) {
ara[i] += ara[i - 1];
}
while (q--) {
long long a, b;
cin >> a >> b;
b = b - m + 1;
cout << max(0ll, ara[max(b, 0ll)] - ara[a - 1]) << endl;
}
}
| 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 Solution {
static class BinaryIndexTree {
private int[] array;
private int total;
public BinaryIndexTree(int[] ar) {
this.total = ar.length;
int i = 1;
while (i < this.total) {
i = i * 2;
}
array = new int[i];
}
public BinaryIndexTree(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;
}
}
static class KMP_String {
List<Integer> list = new ArrayList<Integer>();
void KMPSearch(String pat, String txt) {
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) {
list.add((i - j));
j = lps[j - 1];
}
else if (i < N && pat.charAt(j) != txt.charAt(i)) {
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
}
void computeLPSArray(String pat, int M, int lps[]) {
int len = 0;
int i = 1;
lps[0] = 0;
while (i < M) {
if (pat.charAt(i) == pat.charAt(len)) {
len++;
lps[i] = len;
i++;
} else
{
if (len != 0) {
len = lps[len - 1];
} else
{
lps[i] = len;
i++;
}
}
}
}
}
public static void main(String[] args) throws IOException {
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();
BinaryIndexTree tree1 = new BinaryIndexTree(n + 2);
KMP_String kmp_String_Matching = new KMP_String();
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 | #include <bits/stdc++.h>
using namespace std;
int n, m, q, lct1, lct2;
string s, t;
int z[3005];
int cnt;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m >> q;
cin >> s >> t;
s = t + s;
int len = s.length();
z[0] = 0;
for (int i = 1, l = 0, r = 0; i < len; ++i) {
if (i <= r) z[i] = min(z[i - l], r - i + 1);
while (i + z[i] < len && s[z[i]] == s[i + z[i]]) z[i]++;
if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1;
}
for (int i = 1; i <= q; ++i) {
cnt = 0;
cin >> lct1 >> lct2;
for (int i = lct1 + m - 1; i <= lct2 + m - 1; ++i)
if (z[i] >= m && i + m - 1 <= lct2 + m - 1) cnt++;
cout << cnt << "\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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class SegmentOccurrences {
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());
char[] s = br.readLine().toCharArray();
char[] t = br.readLine().toCharArray();
int[] k = new int[n];
//store start indices of t substring in s
for(int i=0; i<=n-m; i++){
boolean found=true;
for(int j=0; j<m; j++){
if(s[i+j] != t[j]){
found = false;
break;
}
}
if(found){
k[i] = 1;
}
}
//store the count of all previous substrings in k[i]
//avoids looping over l to r repeatedly
for(int i=1; i<n; i++){
k[i] += k[i-1];
}
while (q-- != 0){
st = new StringTokenizer(br.readLine());
int l = Integer.parseInt(st.nextToken()) -1;
int r = Integer.parseInt(st.nextToken()) -1;
if(r-l+1 < m){
System.out.println(0);
continue;
}
int count = l-1 >= 0? k[r-m+1] - k[l-1] : k[r-m+1];
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;
struct Node {
int d;
struct Node *left, *right;
};
int p[1000][1000] = {0};
string s, t;
void dp(int i, int j) {
if (j - i + 1 == t.size()) {
int f = 1;
for (int z = 0; z < t.size(); z++) {
if (s[i + z] != t[z]) {
f = 0;
break;
}
}
if (f == 1) p[i][j] = 1;
} else if (j - i + 1 < t.size())
return;
else if (j - i + 1 > t.size()) {
int maxm = 0, l = t.size();
for (int x = i; x <= j - l + 1; x++) {
if (p[x][x + l - 1] > 0) p[i][j]++;
}
}
return;
}
int main() {
int n, m, q;
cin >> n >> m >> q;
int a[q][2];
getchar();
cin >> s;
getchar();
cin >> t;
for (int i = 0; i <= q - 1; i++) {
cin >> a[i][0] >> a[i][1];
}
int l = t.size();
for (int si = l; si <= n; si++) {
for (int j = 0; j <= n - si; j++) dp(j, j + si - 1);
}
for (int i = 0; i <= q - 1; i++) cout << p[a[i][0] - 1][a[i][1] - 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;
string s, t;
vector<int> can;
int n, m, k;
int main() {
cin >> n >> m >> k;
cin >> s >> t;
for (int i = 0; i <= n - m; i++) {
if (s.substr(i, m) != t) continue;
can.push_back(i + 1);
}
while (k--) {
int l, r;
cin >> l >> r;
r = r - m + 1;
int first = lower_bound(can.begin(), can.end(), l) - can.begin();
int last = upper_bound(can.begin(), can.end(), r) - can.begin();
cout << max(0, last - first) << endl;
}
return 0;
}
| CPP |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int n, m, q, l, r, vf, ans, i, j, k;
const int LMAX = 1e3;
char s[LMAX + 5], t[LMAX + 5];
int st[LMAX + 5], dr[LMAX + 5], cnt = 0;
int main() {
cin >> n >> m >> q;
for (i = 1; i <= n; i++) cin >> s[i];
for (i = 1; i <= m; i++) cin >> t[i];
for (i = 1; i <= n - m + 1; i++)
if (s[i] == t[1]) {
bool ok = true;
int vf = 0;
for (j = i; j <= i + m - 1; j++)
if (s[j] != t[++vf]) {
ok = false;
break;
}
if (ok == true) {
st[++cnt] = i;
dr[cnt] = i + m - 1;
}
}
for (i = 1; i <= q; i++) {
cin >> l >> r;
ans = 0;
for (j = 1; j <= cnt; j++)
if (st[j] >= l && dr[j] <= r) ans++;
cout << ans << '\n';
}
return 0;
}
| CPP |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | # cook your code here
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 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string s, t;
int n, m, q, ct = 0, flag, l, r;
cin >> n >> m >> q;
vector<vector<int>> ans(n + 1, vector<int>(n + 1, 0));
cin >> s;
cin >> t;
for (int i = 0; i < n; i++) {
flag = 1;
for (int j = 0; j < m; j++) {
if (s[i + j] != t[j]) {
flag = 0;
break;
}
}
if (flag) {
ct++;
for (int j = 0; j <= i; j++) ans[j + 1][i + m]++;
}
}
for (int i = 1; i <= n; i++) {
for (int j = i; j < n; j++) ans[i][j + 1] += ans[i][j];
}
for (int i = 0; i < q; i++) {
cin >> l >> r;
cout << ans[l][r] << endl;
}
return 0;
}
| CPP |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | n, m, q = map(int, input().split())
s = input()
t = input()
a = [0] * (n + 1)
for i in range(n - m + 1):
if s[i:i + m] == t:
a[i + 1] += 1
S = [0] * (n + 1)
for i in range(1, n + 1):
S[i] += S[i - 1] + a[i]
for i in range(q):
l, r = map(int, input().split())
if r + 1 - l >= m:
print(S[r - m + 1] - S[l - 1])
else:
print(0)
| PYTHON3 |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
string str;
vector<int> z;
int len = 0;
void zfunc() {
len = str.size();
z.resize(len);
int ind = 0;
for (int i = 1; i < len; i++) {
if (ind + z[ind] - 1 >= i) {
z[i] = min(ind + z[ind] - i, z[i - ind]);
}
for (; i + z[i] < len && str[i + z[i]] == str[z[i]]; z[i]++)
;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m, q;
cin >> n >> m >> q;
string s, t;
cin >> s >> t;
str = t + '#' + s;
zfunc();
vector<int> pref(s.size() + 1);
for (int i = 1; i <= s.size(); i++) {
pref[i] = pref[i - 1] + int(z[i + t.size()] == t.size());
}
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
if (r - l + 1 < t.size()) {
cout << 0 << '\n';
} else {
cout << pref[r - t.size() + 1] - pref[l - 1] << '\n';
}
}
return 0;
}
| CPP |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
public class Main{
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;
}
}
public static void main(String[] args) throws IOException {
PrintWriter out = new PrintWriter(System.out);
MyScanner sc = new MyScanner();
Main solver = new Main();
solver.solve(out, sc);
out.flush();
out.close();
}
void solve(PrintWriter out, MyScanner sc) throws IOException{
int n = sc.nextInt();
int m = sc.nextInt();
int q = sc.nextInt();
String s = sc.next();
String t = sc.next();
int[] ss = new int[n];
outer:
for(int i = 0 ; i < n ; ++i) {
if(s.charAt(i) == t.charAt(0));
int k = 0;
for( int j = i ; j < n && k < m ; ++k, ++j) {
if(s.charAt(j) != t.charAt(k)) continue outer;
}
if(k == m) ss[i] = 1;
}
// out.println(Arrays.toString(ss));
for(int i = 1 ; i < n ; ++i) ss[i] += ss[i - 1];
// out.println(Arrays.toString(ss));
while(q-- > 0) {
int l = sc.nextInt() - 1;
int r = sc.nextInt() - 1;
if(r - l + 1 < m) out.println(0);
else {
if(l == 0) out.println(ss[r - m + 1]);
else out.println(ss[r - m + 1] - ss[l - 1]);
}
}
}
} | JAVA |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
MyReader reader = new MyReader(System.in);
// MyReader reader = new MyReader(new FileInputStream("input.txt"));
MyWriter writer = new MyWriter(System.out);
new Main().run(reader, writer);
writer.close();
}
private void run(MyReader reader, MyWriter writer) throws Exception {
int n = reader.nextInt();
int m = reader.nextInt();
int q = reader.nextInt();
char[] s = reader.nextCharArray();
char[] t = reader.nextCharArray();
int[] a = new int[n];
for (int i = 0; i <= n - m; i++) {
if (i > 0) {
a[i] += a[i - 1];
}
boolean b = true;
for (int j = 0; j < m; j++) {
b &= s[i + j] == t[j];
}
if (b) {
a[i]++;
}
}
for (int i = 0; i < q; i++) {
int x = reader.nextInt() - 1;
int y = reader.nextInt() - 1;
if (y - x + 1 >= m) {
writer.println(a[y - m + 1] - (x > 0 ? a[x - 1] : 0) + "");
} else {
writer.println("0");
}
}
}
static class MyReader {
final BufferedInputStream in;
final int bufSize = 1 << 16;
final byte buf[] = new byte[bufSize];
int i = bufSize;
int k = bufSize;
boolean end = false;
final StringBuilder str = new StringBuilder();
MyReader(InputStream in) {
this.in = new BufferedInputStream(in, bufSize);
}
int nextInt() throws IOException {
return (int) nextLong();
}
int[] nextIntArray(int n) throws IOException {
int[] m = new int[n];
for (int i = 0; i < n; i++) {
m[i] = nextInt();
}
return m;
}
int[][] nextIntMatrix(int n, int m) throws IOException {
int[][] a = new int[n][0];
for (int j = 0; j < n; j++) {
a[j] = nextIntArray(m);
}
return a;
}
long nextLong() throws IOException {
int c;
long x = 0;
boolean sign = true;
while ((c = nextChar()) <= 32) ;
if (c == '-') {
sign = false;
c = nextChar();
}
if (c == '+') {
c = nextChar();
}
while (c >= '0') {
x = x * 10 + (c - '0');
c = nextChar();
}
return sign ? x : -x;
}
long[] nextLongArray(int n) throws IOException {
long[] m = new long[n];
for (int i = 0; i < n; i++) {
m[i] = nextLong();
}
return m;
}
int nextChar() throws IOException {
if (i == k) {
k = in.read(buf, 0, bufSize);
i = 0;
}
return i >= k ? -1 : buf[i++];
}
String nextString() throws IOException {
if (end) {
return null;
}
str.setLength(0);
int c;
while ((c = nextChar()) <= 32 && c != -1) ;
if (c == -1) {
end = true;
return null;
}
while (c > 32) {
str.append((char) c);
c = nextChar();
}
return str.toString();
}
String nextLine() throws IOException {
if (end) {
return null;
}
str.setLength(0);
int c = nextChar();
while (c != '\n' && c != '\r' && c != -1) {
str.append((char) c);
c = nextChar();
}
if (c == -1) {
end = true;
if (str.length() == 0) {
return null;
}
}
if (c == '\r') {
nextChar();
}
return str.toString();
}
char[] nextCharArray() throws IOException {
return nextString().toCharArray();
}
char[][] nextCharMatrix(int n) throws IOException {
char[][] a = new char[n][0];
for (int i = 0; i < n; i++) {
a[i] = nextCharArray();
}
return a;
}
}
static class MyWriter {
final BufferedOutputStream out;
final int bufSize = 1 << 16;
final byte buf[] = new byte[bufSize];
int i = 0;
final byte c[] = new byte[30];
static final String newLine = System.getProperty("line.separator");
MyWriter(OutputStream out) {
this.out = new BufferedOutputStream(out, bufSize);
}
void print(long x) throws IOException {
int j = 0;
if (i + 30 >= bufSize) {
flush();
}
if (x < 0) {
buf[i++] = (byte) ('-');
x = -x;
}
while (j == 0 || x != 0) {
c[j++] = (byte) (x % 10 + '0');
x /= 10;
}
while (j-- > 0)
buf[i++] = c[j];
}
void print(int[] m) throws IOException {
for (int a : m) {
print(a);
print(' ');
}
}
void print(long[] m) throws IOException {
for (long a : m) {
print(a);
print(' ');
}
}
void print(String s) throws IOException {
for (int i = 0; i < s.length(); i++) {
print(s.charAt(i));
}
}
void print(char x) throws IOException {
if (i == bufSize) {
flush();
}
buf[i++] = (byte) x;
}
void print(char[] m) throws IOException {
for (char c : m) {
print(c);
}
}
void println(String s) throws IOException {
print(s);
println();
}
void println() throws IOException {
print(newLine);
}
void flush() throws IOException {
out.write(buf, 0, i);
out.flush();
i = 0;
}
void close() throws IOException {
flush();
out.close();
}
}
} | JAVA |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import java.util.* ;
import java.io.BufferedReader ;
import java.io.InputStreamReader ;
public class SegmentOccurrences
{
private static final boolean debug = false ;
public static void main(String args[]) throws Exception
{
// String T = "aanonoaanonoaanono" ;
// String P = "aa" ;
// int[] b = kmpPreprocess(P.toCharArray()) ;
// List<Integer> L = kmpSearch(P.toCharArray(),T.toCharArray(),b) ;
// System.out.println(L.toString());
BufferedReader bro = new BufferedReader(new InputStreamReader(System.in)) ;
String[] S = bro.readLine().split(" ") ;
int n = Integer.parseInt(S[0]) ;
int m = Integer.parseInt(S[1]) ;
int q = Integer.parseInt(S[2]) ;
String T = bro.readLine() ;
String P = bro.readLine() ;
int[] b = kmpPreprocess(P.toCharArray()) ;
List<Integer> L = kmpSearch(P.toCharArray(),T.toCharArray(),b) ;
HashSet<Integer> H = new HashSet<Integer>(L) ;
// int[] ppc = new int[T.length()] ;
// int count = 0,idx = 0 ;
// for(int i=0;i<T.length();i++)
// {
// if(idx<L.size() && i==L.get(idx))
// {
// count++ ;
// idx++ ;
// }
// ppc[i] = count ;
// }
// if(debug)
// {
// System.out.println(Arrays.toString(ppc)) ;
// }
// for(int i=0;i<q;i++)
// {
// S = bro.readLine().split(" ") ;
// int a = Integer.parseInt(S[0]) ;
// int b2 = Integer.parseInt(S[1]) ;
// a-- ;
// b2-- ;
// System.out.println(ppc[b2]-ppc[a]) ;
// }
for(int i=0;i<q;i++)
{
S = bro.readLine().split(" ") ;
int x = Integer.parseInt(S[0]) ;
int y = Integer.parseInt(S[1]) ;
x--;
y-- ;
if(y-x+1<P.length())
System.out.println(0) ;
else
{
int idx = Collections.binarySearch(L,x) ;
int idy = Collections.binarySearch(L,y) ;
if(idx<0)
idx = -idx-1 ;
if(idy<0)
idy = -idy-1 ;
if(debug)
{
// System.out.println(idx+" "+idy) ;
}
int count = 0 ;
for(int j=idx;j<=idy;j++)
{
if(j<L.size()&&L.get(j)+P.length()-1<=y)
count++ ;
}
System.out.println(count);
}
}
}
static int[] kmpPreprocess(char[] P)
{
int n = P.length ;
int[] b = new int[n+1];
int i=0,j=-1 ;
b[0] = -1 ;
while(i<n)
{
while(j>=0 && P[i]!=P[j]) j = b[j] ;
i++ ;j++ ;
b[i] = j ;
}
return b ;
}
static List<Integer> kmpSearch(char[] P,char[] T,int[] b)
{
int m = P.length ;
int n = T.length ;
int i=0,j=0 ;
List<Integer> L = new ArrayList<Integer>() ;
while(i<n)
{
while(j>=0 && T[i]!=P[j]) j=b[j] ;
i++;j++;
if(j==m)
{
L.add(i-j) ;
j=b[j] ;
}
}
return L ;
}
} | JAVA |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class Problem1016B {
public static void main(String[] args) throws Exception {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
int n = in.nextInt();
int m = in.nextInt();
int q = in.nextInt();
String s = in.nextLine();
String t = in.nextLine();
int[][] qs = in.nextIntMatrix(q, 2);
int lastIndex = 0;
ArrayList<Integer> occs = new ArrayList<>();
while (lastIndex != -1) {
lastIndex = s.indexOf(t, lastIndex);
if (lastIndex != -1) {
occs.add(lastIndex + 1);
lastIndex++;
}
}
for (int i = 0; i < q; i++) {
final int que = i;
out.println(occs.stream().filter(oc -> qs[que][0] <= oc && qs[que][1] >= oc + m - 1).count());
}
out.close();
return;
}
static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
private String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine(), " \t\n\r\f,");
} 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 int[] nextIntArray(int n) {
return nextIntArray(n, 0);
}
public int[] nextIntArray(int n, int offset) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt() + offset;
}
return a;
}
public int[][] nextIntMatrix(int n, int m) {
int[][] a = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = nextInt();
}
}
return a;
}
public String nextLine() {
return next();
}
public char[] nextLineCharArray() {
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 | n, m, k = map(int, input().split())
s = input()
st = input()
ar = [0 for i in range(n)]
pr = [0 for i in range(n+1)]
for i in range(n-m+1):
found = 1
for j in range(m):
if s[i+j] != st[j]:
found = 0
break
ar[i] = found
pr[i+1] = pr[i]+ ar[i]
for i in range(max(0, n-m+1), n):
pr[i+1] = pr[i]
for i in range(k):
l, r = map(int, input().split())
if r - l +1 < m:
print(0)
else:
print(pr[r-m+1]-pr[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 os
import sys
from atexit import register
from io import BytesIO
sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
sys.stdout = BytesIO()
register(lambda: os.write(1, sys.stdout.getvalue()))
input = lambda: sys.stdin.readline().rstrip('\r\n')
raw_input = lambda: sys.stdin.readline().rstrip('\r\n')
n,m,q = map(int,raw_input().split(" "))
s = raw_input()
t = raw_input()
l1 = len(t)
ans = [0]*(n+1)
for i in range(n-m+1):
if s[i:i+l1] == t:
ans[i]+= 1
psum = [0]*(n+2)
for i in range(n+1):
psum[i+1] = psum[i]+ans[i]
for i in range(q):
l,r = map(int,raw_input().split(" "))
print max(0,psum[max(0,r-l1+1)]-psum[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, raw_input().split())
a = raw_input()
b = raw_input()
wynik = ''
for i in range(0, n - m + 1):
if a[i : i + m] == b:
wynik += '1'
else: wynik += '0'
for i in range(q):
x , y = map(int, raw_input().split())
if y - x + 1 >= m:
print(wynik[x - 1 : y - m + 1].count('1'))
else: print(0) | PYTHON |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import java.util.*;
public class A
{
public static void main(String ar[])
{
Scanner s=new Scanner(System.in);
int n=s.nextInt(); int m=s.nextInt(); int q=s.nextInt(); s.nextLine();
char c[]=s.nextLine().toCharArray();
char d[]=s.nextLine().toCharArray();
int a[]=new int[n];
for(int i=0;i<=n-m;i++)
{
int t=0;
for(int j=0;j<m;j++)
{
if(c[i+j]!=d[j])
t=1;
}
if(t==0)
a[i]=1;
if(i!=0)
a[i]+=a[i-1];
}
for(int i=0;i<q;i++)
{
int l=s.nextInt(); int r=s.nextInt(); r-=m-1; l--; r--;
if(r<l)
System.out.println("0");
else if(l==0)
System.out.println(a[r]);
else
System.out.println(a[r]-a[l-1]);
}
}
} | JAVA |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
char s[100005], t[100005];
int Q, m, n, l1, l2;
int sum[100005], fr[100005], ma[100005];
void init() {
int i, np;
scanf("%d%d%d", &l1, &l2, &Q);
scanf("%s", s);
scanf("%s", t);
for (i = l1; i > 0; i--) s[i] = s[i - 1];
for (i = l2; i > 0; i--) t[i] = t[i - 1];
fr[1] = 0;
for (i = 2; i <= l2; i++) {
np = fr[i - 1];
while (t[i] != t[np + 1] && np) np = fr[np];
if (t[i] == t[np + 1])
fr[i] = np + 1;
else
fr[i] = 0;
}
t[l2 + 1] = '&';
ma[0] = 0;
for (i = 1; i <= l1; i++) {
np = ma[i - 1];
while (np && s[i] != t[np + 1]) np = fr[np];
if (s[i] == t[np + 1])
ma[i] = np + 1;
else
ma[i] = 0;
}
sum[0] = 0;
for (i = 1; i <= l1; i++) sum[i] = sum[i - 1] + (ma[i] == l2);
}
int main() {
int i, l, r;
init();
for (i = 1; i <= Q; i++) {
scanf("%d%d", &l, &r);
if (r - l + 1 < l2) {
printf("0\n");
continue;
}
printf("%d\n", max(0, sum[r] - sum[l + l2 - 2]));
}
return 0;
}
| CPP |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | def main():
(n,m,q) = [int(x) for x in input().split()]
s = input()
t = input()
st1 = [0 for i in range(n+1)]
st2 = [0 for i in range(n+1)]
for i in range(m,n+1) :
if t == s[i-m:i] :
st1[i-m+1] = 1
st2[i-1]=1
for i in range(1,n+1) :
st1[i] = st1[i-1]+st1[i]
st2[i] = st2[i-1]+st2[i]
for i in range(q) :
(l,r) = [int(x) for x in input().split()]
if r-l<m-1:
print(0)
else :
print((st1[n]-st1[l-1])-(st2[n]-st2[r-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 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.io.Writer;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author palayutm
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt(), m = in.nextInt(), q = in.nextInt();
String s = in.next();
String t = in.next();
int[] a = new int[n + 1];
for (int i = 0; i + m <= n; i++) {
if (s.substring(i, i + m).equals(t)) {
a[i + 1]++;
}
}
for (int i = 1; i <= n; i++) {
a[i] += a[i - 1];
}
while (q-- > 0) {
int l = in.nextInt(), r = in.nextInt();
r = r - m + 1;
out.println((r >= l ? a[r] - a[l - 1] : 0));
}
}
}
static class InputReader {
private InputStream stream;
private byte[] inbuf = new byte[1024];
private int lenbuf = 0;
private int ptrbuf = 0;
public InputReader(InputStream stream) {
this.stream = stream;
}
private int readByte() {
if (lenbuf == -1) throw new UnknownError();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = stream.read(inbuf);
} catch (IOException e) {
throw new UnknownError();
}
if (lenbuf <= 0) return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b)) ;
return b;
}
public String next() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
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();
}
}
}
static class OutputWriter extends PrintWriter {
public OutputWriter(OutputStream out) {
super(out);
}
public OutputWriter(Writer out) {
super(out);
}
public void close() {
super.close();
}
}
}
| JAVA |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
long long ans[1005];
int main() {
long long n, m, k;
string a, b;
cin >> n >> m >> k;
cin >> a >> b;
for (int i = 0; i <= n - m; i++) {
ans[i + 1] = ans[i] + (a.substr(i, m) == b);
}
while (k--) {
long long l, r;
cin >> l >> r;
if (r - l + 1 < m) {
cout << "0" << endl;
} else {
cout << ans[r - m + 1] - ans[l - 1] << endl;
}
}
}
| CPP |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | /* Author: Ronak Agarwal, reader part not written by me*/
import java.io.* ; import java.util.* ;
import static java.lang.Math.min ; import static java.lang.Math.max ;
import static java.lang.Math.abs ; import static java.lang.Math.log ;
import static java.lang.Math.pow ; import static java.lang.Math.sqrt ;
/* Thread is created here to increase the stack size of the java code so that recursive dfs can be performed */
public class Codeshefcode{
public static void main(String[] args) throws IOException{
new Thread(null,new Runnable(){ public void run(){ exec_Code() ;} },"Solver",1l<<27).start() ;
}
static void exec_Code(){
try{
Solver Machine = new Solver() ;
Machine.Solve() ; Machine.Finish() ;}
catch(Exception e){ e.printStackTrace() ; print_and_exit() ;}
catch(Error e){ e.printStackTrace() ; print_and_exit() ; }
}
static void print_and_exit(){ System.out.flush() ; System.exit(-1) ;}
}
/* Implementation of the Reader class */
class Reader {
private InputStream mIs;
private byte[] buf = new byte[1024];
private int curChar,numChars;
public Reader() { this(System.in); }
public Reader(InputStream is) { mIs = is;}
public int read() {
if (numChars == -1) throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try { numChars = mIs.read(buf);} catch (IOException e) { throw new InputMismatchException();}
if (numChars <= 0) return -1;
}
return buf[curChar++];
}
public String nextLine(){
int c = read();
while (isSpaceChar(c)) c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString() ;
}
public String s(){
int c = read();
while (isSpaceChar(c)) c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
}while (!isSpaceChar(c));
return res.toString();
}
public long l(){
int c = read();
while (isSpaceChar(c)) c = read();
int sgn = 1;
if (c == '-') { sgn = -1 ; c = read() ; }
long res = 0;
do{
if (c < '0' || c > '9') throw new InputMismatchException();
res *= 10 ; res += c - '0' ; c = read();
}while(!isSpaceChar(c));
return res * sgn;
}
public int i(){
int c = read() ;
while (isSpaceChar(c)) c = read();
int sgn = 1;
if (c == '-') { sgn = -1 ; c = read() ; }
int res = 0;
do{
if (c < '0' || c > '9') throw new InputMismatchException();
res *= 10 ; res += c - '0' ; c = read() ;
}while(!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; }
public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; }
}
/* All the useful functions,constants,renamings are here*/
class Template{
/* Constants Section */
final int INT_MIN = Integer.MIN_VALUE ; final int INT_MAX = Integer.MAX_VALUE ;
final long LONG_MIN = Long.MIN_VALUE ; final long LONG_MAX = Long.MAX_VALUE ;
static long MOD = 1000000007 ;
static Reader ip = new Reader() ;
static PrintWriter op = new PrintWriter(System.out) ;
/* Methods for writing */
static void p(Object o){ op.print(o) ; }
static void pln(Object o){ op.println(o) ;}
static void Finish(){ op.flush(); op.close(); }
/* Implementation of operations modulo MOD */
static long inv(long a){ return powM(a,MOD-2) ; }
static long m(long a,long b){ return (a*b)%MOD ; }
static long d(long a,long b){ return (a*inv(b))%MOD ; }
static long powM(long a,long b){
if(b<0) return powM(inv(a),-b) ;
long ans=1 ;
for( ; b!=0 ; a=(a*a)%MOD , b/=2) if((b&1)==1) ans = (ans*a)%MOD ;
return ans ;
}
/* Renaming of some generic utility classes */
final static class mylist extends ArrayList<Integer>{}
final static class myset extends TreeSet<pair>{}
final static class mystack extends Stack<Integer>{}
final static class mymap extends TreeMap<Integer,Integer>{}
}
/* Implementation of the pair class, useful for every other problem */
class pair implements Comparable<pair>{
int x ; int y ;
pair(int _x,int _y){ x=_x ; y=_y ;}
public int compareTo(pair p){
return (this.x<p.x ? -1 : (this.x>p.x ? 1 : (this.y<p.y ? -1 : (this.y>p.y ? 1 : 0)))) ;
}
}
/* Main code starts here */
class Solver extends Template{
public void Solve() throws IOException{
int n = ip.i() ; int m = ip.i(); int q = ip.i();
char s[] = ip.s().toCharArray();
char t[] = ip.s().toCharArray();
int matches[] = new int[n];
for(int i=0 ; (i+m-1)<n ; i++){
boolean match = true;
for(int j=0 ; j<m && (i+j)<n ; j++) if(s[i+j]!=t[j]) match=false;
matches[i] = match ? 1 : 0;
}
// for(int i=0 ; i<n ; i++) p(matches[i]+" ");
// pln("");
int pmat[] = new int[n];
pmat[0] = matches[0];
for(int i=1 ; i<n ; i++) pmat[i] = matches[i]+pmat[i-1];
while(q--!=0){
int l = ip.i()-1 ; int r = ip.i()-1;
r = max(l-1,r-m+1);
pln(((r==-1) ? 0 : pmat[r])-((l==0) ? 0 : pmat[l-1]));
}
}
}
| JAVA |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | n, m, q = map(int, input().split())
s = input()
t = input()
a = [0] * n
for i in range(n - m + 1):
if s[i:i + m] == t:
a[i] = 1
for i in range(q):
k = 0
l, r = map(int, input().split())
for j in range(l - 1, r):
if a[j] == 1 and j + m - 1 < r:
k += 1
print(k) | PYTHON3 |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 10;
int main() {
int n, q, g;
int index;
int a[1005];
int k = 0;
string s, t;
string ss[1005];
scanf("%d%d%d", &n, &q, &g);
getchar();
cin >> s >> t;
for (int i = 0; i < n; i++) {
string str = s.substr(i, q);
if (str == t) {
a[k++] = i + 1;
}
}
while (g--) {
int sum = 0;
int l, r;
scanf("%d%d", &l, &r);
for (int i = 0; i < k; i++) {
if (a[i] >= l && a[i] + q - 1 <= r) sum++;
}
printf("%d\n", sum);
}
return 0;
}
| CPP |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import java.util.*;
public class B
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int m = input.nextInt();
int q = input.nextInt();
input.nextLine();
String s = input.nextLine();
String t = input.nextLine();
int[] occurences = new int[n + 1];
for(int i = 0; i <= n - m; i++)
{
occurences[i + 1] = occurences[i];
if(s.substring(i, i + m).equals(t))
occurences[i + 1]++;
}
while(q-- > 0)
{
int l = input.nextInt();
int r = input.nextInt();
if(r - l + 1 < m)
System.out.println(0);
else
System.out.println(occurences[r - m + 1] - occurences[l - 1]);
}
}
} | JAVA |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #####################################
import atexit, io, sys, collections
buffer = io.BytesIO()
sys.stdout = buffer
@atexit.register
def write(): sys.__stdout__.write(buffer.getvalue())
#####################################
import collections, math
n,m,qq = map(int, raw_input().split())
def f(u,v,dp):
u = min(n-1, u)
u = max(0, u)
u += len(t) -1
u -=1
if u > v:
return 0
v = min(n-1, v)
if (v >= n) or (v < 0):
return 0
return dp[v] - (dp[u] if u >= 0 else 0)
s = raw_input()
t = raw_input()
dp = [0 for _ in range(n)]
for i in range(len(t)-1, len(s)):
if s[i - len(t) +1:i+1] == t: dp[i] += 1
dp[i] += dp[i-1] if i-1 >= 0 else 0
for u,v in [map(int,raw_input().split()) for _ in range(qq)]:
if v - u + 1 >= m:
print f(u-1,v-1,dp)
else:
print 0 | PYTHON |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int arr[1001] = {0};
int u[200001] = {0};
int main() {
ios::sync_with_stdio(false);
int n, m, q, i, l, r;
cin >> n >> m >> q;
string s, t, ch;
cin >> s >> t;
int pos = -1, x = 0;
while (pos + 1 < s.size() && s.find(t, pos + 1) != string::npos) {
pos = s.find(t, pos + 1);
arr[pos + t.size() - 1]++;
}
for (i = 1; i < n; i++) {
arr[i] += arr[i - 1];
}
for (i = 1; i <= q; i++) {
cin >> l >> r;
if (r - l + 1 < t.size())
cout << "0";
else if (l > 1) {
cout << arr[r - 1] - arr[l - 1 + t.size() - 2];
} else if (l == 1) {
cout << arr[r - 1];
}
if (i != q) cout << endl;
}
return 0;
}
| CPP |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
;
const double eps = 1e-8;
const int mod = 10007;
const int maxn = 1e6 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f;
const unsigned long long p = 2333;
int n, m, q, l, r, ans;
unsigned long long hs[1007], ht, pw[1007];
char s[1007], t[1007];
void check(int l, int r) {
ans = 0;
for (int i = l; i <= r - m + 1; i++) {
if (hs[i + m - 1] - hs[i - 1] * pw[m] == ht) {
ans++;
}
}
}
int main() {
pw[0] = 1;
for (int i = 1; i < 1007; i++) {
pw[i] = pw[i - 1] * p;
}
scanf("%d%d%d", &n, &m, &q);
scanf("%s%s", s + 1, t + 1);
hs[0] = 0, ht = 0;
for (int i = 1; i <= n; i++) {
hs[i] = hs[i - 1] * p + (s[i] - 'a' + 1);
}
for (int i = 1; i <= m; i++) {
ht = ht * p + (t[i] - 'a' + 1);
}
while (q--) {
scanf("%d%d", &l, &r);
check(l, r);
printf("%d\n", ans);
}
return 0;
}
| CPP |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | n,m,q=map(int,input().split())
s=str(input())
t=str(input())
arrx=[]
i=0
count=0
while(i<n):
if(s[i]==t[0]):
j=0
while(j+i<n and j<m and s[j+i]==t[j]):
j+=1
if(j==len(t)):
count+=1
arrx.append(count)
i+=1
for i in range(q):
x,y=map(int,input().split())
if(y-x<m-1):
print(0)
else:
if(x==1):
print(arrx[y-m])
else:
print(arrx[y-m]-arrx[x-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 sys
import itertools
input = sys.stdin.readline
def main():
N, M, Q = [int(x) for x in input().split()]
S = input().strip()
T = input().strip()
LR = [[int(x) for x in input().split()] for _ in range(Q)]
ans = [0] * (N + 1)
for i in range(N):
if S[i:i + M] == T:
ans[i + M] = 1
ansaccu = list(itertools.accumulate(ans))
for l, r in LR:
if r - l + 1 < M:
print(0)
else:
print(ansaccu[r] - ansaccu[max(0, l + M - 2)])
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 | #include <bits/stdc++.h>
using namespace std;
inline int two(int n) { return 1 << n; }
inline void set_bit(int& n, int b) { n |= two(b); }
inline void unset_bit(int& n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
template <class T>
T gcd(T a, T b) {
return (b != 0 ? gcd<T>(b, a % b) : a);
}
template <class T>
T lcm(T a, T b) {
return (a / gcd<T>(a, b) * b);
}
const int maxx = 1e4;
int arr[maxx] = {0};
string s, t;
int n, m, q;
bool ok = true;
int main() {
std::ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> q;
cin >> s >> t;
for (int i = 0; i < n - m + 1; ++i) {
if (s[i] == t[0]) {
ok = true;
for (int j = 0; j < m; ++j) {
if (j + i > n - 1) break;
if (s[j + i] != t[j]) {
ok = false;
break;
}
}
if (ok) arr[i] = 1;
}
if (i > 0) arr[i] += arr[i - 1];
}
while (q--) {
int start, end;
cin >> start >> end;
start--, end--;
if (end - start + 1 < m)
cout << 0 << '\n';
else
cout << arr[end - m + 1] - (start ? arr[start - 1] : 0) << '\n';
}
}
| CPP |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | from bisect import bisect_left, bisect_right
n, m, q = map(int, raw_input().split(" "))
s = raw_input()
t = raw_input()
def occurrences(string, sub):
count = start = 0
starts = []
while True:
start = string.find(sub, start)
if start >= 0:
starts.append(start)
start += 1
else:
return starts
tut = occurrences(s, t)
tut = map(lambda x: x+1, tut)
# function to find first index >= x
def lowerIndex(arr, n, x):
l = 0
h = n-1
while (l <= h):
mid = int((l + h)/2)
if (arr[mid] >= x):
h = mid - 1
else:
l = mid + 1
return l
# function to find last index <= x
def upperIndex(arr, n, x):
l = 0
h = n-1
while (l <= h):
mid = int((l + h)/2)
if (arr[mid] <= x):
l = mid + 1
else:
h = mid - 1
return h
# function to count elements within given range
def countInRange(arr, n, x, y):
# initialize result
count = 0;
count = upperIndex(arr, n, y) - lowerIndex(arr, n, x) + 1;
return count
for i in xrange(q):
l, r = map(int, raw_input().split(" "))
if r-l+1 < len(t) or r < l:
print 0
else:
r = r - len(t) + 1
#print tut
#print l, r
print countInRange(tut, len(tut), l, r)
| 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 | '''input
10 3 4
foreforfor
for
1 3
1 10
5 6
5 7
'''
from collections import defaultdict as df
from bisect import bisect_left as bl
from random import randint as R
import sys
def gcd(a,b):
if b==0:
return a
return gcd(b,a%b)
mod=10**9+7
def brute(a,b,n):
return gcd(pow(a,n)+pow(b,n),abs(a-b))%mod
def good(a,b,n):
if n==1:
return gcd(a+b,abs(a-b))
return gcd(a*a+b*b,a-b)
n,m,q=[int(x) for x in raw_input().split()]
s=raw_input().strip()
s1=raw_input().strip()
ss=s[:m]
C1=[0]*(n+1)
C2=[0]*(n+1)
for i in range(n-m+1):
if s1==ss:
C1[i+m]+=1
C2[i+1]+=1
ss=ss[1:]
if i+m<n:
ss+=s[i+m]
for i in range(1,n+1):
C1[i]+=C1[i-1]
C2[i]+=C2[i-1]
#print C1,C2
for i in range(q):
l,r=[int(x) for x in raw_input().split()]
print max(C1[r]-C2[l-1],0)
| PYTHON |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int n, m, q;
char a[1005], b[1005];
struct qujian {
int s, e;
};
qujian qq[10005];
int num = 0;
int main() {
scanf("%d%d%d", &n, &m, &q);
scanf("%s\n%s", a, b);
if (n < m) {
for (int i = 0; i < q; i++) {
int x, y;
scanf("%d%d", &x, &y);
printf("0\n");
}
} else {
for (int i = 0; i <= n - m; i++) {
if (strncmp((a + i), b, m) == 0) {
qq[num].s = i;
qq[num++].e = i + m - 1;
}
}
for (int i = 0; i < q; i++) {
int x, y;
scanf("%d%d", &x, &y);
if (y - x + 1 < m)
printf("0\n");
else {
x = x - 1;
y = y - 1;
int flag = 0, tnum = 0;
for (int i = 0; i < num; i++) {
if (qq[i].s >= x && qq[i].e <= y) {
tnum++;
}
}
printf("%d\n", tnum);
}
}
}
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;
import java.util.InputMismatchException;
public class cf1016b {
public static void main(String[] args) {
FastScanner in = new FastScanner(System.in);
int N = in.nextInt();
int M = in.nextInt();
int Q = in.nextInt();
char[] s = in.next().toCharArray();
char[] t = in.next().toCharArray();
int[] start = new int[N + 1];
for(int i = 0; i < N; i++)
if(equals(s, t, i))
start[i + 1]++;
for(int i = 1; i < start.length; i++)
start[i] += start[i - 1];
StringBuilder ans = new StringBuilder();
for(int i = 0; i < Q; i++) {
int l = in.nextInt() - 1;
int r = in.nextInt() - t.length + 1;
if(r < l || r < 0)
ans.append(0);
else
ans.append(start[r] - start[l]);
ans.append('\n');
}
System.out.print(ans);
}
static boolean equals(char[] a, char[] b, int aInd) {
if(a.length < b.length || aInd + b.length > a.length)
return false;
for(int i = aInd; i < aInd + b.length; i++)
if(a[i] != b[i - aInd])
return false;
return true;
}
//Credits to Matt Fontaine
static class FastScanner{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastScanner(InputStream stream)
{
this.stream = stream;
}
int read()
{
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars){
curChar = 0;
try{
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
boolean isSpaceChar(int c)
{
return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1;
}
boolean isEndline(int c)
{
return c=='\n'||c=='\r'||c==-1;
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String next(){
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do{
res.appendCodePoint(c);
c = read();
}while(!isSpaceChar(c));
return res.toString();
}
String nextLine(){
int c = read();
while (isEndline(c))
c = read();
StringBuilder res = new StringBuilder();
do{
res.appendCodePoint(c);
c = read();
}while(!isEndline(c));
return res.toString();
}
}
}
| 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
sys.setrecursionlimit(2000)
from collections import Counter
from functools import reduce
# sys.stdin.readline()
if __name__ == "__main__":
# single variables
n, m, q = [int(val) for val in sys.stdin.readline().split()]
s = input()
t = input()
count = [int(s[i:i+m]==t) for i in range(n)]
for query in range(q):
l, r = [int(val)-1 for val in sys.stdin.readline().split()]
print(sum(count[l:max(l, r+2-m)]))
| PYTHON3 |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 10;
string s1, s2;
int ans[maxn];
int main() {
int n, m, q;
cin >> n >> m >> q;
cin >> s1 >> s2;
memset(ans, 0, sizeof(ans));
for (int i = 0; i + m <= n; i++) {
if (s1.substr(i, m) == s2) {
ans[i + 1]++;
}
}
for (int i = 1; i <= n; i++) {
ans[i] += ans[i - 1];
}
while (q--) {
int a, b;
cin >> a >> b;
b -= m - 1;
if (b < 0) b = 0;
cout << max(0, ans[b] - ans[a - 1]) << endl;
}
return 0;
}
| CPP |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int const lmt = 1e5 + 4;
long long pre[lmt];
int main() {
int n, m, q;
cin >> m >> n >> q;
string s, t;
cin >> s >> t;
string tt = t + '#' + s;
int len = tt.length();
int f[len];
int j = 0;
f[0] = 0;
for (int i = 1; i < len; i++) {
j = f[i - 1];
while (j > 0 && tt[i] != tt[j]) j = f[j - 1];
if (tt[i] == tt[j]) j++;
f[i] = j;
if (j == n) {
pre[i + 1 - (n + 1)] = 1;
}
}
for (int i = 1; i <= m; i++) {
pre[i] += pre[i - 1];
}
int l, r;
while (q--) {
cin >> l >> r;
if (r - l + 1 < n)
cout << "0\n";
else
cout << max(1ll * 0, pre[r] - pre[l + n - 2]) << "\n";
}
}
| CPP |
1016_B. Segment Occurrences | You are given two strings s and t, both consisting only of lowercase Latin letters.
The substring s[l..r] is the string which is obtained by taking characters s_l, s_{l + 1}, ..., s_r without changing the order.
Each of the occurrences of string a in a string b is a position i (1 β€ i β€ |b| - |a| + 1) such that b[i..i + |a| - 1] = a (|a| is the length of string a).
You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[l_i..r_i].
Input
The first line contains three integer numbers n, m and q (1 β€ n, m β€ 10^3, 1 β€ q β€ 10^5) β the length of string s, the length of string t and the number of queries, respectively.
The second line is a string s (|s| = n), consisting only of lowercase Latin letters.
The third line is a string t (|t| = m), consisting only of lowercase Latin letters.
Each of the next q lines contains two integer numbers l_i and r_i (1 β€ l_i β€ r_i β€ n) β the arguments for the i-th query.
Output
Print q lines β the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[l_i..r_i].
Examples
Input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
Output
0
1
0
1
Input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
Output
4
0
3
Input
3 5 2
aaa
baaab
1 3
1 1
Output
0
0
Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively. | 2 | 8 | import bisect
f=lambda: map(int, input().split())
n,m,q=f()
s,t=input(),input()
x,st=[],[]
c1=0
for i in range(n):
if "".join(c for c in s[i:i+m])==t:
x.append(i+1)
c1+=1
for i in range(q):
cnt=0
l,r=f()
if x!=[] and r-l+1>=m:
r=r-m+1
left=bisect.bisect_left(x,l)
right=bisect.bisect_left(x,r)
if right>=c1: right-=1
if x[right]>r: right-=1
cnt=right-left+1
st.append(str(cnt))
print('\n'.join(st))
| 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;
string s, t;
int n, m, q;
int v[100000] = {};
void match() {
for (int i = 0; i <= n - m; i++) {
v[i + 1] = v[i] + (s.substr(i, m) == t);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
cin >> n >> m >> q;
cin >> s;
cin >> t;
match();
for (int i = 0; i < q; i++) {
int a, b;
cin >> a >> b;
if ((b - a + 1) < m)
cout << "0\n";
else {
cout << v[b - m + 1] - v[a - 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 = list(map(int, input().strip().split()))
s = input()
t = input()
locs = []
for i in range(n):
if i + m <= n and s[i:i+m] == t:
locs.append(i)
#print(locs)
def find_max(nums, target):
l, r = 0, len(nums) - 1
while l < r:
mid = (l + r) // 2
if nums[mid] >= target:
r = mid
else:
l = mid + 1
if nums[l] >= target:
return l
else:
return len(nums)
def helper(l, r, nums):
if len(nums) == 0:
return 0
x = find_max(nums, l)
y = find_max(nums, r)
if y >= len(nums) or nums[y] > r:
return y - x
else:
return y - x + 1
for i in range(q):
l, r = list(map(int, input().strip().split()))
l -= 1
r -= 1
if r - m + 1 < l:
print(0)
else:
res = helper(l, r - m + 1, locs)
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 | from collections import defaultdict
n, m, q = [int(i) for i in input().split()]
s = input()
t = input()
x = []
sums = [0 for i in range(n)]
for i in range(q):
x.append([int(i) - 1 for i in input().split()])
for i in range(n):
if i - m + 1 >= 0:
if s[i - m + 1:i + 1] == t:
sums[i] += 1
if i != 0:
sums[i] += sums[i - 1]
for i in x:
if abs(i[1] - i[0]) + 1 < m:
print(0)
else:
l = sums[i[1]]
if i[0] + m - 2 < 0:
r = 0
else:
r = sums[min(n - 1, i[0] + m - 2)]
print(l - r)
| PYTHON3 |