
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to find minimal sum of all MEX of substrings
Suppose we have a binary string S with n bits. Let an operation MEX of a binary string be the smallest digit among 0, 1, or 2 that does not occur in the string. For example, MEX(001011) is 2, because 0 and 1 occur in the string at least once, MEX(1111) is 0, because 0 is not present and 0 is minimum. From the given string S. You should cut it into any number of substrings such that each character is in exactly one substring. It is possible to cut the string into a single substring — the whole string. We have to find the minimal sum of MEX of all substring’s pieces can be?
Problem Category
To solve this problem, we need to manipulate strings. Strings in a programming language are a stream of characters that are stored in a particular array-like data type. Several languages specify strings as a specific data type (eg. Java, C++, Python); and several other languages specify strings as a character array (eg. C). Strings are instrumental in programming as they often are the preferred data type in various applications and are used as the datatype for input and output. There are various string operations, such as string searching, substring generation, string stripping operations, string translation operations, string replacement operations, string reverse operations, and much more. Check out the links below to understand how strings can be used in C/C++.
https://www.tutorialspoint.com/cplusplus/cpp_strings.htm
https://www.tutorialspoint.com/cprogramming/c_strings.htm
So, if the input of our problem is like S = "01", then the output will be 1, because MEX(0) is 1, MEX(1) is 0, so sum is 1 + 0 = 1.
Steps
To solve this, we will follow these steps −
count := (1 if S[0] is 0, otherwise 0) for initialize i := 1, when S[i] is non-zero, update (increase i by 1), do: if S[i] is same as '0' and S[i] is not equal to S[i - 1], then: (increase count by 1) return minimum of count and 2
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(string S){ int count = (S[0] == '0'); for (int i = 1; S[i]; i++) if (S[i] == '0' && S[i] != S[i - 1]) count++; return min(count, 2); } int main(){ string S = "01"; cout << solve(S) << endl; }
Input
"01"
Output
1
- Related Articles
- Program to find sum of beauty of all substrings in Python
- C++ code to find two substrings with one minimal substring
- Program to find total sum of all substrings of a number given as string in Python
- C# Program to find all substrings in a string
- Program to print all substrings of a given string in C++
- Program to find sum of medians of all odd length sublists in C++
- C++ program to find sum of all cells lighten by the lamps
- Program to find out the substrings of given strings at given positions in a set of all possible substrings in python
- Program to find sum of the sum of all contiguous sublists in Python
- C# Program to Read a String and Find the Sum of all Digits
- Find sum of sum of all sub-sequences in C++
- Find all substrings in a string using C#
- Program to find sum of all elements of a tree in Python
- Program to find sum of all odd length subarrays in Python
- Program to Find Out the Minimal Submatrices in Python
