def dst(push, pop, olst, dlst, s, t):
#global result
l = len(olst)
if push==l and pop==l:
print t
return 0
if len(s)==0:
s.append(olst[push])
push = push+1
t.append('i')
dst(push, pop, olst, dlst, s, t)
else:
temp = s.pop()
if temp==dlst[pop]:
t.append('o')
pop = pop+1
dst(push, pop, olst, dlst, s, t)
else:
s.append(temp)
s.append(olst[push])
push = push+1
t.append('i')
dst(push, pop, olst, dlst, s, t)
def Compare(olst, dlst):
l = len(olst)
s = []
t = []
push = 0
pop = 0
#print '[\n'
dst(push, pop, olst, dlst, s, t)
#print ']\n'
org = raw_input()
dis = raw_input()
olst = []
dlst = []
for i in org:
olst.append(i)
for j in dis:
dlst.append(j)
while org or dis:
ol = len(olst)
dl = len(dlst)
if ol!=dl:
print '[\n]\n'
else:
Compare(olst, dlst)
org = raw_input()
dis = raw_input()
olst = []
dlst = []
for i in org:
olst.append(i)
for j in dis:
dlst.append(j)
摘抄:
c++代码:
#include <iostream>
#include <vector>
#include <stack>
#include <string>
using namespace std;
string s1,s2;
stack<char> cs;
vector<char> io;
int l;//当前案例,字符串的长度
void Prints()
{
for(int i=0;i<io.size();i++)
cout<<io[i]<<" ";
cout<<endl;
}
//in表示入栈数量,out表示出栈数量
void dfs(int in,int out)
{
char t;
if( in==l && out==l )//如果入栈和出栈数量都等于字符串长度,则表示已得到一个成功解
{
Prints();
return;
}
if( in<l )
{
cs.push(s1[in]);
io.push_back('i');
dfs(in+1,out);
cs.pop();
io.pop_back();
}
if( out<in && out <l && cs.top()==s2[out] )
{
t = cs.top();
cs.pop();
io.push_back('o');
dfs(in,out+1);
cs.push(t);
io.pop_back();
}
}
int main()
{
while(cin>>s1>>s2)
{
l=s1.length();
cout<<"["<<endl;
dfs(0,0);
cout<<"]"<<endl;
}
return 0;
}