text: Form minimum number from given sequence | C ++ program to print minimum number that can be formed from a given sequence of Is and Ds ; Function to decode the given sequence to construct minimum number without repeated digits ; result store output string ; create an empty stack of integers ; run n + 1 times where n is length of input sequence ; push number i + 1 into the stack ; if all characters of the input sequence are processed or current character is ' I ' ( increasing ) ; run till stack is empty ; remove top element from the stack and add it to solution ; main function
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; void PrintMinNumberForPattern ( string seq ) { string result ; stack < int > stk ; for ( int i = 0 ; i <= seq . length ( ) ; i ++ ) { stk . push ( i + 1 ) ; if ( i == seq . length ( ) seq [ i ] == ' I ' ) { while ( ! stk . empty ( ) ) { result += to_string ( stk . top ( ) ) ; result += " ▁ " ; stk . pop ( ) ; } } } cout << result << endl ; } int main ( ) { PrintMinNumberForPattern ( " IDID " ) ; PrintMinNumberForPattern ( " I " ) ; PrintMinNumberForPattern ( " DD " ) ; PrintMinNumberForPattern ( " II " ) ; PrintMinNumberForPattern ( " DIDI " ) ; PrintMinNumberForPattern ( " IIDDD " ) ; PrintMinNumberForPattern ( " DDIDDIID " ) ; return 0 ; }