Dada una string str . Ahora, para cada consulta que consta de dos enteros L y R , la tarea es encontrar el número de índices tales que str[i] = str[i+1] y L ≤ i, i+1 ≤ R.
Ejemplos:
Entrada: str = “gggggggggggg”, consulta[] = {{1, 2}, {1, 5}}
Salida: 1 4
La respuesta es 1 para la primera consulta y 4 para la segunda consulta.
La condición es verdadera para todos los índices excepto el último de cada consulta.
Entrada: str = “geeg”, query[] = {{0, 3}}
Salida: 1
La condición es verdadera solo para i = 1.
Enfoque: Cree una array de prefijos pref tal que pref[i] contenga la cuenta de todos los índices de 1 a i-1 tal que str[i] = str[i+1] . Ahora, para cada consulta (L, R), el resultado será pref[r] – pref[l] .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to find substring with #include <bits/stdc++.h> 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
<?php // PHP program to find substring with // Function to create prefix array function preCompute($n, $s, &$pref) { $pref[0] = 0; for ($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 function query(&$pref, $l, $r) { return $pref[$r] - $pref[$l]; } // Driver Code $s = "ggggggg"; $n = strlen($s); $pref = array_fill(0, $n, NULL); preCompute($n, $s, $pref); // Query 1 $l = 1; $r = 2; echo query($pref, $l, $r) . "\n"; // Query 2 $l = 1; $r = 5; echo query($pref, $l, $r) . "\n"; // This code is contributed by ita_c ?>
Javascript
<script> // Javascript program to find substring with // Function to create prefix array function preCompute(n,s,pref) { pref[0] = 0; for (let 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 function query(pref,l,r) { return pref[r] - pref[l]; } // Driver Code let s = "ggggggg"; let n = s.length; let pref = new Array(n); preCompute(n, s, pref); // Query 1 let l = 1; let r = 2; document.write( query(pref, l, r)+"<br>"); // Query 2 l = 1; r = 5; document.write(query(pref, l, r)+"<br>"); // This code is contributed by rag2127 </script>
1 4
Complejidad de tiempo: O(n+k) donde n es el tamaño de la string yk es el número de consultas.
Espacio Auxiliar: O(n)
Publicación traducida automáticamente
Artículo escrito por Abdullah Aslam y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA