you can submit in here
the problem give us a string A,and many strings B,find a stack push/pop operation sequence satisfy the A transform to B
it’s a search problem and we should record the times of push and pop operation and when we search pop ,the top of the stack should be the matched character .
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <stack>
using namespace std;
string str,tar;
char op[200];
int n;
stack<char> s;
void dfs(int in,int out,int step)
{
//if(pos > str.length()) return ;
if( in == tar.length() && out ==tar.length())
{
for(int i=0;i<step;i++)
{
cout<<op[i]<<" ";
}
cout<<endl;
return;
}
//in
if(in < tar.length() )
{
s.push(str[in]);
op[step] = 'i';
dfs(in+1,out,step+1);
s.pop();
}
//
if(!s.empty() && s.top() == tar[out])
{
op[step] = 'o';
char t = s.top();
s.pop();
dfs(in,out+1,step+1);
s.push(t);
}
}
int main()
{
while(cin>>str>>tar)
{
while(!s.empty())s.pop();
cout<<"["<<endl;
if(str.length() == tar.length())
{
dfs(0,0,0);
}
cout<<"]"<<endl;
}
return 0;
}