C ++程序查找在二进制字符串的任何旋转中连续放置在开头和结尾处的最大0数
给定一个大小为N的二进制字符串S ,任务是使给定字符串S的任何旋转的开始和结束处出现的连续0的计数总和最大化。
例子:
Input: S = “1001”
Output: 2
Explanation:
All possible rotations of the string are:
“1001”: Count of 0s at the start = 0; at the end = 0. Sum= 0 + 0 = 0.
“0011”: Count of 0s at the start = 2; at the end = 0. Sum = 2 + 0=2
“0110”: Count of 0s at the start = 1; at the end = 1. Sum= 1 + 1 = 2.
“1100”: Count of 0s at the start = 0; at the end = 2. Sum = 0 + 2 = 2
Therefore, the maximum sum possible is 2.
Input: S = “01010”
Output: 2
Explanation:
All possible rotations of the string are:
“01010”: Count of 0s at the start = 1; at the end = 1. Sum= 1+1=1
“10100”: Count of 0s at the start = 0; at the end = 2. Sum= 0+2=2
“01001”: Count of 0s at the start = 1; at the end = 0. Sum= 1+0=1
“10010”: Count of 0s at the start = 0; at the end = 1. Sum= 0+1=1
“00101”: Count of 0s at the start = 2; at the end = 0. Sum= 2+0=2
Therefore, the maximum sum possible is 2.
朴素方法:最简单的想法是生成给定字符串的所有旋转,并且对于每个旋转,计算字符串开头和结尾出现的 0 的数量并计算它们的总和。最后,打印得到的最大总和。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum sum of
// consecutive 0s present at the start
// and end of a string present in any
// of the rotations of the given string
void findMaximumZeros(string str, int n)
{
// Check if all the characters
// in the string are 0
int c0 = 0;
// Iterate over characters
// of the string
for (int i = 0; i < n; ++i) {
if (str[i] == '0')
c0++;
}
// If the frequency of '1' is 0
if (c0 == n) {
// Print n as the result
cout << n;
return;
}
// Concatenate the string
// with itself
string s = str + str;
// Stores the required result
int mx = 0;
// Generate all rotations of the string
for (int i = 0; i < n; ++i) {
// Store the number of consecutive 0s
// at the start and end of the string
int cs = 0;
int ce = 0;
// Count 0s present at the start
for (int j = i; j < i + n; ++j) {
if (s[j] == '0')
cs++;
else
break;
}
// Count 0s present at the end
for (int j = i + n - 1; j >= i; --j) {
if (s[j] == '0')
ce++;
else
break;
}
// Calculate the sum
int val = cs + ce;
// Update the overall
// maximum sum
mx = max(val, mx);
}
// Print the result
cout << mx;
}
// Driver Code
int main()
{
// Given string
string s = "1001";
// Store the size of the string
int n = s.size();
findMaximumZeros(s, n);
return 0;
}
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum sum of
// consecutive 0s present at the start
// and end of any rotation of the string str
void findMaximumZeros(string str, int n)
{
// Stores the count of 0s
int c0 = 0;
for (int i = 0; i < n; ++i) {
if (str[i] == '0')
c0++;
}
// If the frequency of '1' is 0
if (c0 == n) {
// Print n as the result
cout << n;
return;
}
// Stores the required sum
int mx = 0;
// Find the maximum consecutive
// length of 0s present in the string
int cnt = 0;
for (int i = 0; i < n; i++) {
if (str[i] == '0')
cnt++;
else {
mx = max(mx, cnt);
cnt = 0;
}
}
// Update the overall maximum sum
mx = max(mx, cnt);
// Find the number of 0s present at
// the start and end of the string
int start = 0, end = n - 1;
cnt = 0;
// Update the count of 0s at the start
while (str[start] != '1' && start < n) {
cnt++;
start++;
}
// Update the count of 0s at the end
while (str[end] != '1' && end >= 0) {
cnt++;
end--;
}
// Update the maximum sum
mx = max(mx, cnt);
// Print the result
cout << mx;
}
// Driver Code
int main()
{
// Given string
string s = "1001";
// Store the size of the string
int n = s.size();
findMaximumZeros(s, n);
return 0;
}
2
时间复杂度: O(N 2 )
辅助空间: O(N)
有效的方法:这个想法是找到给定字符串中连续 0 的最大数量。另外,在字符串的开头和结尾找到连续0的总和,然后打印其中的最大值。
请按照以下步骤解决问题:
- 检查字符串中'1'的频率, S是否等于0 。如果发现为真,则打印N的值作为结果。
- 否则,请执行以下步骤:
- 将给定字符串中连续 0 的最大数量存储在变量中,例如X 。
- 初始化两个变量,从0开始,以N-1结束。
- 当S[start]不等于“ 1 ”时,增加cnt的值并以1开始。
- 当S[end]不等于 ' 1 ' 时,增加cnt的值并减少end 1 。
- 结果打印出X和cnt的最大值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum sum of
// consecutive 0s present at the start
// and end of any rotation of the string str
void findMaximumZeros(string str, int n)
{
// Stores the count of 0s
int c0 = 0;
for (int i = 0; i < n; ++i) {
if (str[i] == '0')
c0++;
}
// If the frequency of '1' is 0
if (c0 == n) {
// Print n as the result
cout << n;
return;
}
// Stores the required sum
int mx = 0;
// Find the maximum consecutive
// length of 0s present in the string
int cnt = 0;
for (int i = 0; i < n; i++) {
if (str[i] == '0')
cnt++;
else {
mx = max(mx, cnt);
cnt = 0;
}
}
// Update the overall maximum sum
mx = max(mx, cnt);
// Find the number of 0s present at
// the start and end of the string
int start = 0, end = n - 1;
cnt = 0;
// Update the count of 0s at the start
while (str[start] != '1' && start < n) {
cnt++;
start++;
}
// Update the count of 0s at the end
while (str[end] != '1' && end >= 0) {
cnt++;
end--;
}
// Update the maximum sum
mx = max(mx, cnt);
// Print the result
cout << mx;
}
// Driver Code
int main()
{
// Given string
string s = "1001";
// Store the size of the string
int n = s.size();
findMaximumZeros(s, n);
return 0;
}
2
时间复杂度: O(N)
辅助空间: O(1)
有关更多详细信息,请参阅有关在二进制字符串的任何旋转中连续放置在开头和结尾的最大 0 数的完整文章!