给定长度为N的字符串S (由小写字母组成),并以[L,R]形式查询Q [] [ ],任务是计算出现在[L]范围内的奇数次的字符数,R]。
例子 :
Input: S = “geeksforgeeks”, Q[][] = {{2, 4}, {0, 3}, {0, 12}}
Output: 3 2 3
Explanation:
Characters occurring odd number of times in [2, 4] : {‘e’, ‘k’, ‘s’}.
Characters occurring odd number of times in [0, 3] : {‘g’, ‘k’}.
Characters occurring odd number of times in [0, 12 ] : {‘f’, ‘o’, ‘r’}.
Input: S = “hello”, Q[][] = {{0, 4}}
Output: 3
Explanation: Characters occurring odd number of times in [0, 4] : {‘h’, ‘e’, ‘o’}.
方法 :
请按照以下步骤解决问题:
- 每个字符都可以用2的唯一幂表示(按升序排列)。例如, ‘a’的2 0 , ‘ b’的2 1 ,以此类推,直到‘z’的2 25 。
- 初始化大小为N的数组arr [] ,其中arr [i]是S [i]的对应整数值。
- 构造一个大小为N的前缀数组prefix [] ,其中prefix [i]是对从arr [0]到arr [i]的所有数字执行的XOR运算的值。
- {arr [L],arr [L + 1],…,arr [R – 1],arr [R]}的XOR值中的置位位数给出了给定范围[L,R]所需的答案。
下面是上述方法的实现:
C++14
// C++ Program to implement
// the above problem
#include
using namespace std;
// Function to print the number
// of characters having odd
// frequencies for each query
void queryResult(int prefix[],
pair Q)
{
int l = Q.first;
int r = Q.second;
if (l == 0) {
int xorval = prefix[r];
cout << __builtin_popcount(xorval)
<< endl;
}
else {
int xorval = prefix[r]
^ prefix[l - 1];
cout << __builtin_popcount(xorval)
<< endl;
}
}
// A function to construct
// the arr[] and prefix[]
void calculateCount(string S,
pair Q[],
int m)
{
// Stores array length
int n = S.length();
// Stores the unique powers of 2
// associated to each character
int arr[n];
for (int i = 0; i < n; i++) {
arr[i] = (1 << (S[i] - 'a'));
}
// Prefix array to store the
// XOR values from array elements
int prefix[n];
int x = 0;
for (int i = 0; i < n; i++) {
x ^= arr[i];
prefix[i] = x;
}
for (int i = 0; i < m; i++) {
queryResult(prefix, Q[i]);
}
}
// Driver Code
int main()
{
string S = "geeksforgeeks";
pair Q[] = { { 2, 4 },
{ 0, 3 },
{ 0, 12 } };
calculateCount(S, Q, 3);
}
Java
// Java Program to implement
// the above problem
import java.util.*;
class GFG{
static class pair
{
int first, second;
public pair(int first, int second)
{
this.first = first;
this.second = second;
}
}
// Function to print the number
// of characters having odd
// frequencies for each query
static void queryResult(int prefix[], pair Q)
{
int l = Q.first;
int r = Q.second;
if (l == 0)
{
int xorval = prefix[r];
System.out.print(Integer.bitCount(xorval) + "\n");
}
else
{
int xorval = prefix[r] ^ prefix[l - 1];
System.out.print(Integer.bitCount(xorval) + "\n");
}
}
// A function to construct
// the arr[] and prefix[]
static void calculateCount(String S, pair Q[], int m)
{
// Stores array length
int n = S.length();
// Stores the unique powers of 2
// associated to each character
int[] arr = new int[n];
for (int i = 0; i < n; i++)
{
arr[i] = (1 << (S.charAt(i) - 'a'));
}
// Prefix array to store the
// XOR values from array elements
int[] prefix = new int[n];
int x = 0;
for (int i = 0; i < n; i++)
{
x ^= arr[i];
prefix[i] = x;
}
for (int i = 0; i < m; i++)
{
queryResult(prefix, Q[i]);
}
}
// Driver Code
public static void main(String[] args)
{
String S = "geeksforgeeks";
pair Q[] = {new pair(2, 4),
new pair(0, 3), new pair(0, 12)};
calculateCount(S, Q, 3);
}
}
// This code is contributed by shikhasingrajput
Python3
# Python3 program to implement
# the above approach
# Function to print the number
# of characters having odd
# frequencies for each query
def queryResult(prefix, Q):
l = Q[0]
r = Q[1]
if(l == 0):
xorval = prefix[r]
print(bin(xorval).count('1'))
else:
xorval = prefix[r] ^ prefix[l - 1]
print(bin(xorval).count('1'))
# A function to construct
# the arr[] and prefix[]
def calculateCount(S, Q, m):
# Stores array length
n = len(S)
# Stores the unique powers of 2
# associated to each character
arr = [0] * n
for i in range(n):
arr[i] = (1 << (ord(S[i]) - ord('a')))
# Prefix array to store the
# XOR values from array elements
prefix = [0] * n
x = 0
for i in range(n):
x ^= arr[i]
prefix[i] = x
for i in range(m):
queryResult(prefix, Q[i])
# Driver Code
if __name__ == '__main__':
S = "geeksforgeeks"
# Function call
Q = [ [ 2, 4 ],
[ 0, 3 ],
[ 0, 12 ] ]
calculateCount(S, Q, 3)
# This code is contributed by Shivam Singh
C#
// C# program to implement
// the above problem
using System;
class GFG{
class pair
{
public int first, second;
public pair(int first, int second)
{
this.first = first;
this.second = second;
}
}
// Function to print the number
// of characters having odd
// frequencies for each query
static void queryResult(int []prefix, pair Q)
{
int l = Q.first;
int r = Q.second;
if (l == 0)
{
int xorval = prefix[r];
Console.Write(countSetBits(xorval) + "\n");
}
else
{
int xorval = prefix[r] ^ prefix[l - 1];
Console.Write(countSetBits(xorval) + "\n");
}
}
// A function to construct
// the []arr and prefix[]
static void calculateCount(String S, pair []Q,
int m)
{
// Stores array length
int n = S.Length;
// Stores the unique powers of 2
// associated to each character
int[] arr = new int[n];
for(int i = 0; i < n; i++)
{
arr[i] = (1 << (S[i] - 'a'));
}
// Prefix array to store the
// XOR values from array elements
int[] prefix = new int[n];
int x = 0;
for(int i = 0; i < n; i++)
{
x ^= arr[i];
prefix[i] = x;
}
for(int i = 0; i < m; i++)
{
queryResult(prefix, Q[i]);
}
}
static int countSetBits(long x)
{
int setBits = 0;
while (x != 0)
{
x = x & (x - 1);
setBits++;
}
return setBits;
}
// Driver Code
public static void Main(String[] args)
{
String S = "geeksforgeeks";
pair []Q = { new pair(2, 4),
new pair(0, 3),
new pair(0, 12) };
calculateCount(S, Q, 3);
}
}
// This code is contributed by Amit Katiyar
输出:
3
2
3
时间复杂度: O(N + Q)
辅助空间: O(N)