给定字符串中“GFG”子序列的计数
给定一个长度为n的大写字母字符串。任务是找到给定字符串中“GFG”子序列的计数。
例子:
Input : str[] = "GFGFG"
Output : 4
GFGFG, GFGFG, GFGFG, GFGFG
Input : str[] = "ABCFGFPG"
Output : 1
要查找给定字符串中“GFG”子序列的数量,请观察每个“F”,如果我们知道它之前和之后的“G”数量。那么那个'F'的“GFG”子序列的数量等于'F'之前和之后的'G'数量的乘积。
因此,想法是维护一个数组 arr[],其中 arr[i] 在索引 i 之前存储“G”的编号,如果字符串的第 i 个字符是“F”并且在索引 i 之前存储“F”的编号,如果第 i 个字符是“G”。
此外,每当遇到“G”时,我们都会计算并存储“GFG”子序列的数量。
下面是这种方法的实现:
C++
// CPP Program to find the "GFG" subsequence in
// the given string
#include
using namespace std;
#define MAX 100
// Print the count of "GFG" subsequence in the string
void countSubsequence(char s[], int n)
{
int cntG = 0, cntF = 0, result = 0, C=0;
// Traversing the given string
for (int i = 0; i < n; i++) {
switch (s[i]) {
// If the character is 'G', increment
// the count of 'G', increase the result
// and update the array.
case 'G':
cntG++;
result+=C;
break;
// If the character is 'F', increment
// the count of 'F' and update the array.
case 'F':
cntF++;
C+=cntG;
break;
// Ignore other character.
default:
continue;
}
}
cout << result << endl;
}
// Driven Program
int main()
{
char s[] = "GFGFG";
int n = strlen(s);
countSubsequence(s, n);
return 0;
}
Java
// Java Program to find the "GFG" subsequence
// in the given string
public class GFG {
static int max = 100;
// Print the count of "GFG" subsequence
// in the string
static void countSubsequence(String s, int n)
{
int cntG = 0, cntF = 0, result = 0, C=0;
// Traversing the given string
for (int i = 0; i < n; i++) {
switch (s.charAt(i)) {
// If the character is 'G',
// increment the count of 'G',
// increase the result and
// update the array.
case 'G':
cntG++;
result+=C;
break;
// If the character is 'F',
// increment the count of 'F'
// and update the array.
case 'F':
cntF++;
C+=cntG;
break;
// Ignore other character.
default:
continue;
}
}
System.out.println(result);
}
// Driver code
public static void main(String args[]) {
String s= "GFGFG";
int n = s.length();
countSubsequence(s, n);
}
}
// This code is contributed by Sam007
Python3
# Python 3 Program to find the "GFG"
# subsequence in the given string
MAX = 100
# Print the count of "GFG" subsequence
# in the string
def countSubsequence(s, n):
cntG = 0
cntF = 0
result = 0
C=0
# Traversing the given string
for i in range(n):
if (s[i] == 'G'):
# If the character is 'G', increment
# the count of 'G', increase the result
# and update the array.
cntG += 1
result += C
continue
# If the character is 'F', increment
# the count of 'F' and update the array.
if (s[i] == 'F'):
cntF += 1
C += cntG
continue
# Ignore other character.
else:
continue
print(result)
# Driver Code
if __name__ == '__main__':
s = "GFGFG"
n = len(s)
countSubsequence(s, n)
# This code is contributed by
# Sanjit_Prasad
C#
// C# Program to find the "GFG" subsequence
// in the given string
using System;
class GFG {
// Print the count of "GFG" subsequence
// in the string
static void countSubsequence(string s,
int n)
{
int cntG = 0, cntF = 0, result = 0, C=0;
// Traversing the given string
for (int i = 0; i < n; i++) {
switch (s[i]) {
// If the character is 'G',
// increment the count of 'G',
// increase the result and
// update the array.
case 'G':
cntG++;
result += C;
break;
// If the character is 'F',
// increment the count of 'F'
// and update the array.
case 'F':
cntF++;
C+=cntG;
break;
// Ignore other character.
default:
continue;
}
}
Console.WriteLine(result);
}
// Driver code
public static void Main()
{
string s= "GFGFG";
int n = s.Length;
countSubsequence(s, n);
}
}
// This code is contributed by Sam007.
Javascript
输出:
4
时间复杂度: O(n)