计算 s[i] = s[i+1] 的索引数:范围查询
给定一个字符串str 。现在对于由两个整数L和R组成的每个查询,任务是找到索引的数量,使得str[i] = str[i+1]和L ≤ i, i+1 ≤ R 。
例子:
Input: str = “ggggggggggg”, query[] = {{1, 2}, {1, 5}}
Output: 1 4
The answer is 1 for first query and 4 for second query.
The condition is true for all indices except the last one in each query.
Input: str = “geeg”, query[] = {{0, 3}}
Output: 1
The condition is true only for i = 1.
方法:创建一个前缀数组pref使得pref[i]保存从1到i-1的所有索引的计数,使得str[i] = str[i+1] 。现在对于每个查询(L, R) ,结果将是pref[r] – pref[l] 。
下面是上述方法的实现:
C++
// C++ program to find substring with
#include
using namespace std;
// Function to create prefix array
void preCompute(int n, string s, int pref[])
{
pref[0] = 0;
for (int i = 1; i < n; i++) {
pref[i] = pref[i - 1];
if (s[i - 1] == s[i])
pref[i]++;
}
}
// Function to return the result of the query
int query(int pref[], int l, int r)
{
return pref[r] - pref[l];
}
// Driver Code
int main()
{
string s = "ggggggg";
int n = s.length();
int pref[n];
preCompute(n, s, pref);
// Query 1
int l = 1;
int r = 2;
cout << query(pref, l, r) << endl;
// Query 2
l = 1;
r = 5;
cout << query(pref, l, r) << endl;
return 0;
}
Java
// Java program to find substring with
import java.io.*;
class GFG {
// Function to create prefix array
static void preCompute(int n, String s, int pref[])
{
pref[0] = 0;
for (int i = 1; i < n; i++) {
pref[i] = pref[i - 1];
if (s.charAt(i - 1)== s.charAt(i))
pref[i]++;
}
}
// Function to return the result of the query
static int query(int pref[], int l, int r)
{
return pref[r] - pref[l];
}
// Driver Code
public static void main (String[] args) {
String s = "ggggggg";
int n = s.length();
int pref[] = new int[n];
preCompute(n, s, pref);
// Query 1
int l = 1;
int r = 2;
System.out.println( query(pref, l, r));
// Query 2
l = 1;
r = 5;
System.out.println(query(pref, l, r));
}
}
// This code is contributed by inder_verma..
Python3
# Python3 program for the given approach
# Function to create prefix array
def preCompute(n, s, pref):
for i in range(1,n):
pref[i] = pref[i - 1]
if s[i - 1] == s[i]:
pref[i] += 1
# Function to return the result of the query
def query(pref, l, r):
return pref[r] - pref[l]
if __name__ == "__main__":
s = "ggggggg"
n = len(s)
pref = [0] * n
preCompute(n, s, pref)
# Query 1
l = 1
r = 2
print(query(pref, l, r))
# Query 2
l = 1
r = 5
print(query(pref, l, r))
# This code is contributed by Rituraj Jain
C#
// C# program to find substring with
using System;
class GFG {
// Function to create prefix array
static void preCompute(int n, string s, int []pref)
{
pref[0] = 0;
for (int i = 1; i < n; i++) {
pref[i] = pref[i - 1];
if (s[i - 1]== s[i])
pref[i]++;
}
}
// Function to return the result of the query
static int query(int []pref, int l, int r)
{
return pref[r] - pref[l];
}
// Driver Code
public static void Main () {
string s = "ggggggg";
int n = s.Length;
int []pref = new int[n];
preCompute(n, s, pref);
// Query 1
int l = 1;
int r = 2;
Console.WriteLine( query(pref, l, r));
// Query 2
l = 1;
r = 5;
Console.WriteLine(query(pref, l, r));
}
}
// This code is contributed by inder_verma..
PHP
Javascript
输出:
1
4