对长度为 1 到 N 的每个前缀执行所述操作后每个小写字符的计数
给定一个包含N个小写英文字母的字符串S和一个字典Dict ,该字典将所有从 'a' 到 'z' 的小写英文字母映射到1或-1 。对于长度为K的字符串,可以应用以下操作:
- 从索引1到K找到最大字符,并找到相应最大字符的字典映射。
- 如果字典映射为1 ,则将所有字符从1递增到K ,即 'a' 变为 'b','b' 变为 'c',...... . . ,'z'变成'a'。
- 如果字典映射为-1 ,则将所有字符从1递减到K 。即'a'变成'z','b'变成'a',。 . . , 'z' 变成 'y'
对长度为1到N的字符串的每个前缀执行所描述的操作。
任务是在对长度为1到N的字符串的每个前缀执行所描述的操作后确定每个小写英文字母的计数。
例子:
Input: S=”ab”, Dict[] = [1,-1, 1, 1, 1, 1, 1, 1, 1, 1, 1,-1, 1,-1, 1, 1, 1, 1,-1,-1,-1,1,1,-1,1-1]
Output: 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Explanation:
For Prefix of length 1: Max character = ‘a’, Mapping = 1. So, increment prefix string. S=”bb”.
For Prefix of length 2: Max character = ‘b’, Mapping = -1. So, decrement prefix string. S = “aa”.
Input: S=”abcb”, Dict[] = [1, -1, 1, 1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, 1,-1,-1,-1,1,1,-1,1-1]
Output: 0 0 3 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Explanation:
For Prefix of length 1: Max character = ‘a’, Mapping = 1. So, increment prefix string. S=”bbcb”.
For Prefix of length 2: Max character = ‘b’, Mapping = -1. So, decrement prefix string. S = “aacb”.
For Prefix of length 3: Max character = ‘c’, Mapping = 1. So, increment prefix string. S=”bbdb”.
For Prefix of length 4: Max character = ‘d’, Mapping = 1. So, increment prefix string. S = “ccec”.
方法:解决方案基于贪婪方法。请按照以下步骤解决此问题:
- 运行一个从i = 0到i < N的循环,并在该循环中运行另一个从j = i到j >= 0的循环(遍历前缀)。
- 现在找到该前缀中的最大元素以及它在Dict中映射到的数字,例如mp 。
- 然后根据数字mp应用递增或递减操作。
- 现在,创建一个向量ans来存储每个元素的频率。
- 遍历整个字符串并填充向量ans 。
- 打印向量ans的元素。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the frequency of
// all character after performig N operations
void processString(string& S, vector& Dict)
{
int N = S.length();
char mx;
// Vector to store the frequency
// of all characters
vector ans(26, 0);
for (int i = 0; i < N; i++) {
mx = 96;
for (int j = 0; j <= i; j++) {
mx = max(mx, S[j]);
}
for (int j = 0; j <= i; j++) {
// If S[j] is 'a' and
// Dict[S[j]] is -1 then
// make S[j] equals to 'z'
if (S[j] + Dict[mx - 'a'] < 97) {
S[j] = S[j] +
Dict[mx - 'a'] + 26;
}
// If S[j] is 'z' and
// Dict[S[j]] is 1
// then make S[j] equals to 'a'
else if (S[j] + Dict[mx - 'a']
> 122) {
S[j] = S[j] +
Dict[mx - 'a'] - 26;
}
else {
S[j] += Dict[mx - 'a'];
}
}
}
for (int i = 0; i < N; i++) {
ans[S[i] - 'a']++;
}
for (auto x : ans) {
cout << x << ' ';
}
}
// Driver code
int main()
{
string S = "ab";
vector Dict
= { 1, -1, 1, 1, 1, -1, 1, 1,
1, -1, 1, -1, 1, -1, 1, 1, 1,
1, -1, -1, -1, 1, 1, -1, 1 - 1 };
processString(S, Dict);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to find the frequency of
// all character after performig N operations
static void processString(char[] S, int[] Dict)
{
int N = S.length;
char mx;
// Vector to store the frequency
// of all characters
int[] ans = new int[26];
for (int i = 0; i < N; i++) {
mx = 96;
for (int j = 0; j <= i; j++) {
mx = (char) Math.max(mx, S[j]);
}
for (int j = 0; j <= i; j++)
{
// If S[j] is 'a' and
// Dict[S[j]] is -1 then
// make S[j] equals to 'z'
if (S[j] + Dict[mx - 'a'] < 97) {
S[j] = (char) (S[j] +
Dict[mx - 'a'] + 26);
}
// If S[j] is 'z' and
// Dict[S[j]] is 1
// then make S[j] equals to 'a'
else if (S[j] + Dict[mx - 'a']
> 122) {
S[j] = (char) (S[j] +
Dict[mx - 'a'] - 26);
}
else {
S[j] += Dict[mx - 'a'];
}
}
}
for (int i = 0; i < N; i++) {
ans[S[i] - 'a']++;
}
for (int x : ans) {
System.out.print(x +" ");
}
}
// Driver code
public static void main(String[] args)
{
char []S = "ab".toCharArray();
int[] Dict
= { 1, -1, 1, 1, 1, -1, 1, 1,
1, -1, 1, -1, 1, -1, 1, 1, 1,
1, -1, -1, -1, 1, 1, -1, 1 - 1 };
processString(S, Dict);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python program for the above approach
# Function to find the frequency of
# all character after performig N operations
def processString(S, Dict):
N = len(S);
mx = '';
# Vector to store the frequency
# of all characters
ans = [0 for i in range(26)];
for i in range(N):
mx = chr(96);
for j in range(i+1):
mx = chr(max(ord(mx), ord(S[j])));
for j in range(i + 1):
# If S[j] is ord('a') and
# Dict[S[j]] is -1 then
# make S[j] equals to 'z'
if (ord(S[j]) + Dict[ord(mx) - ord('a')] < 97):
S[j] = ord(S[j] + Dict[ord(mx) - ord('a')] + 26);
# If S[j] is 'z' and
# Dict[S[j]] is 1
# then make S[j] equals to ord('a')
elif (ord(S[j]) + Dict[ord(mx) - ord('a')] > 122):
S[j] = ord(ord(S[j]) + Dict[ord(mx) - ord('a')] - 26);
else:
tempc = chr(ord(S[j]) + Dict[ord(mx) - ord('a')]);
S= S[0:j]+tempc+S[j:]
#S[j] = tempc;
for i in range(N):
ans[ord(S[i]) - ord('a')] += 1;
for x in ans:
print(x, end=" ");
# Driver code
if __name__ == '__main__':
S = "ab";
Dict = [1, -1, 1, 1, 1, -1, 1, 1, 1,
-1, 1, -1, 1, -1, 1, 1, 1,
1, -1, -1, -1, 1, 1, -1, 1 - 1];
processString(S, Dict);
# This code is contributed by 29AjayKumar
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to find the frequency of
// all character after performig N operations
static void processString(char[] S, int[] Dict)
{
int N = S.Length;
char mx;
// List to store the frequency
// of all characters
int[] ans = new int[26];
for (int i = 0; i < N; i++) {
mx = (char)96;
for (int j = 0; j <= i; j++) {
mx = (char) Math.Max(mx, S[j]);
}
for (int j = 0; j <= i; j++)
{
// If S[j] is 'a' and
// Dict[S[j]] is -1 then
// make S[j] equals to 'z'
if (S[j] + Dict[mx - 'a'] < 97) {
S[j] = (char) (S[j] +
Dict[mx - 'a'] + 26);
}
// If S[j] is 'z' and
// Dict[S[j]] is 1
// then make S[j] equals to 'a'
else if (S[j] + Dict[mx - 'a']
> 122) {
S[j] = (char) (S[j] +
Dict[mx - 'a'] - 26);
}
else {
S[j] += (char)Dict[mx - 'a'];
}
}
}
for (int i = 0; i < N; i++) {
ans[S[i] - 'a']++;
}
foreach (int x in ans) {
Console.Write(x +" ");
}
}
// Driver code
public static void Main(String[] args)
{
char []S = "ab".ToCharArray();
int[] Dict
= { 1, -1, 1, 1, 1, -1, 1, 1,
1, -1, 1, -1, 1, -1, 1, 1, 1,
1, -1, -1, -1, 1, 1, -1, 1 - 1 };
processString(S, Dict);
}
}
// This code is contributed by 29AjayKumar
2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
时间复杂度: O(N 2 )
辅助空间: O(1)