给定整数N,任务是从无限字符串str的前N个字符中找到仅包含4个字符的最长子字符串的长度。
字符串str是通过将仅4和5组成的数字按升序级联而生成的。例如4个,5,44,45,54,55等。因此,字符串str看起来像“ 4544455455444445454455…” 。
例子:
Input : N = 4
Output : 2
First 4 characters of str are "4544".
Therefore the required length is 2.
Input : N = 10
Output : 3
First 10 characters of str are "4544455455".
Therefore the required length is 3.
方法:通过观察模式可以轻松解决问题。任务是计算出现在字符串的最大连续4个数字。因此,无需生成整个字符串。
如果将字符串分成不同的组,则可以观察到一种模式,因为第一组将有2个字符,第二组将有4个字符,第三组将有8个字符,依此类推。
例如:
Group 1 -> 45
Group 2 -> 44455455
Group 3 -> 444445454455544545554555
.
.
.
and, so on…
现在,任务简化为查找N所在的组,以及从一开始就在该组中覆盖了多少个字符。
这里,
现在,
组1具有1 * 2 ^ 1个字符
组2具有2 * 2 ^ 2个字符
通常,组K具有K * 2 ^ K个字符。因此,问题减少到找到给定的N属于哪个组。这可以通过使用前缀求和数组pre []轻松找到,其中第ith个元素包含不超过第ith个字符数之和。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
#define MAXN 30
// Function to return the length of longest
// contiguous string containing only 4’s from
// the first N characters of the string
int countMaxLength(int N)
{
// Initialize result
int res;
// Initialize prefix sum array of
// characters and product variable
int pre[MAXN], p = 1;
// Preprocessing of prefix sum array
pre[0] = 0;
for (int i = 1; i < MAXN; i++) {
p *= 2;
pre[i] = pre[i - 1] + i * p;
}
// Initialize variable to store the
// string length where N belongs to
int ind;
// Finding the string length where
// N belongs to
for (int i = 1; i < MAXN; i++) {
if (pre[i] >= N) {
ind = i;
break;
}
}
int x = N - pre[ind - 1];
int y = 2 * ind - 1;
if (x >= y)
res = min(x, y);
else
res = max(x, 2 * (ind - 2) + 1);
return res;
}
// Driver Code
int main()
{
int N = 25;
cout << countMaxLength(N);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
static int MAXN = 30;
// Function to return the length of longest
// contiguous string containing only 4's from
// the first N characters of the string
static int countMaxLength(int N)
{
// Initialize result
int res;
// Initialize prefix sum array of
// characters and product variable
int pre[] = new int[MAXN];
int p = 1;
// Preprocessing of prefix sum array
pre[0] = 0;
for (int i = 1; i < MAXN; i++)
{
p *= 2;
pre[i] = pre[i - 1] + i * p;
}
// Initialize variable to store the
// string length where N belongs to
int ind = 0;
// Finding the string length where
// N belongs to
for (int i = 1; i < MAXN; i++)
{
if (pre[i] >= N)
{
ind = i;
break;
}
}
int x = N - pre[ind - 1];
int y = 2 * ind - 1;
if (x >= y)
res = Math.min(x, y);
else
res = Math.max(x, 2 * (ind - 2) + 1);
return res;
}
// Driver Code
public static void main(String[] args)
{
int N = 25;
System.out.println(countMaxLength(N));
}
}
// This code is contributed by Code_Mech.
Python3
# Python 3 implementation of the approach
MAXN = 30
# Function to return the length of longest
# contiguous string containing only 4’s from
# the first N characters of the string
def countMaxLength(N):
# Initialize result
# Initialize prefix sum array of
# characters and product variable
pre = [0 for i in range(MAXN)]
p = 1
# Preprocessing of prefix sum array
pre[0] = 0
for i in range(1, MAXN, 1):
p *= 2
pre[i] = pre[i - 1] + i * p
# Initialize variable to store the
# string length where N belongs to
# Finding the string length where
# N belongs to
for i in range(1, MAXN, 1):
if (pre[i] >= N):
ind = i
break
x = N - pre[ind - 1]
y = 2 * ind - 1
if (x >= y):
res = min(x, y)
else:
res = max(x, 2 * (ind - 2) + 1)
return res
# Driver Code
if __name__ == '__main__':
N = 25
print(countMaxLength(N))
# This code is contributed by
# Surendra_Gangwar
C#
// C# implementation of the approach
using System;
class GFG
{
static int MAXN = 30;
// Function to return the length of longest
// contiguous string containing only 4's from
// the first N characters of the string
static int countMaxLength(int N)
{
// Initialize result
int res;
// Initialize prefix sum array of
// characters and product variable
int[] pre = new int[MAXN];
int p = 1;
// Preprocessing of prefix sum array
pre[0] = 0;
for (int i = 1; i < MAXN; i++)
{
p *= 2;
pre[i] = pre[i - 1] + i * p;
}
// Initialize variable to store the
// string length where N belongs to
int ind = 0;
// Finding the string length where
// N belongs to
for (int i = 1; i < MAXN; i++)
{
if (pre[i] >= N)
{
ind = i;
break;
}
}
int x = N - pre[ind - 1];
int y = 2 * ind - 1;
if (x >= y)
res = Math.Min(x, y);
else
res = Math.Max(x, 2 * (ind - 2) + 1);
return res;
}
// Driver Code
public static void Main()
{
int N = 25;
Console.WriteLine(countMaxLength(N));
}
}
// This code is contributed by Code_Mech.
PHP
= $N)
{
$ind = $i;
break;
}
}
$x = $N - $pre[$ind - 1];
$y = 2 * $ind - 1;
if ($x >= $y)
$res = min($x, $y);
else
$res = max($x, 2 * ($ind - 2) + 1);
return $res;
}
// Driver Code
$N = 25;
echo countMaxLength($N);
// This code is contributed by Ryuga
?>
输出:
5