Given a pattern containing only I's and D's. I for increasing and D for decreasing. Devise an algorithm to print the minimum number following that pattern. Digits from 1-9 and digits can't repeat.
A simple approach using a stack that execute in 0.01s.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
int num=1;
stack<int> st;
for(int i=0;i<=s.size();++i)
{
st.push(num++);//just push and increase value until found 'I'.
if(s[i]=='I' ||s.size()==i)//when found or string size equal to iteration
while(!st.empty())//until empty print and pop. {
{
cout<<st.top();
st.pop();
}
}
}
cout<<endl;
}
}