给定一个仅由字符‘a’和‘b’组成的字符串S和一个整数N。将字符串S相加N次以获得字符串T。您的任务是计算a的数量严格大于b的前缀的数量。
字符串T = S + S + S + S……。 N次
例子 :
Input : aba 2
Output : 5
Explanation :
The string T is "abaaba". It has five prefixes
which contain more a-s than b-s: "a", "aba",
"abaa", "abaab" and "abaaba".
Input : baa 3
Output : 6
Explanation : The string T is "baabaabaa". The strings
"baa", "baaba", "baabaa", "baabaab", "baabaaba" and
"baabaabaa" are the six valid prefixes.
天真的方法:执行此程序的一种简单方法是生成整个字符串T,然后运行循环以检查有效前缀(其中a的数量大于b的数量)。如果N的值非常大,则此方法效率不高,但很耗时。
高效的方法:
请注意,该字符串是重复的。因此,我们不必检查整个字符串T。
在字符串S上操作。让,
count =字符串S中的前缀数
A =字符串S中字符“ a”的出现频率
B =字符串S中字符“ b”的出现频率
情况1:计数== 0
如果有效前缀数为零。然后,即使我们生成了整个字符串T。有效前缀的数量仍将为零。
情况2:计数> 0
此案例包含三个子案例:
一种。 A == B
在这种情况下,先前S的级联对S的传入/新级联没有影响。换句话说,当A!= B时,在每次将S加到T之后,(AB)的值都会发生一些变化,这会影响S的任何未来级联对计数的贡献。这意味着由于A == B,因此T中b的数目将不会以与每次添加的a数目相同的速率增加,这将影响到下一个对a的贡献最终答案。当A == B时不是这种情况。因此,每次添加S都会对最终答案有所帮助。 S中有N个加法,我们之前已经通过简单的循环找到了计数。因此,在这种情况下,答案=计数*N。
b。 A 例如,字符串S的Say计数在添加1000后会收敛为零。如果N = 99999,我们只需要检查到1000,然后忽略其余情况即可。如果N = 5,我们必须计算到5次加法。 C。 A> B 下面是上述方法的实现:
在这种情况下,由于A
显然,向T每次添加S都会增加AB。因此,T中a的数量将比b中的数量增加更快,这将增加S的每个将来添加对最终答案的贡献。加法对我们的答案的最大可能贡献可以是| S |,即字符串S的长度。因此,在某些加法之后,每个字符串的计数将饱和到字符串的长度。我们必须检查,直到发生这种情况。
例如:说X后,字符串S的计数饱和到S的长度。因此,我们必须计算到X的计数,然后加上等于(NX)* S长度的残差(iff N> X)
如果N C++
// CPP code to count the prefixes
// with more a than b
#include
Java
// Java code to count the
// prefixes with more a than b
import java.io.*;
class GFG
{
// Function to
// count prefixes
static int prefix(String k, int n)
{
int a = 0, b = 0,
count = 0;
int i = 0;
int len = k.length();
// calculating for string S
for (i = 0; i < len; i++)
{
if (k.charAt(i) == 'a')
a++;
if (k.charAt(i) == 'b')
b++;
if (a > b)
{
count++;
}
}
// count==0 or when N==1
if (count == 0 || n == 1)
{
System.out.println(count);
return 0;
}
// when all characters
// are a or a-b==0
if (count == len || a - b == 0)
{
System.out.println(count * n);
return 0;
}
int n2 = n - 1, count2 = 0;
// checking for saturation
// of string after repetitive
// addition
while (n2 != 0)
{
for (i = 0; i < len; i++)
{
if (k.charAt(i) == 'a')
a++;
if (k.charAt(i) == 'b')
b++;
if (a > b)
{
count2++;
}
}
count += count2;
n2--;
if (count2 == 0)
break;
if (count2 == len)
{
count += (n2 * count2);
break;
}
count2 = 0;
}
return count;
}
// Driver Code
public static void main (String[] args)
{
String S = "aba";
int N = 2;
System.out.println(prefix(S, N));
S = "baa";
N = 3;
System.out.println(prefix(S, N)) ;
}
}
// This code is contributed
// by anuj_67.
Python3
# Python3 code to count the prefixes
# with more a than b
# Function to count prefixes
def prefix(k, n):
a = 0
b = 0
count = 0
i = 0
Len = len(k)
# calculating for string S
for i in range(Len):
if (k[i] == "a"):
a += 1
if (k[i] == "b"):
b += 1
if (a > b) :
count += 1
# count==0 or when N==1
if (count == 0 or n == 1):
print(count)
return 0
# when all characters are a or a-b==0
if (count == Len or a - b == 0) :
print(count * n)
return 0
n2 = n - 1
count2 = 0
# checking for saturation of
# string after repetitive addition
while (n2 != 0):
for i in range(Len):
if (k[i] == "a"):
a += 1
if (k[i] == "b"):
b += 1
if (a > b):
count2 += 1
count += count2
n2 -= 1
if (count2 == 0):
break
if (count2 == Len):
count += (n2 * count2)
break
count2 = 0
return count
# Driver Code
S = "aba"
N = 2
print(prefix(S, N))
S = "baa"
N = 3
print(prefix(S, N))
# This code is contributed by
# Mohit kumar 29
C#
// C# code to count the
// prefixes with more
// a than b
using System;
class GFG
{
// Function to
// count prefixes
static int prefix(String k, int n)
{
int a = 0, b = 0,
count = 0;
int i = 0;
int len = k.Length;
// calculating for string S
for (i = 0; i < len; i++)
{
if (k[i] == 'a')
a++;
if (k[i] == 'b')
b++;
if (a > b)
{
count++;
}
}
// count==0 or when N==1
if (count == 0 || n == 1)
{
Console.WriteLine(count);
return 0;
}
// when all characters
// are a or a-b==0
if (count == len ||
a - b == 0)
{
Console.WriteLine(count * n);
return 0;
}
int n2 = n - 1, count2 = 0;
// checking for saturation
// of string after repetitive
// addition
while (n2 != 0)
{
for (i = 0; i < len; i++)
{
if (k[i] == 'a')
a++;
if (k[i] == 'b')
b++;
if (a > b)
{
count2++;
}
}
count += count2;
n2--;
if (count2 == 0)
break;
if (count2 == len)
{
count += (n2 * count2);
break;
}
count2 = 0;
}
return count;
}
// Driver Code
public static void Main ()
{
string S = "aba";
int N = 2;
Console.WriteLine(prefix(S, N));
S = "baa";
N = 3;
Console.WriteLine(prefix(S, N)) ;
}
}
// This code is contributed
// by anuj_67.
PHP
$b)
{
$count++;
}
}
// count==0 or when N==1
if ($count == 0 || $n == 1)
{
echo($count);
return 0;
}
// when all characters
// are a or a-b==0
if ($count == $len || $a - $b == 0)
{
echo($count * $n);
return 0;
}
$n2 = $n - 1; $count2 = 0;
// checking for saturation
// of string after repetitive
// addition
while ($n2 != 0)
{
for ($i = 0; $i < $len; $i++)
{
if ($k[$i] == 'a')
$a++;
if ($k[$i] == 'b')
$b++;
if ($a > $b)
{
$count2++;
}
}
$count += $count2;
$n2--;
if ($count2 == 0)
break;
if ($count2 == $len)
{
$count += ($n2 * $count2);
break;
}
$count2 = 0;
}
return $count;
}
// Driver Code
$S = "aba";
$N = 2;
echo(prefix($S,$N)."\n");
$S = "baa";
$N = 3;
echo(prefix($S, $N)."\n");
// This code is contributed
// by Mukul Singh.
5
6