时限:1000ms 内存限制:10000K 总时限:3000ms
描述:
通过栈交换字母顺序。给定两个字符串,要求所有的进栈和出栈序列(i表示进栈,o表示出栈),使得字符串2在求得的进出栈序列的操作下,变成字符串1。输出结果需满足字典序。例如TROT 到 TORT:
[
i i i i o o o o
i o i i o o i o
]
输入:
给定两个字符串,第一个字符串是源字符串,第二个字符是目标目标字符串。
输出:
所有的进栈和出栈序列,输出结果需满足字典序
输入样例:
madam
adamm
bahama
bahama
long
short
eric
rice
输出样例:
[
i i i i o o o i o o
i i i i o o o o i o
i i o i o i o i o o
i i o i o i o o i o
]
[
i o i i i o o i i o o o
i o i i i o o o i o i o
i o i o i o i i i o o o
i o i o i o i o i o i o
]
[
]
[
i i o i o i o o
]
code:
#include<stdio.h>
#include<iostream>
#include<stack>
#include<string.h>
using namespace std;
stack<int>s;
char str[100];
char str1[30];
char str2[30];
int num1=0,num2=0;
bool ans[30];
char tmp;
int q=0;
int y;
void search(int n);
void none();
void output();
int testout();
int main()
{
while(scanf("%s",&str1) != EOF)
{
scanf("%s",&str2);
num1 = strlen(str1);
num2 = strlen(str2);
while (!s.empty())
{
s.pop();
}
printf("[\n");
search(0);
printf("]\n");
}
return 0;
}
void search(int n)
{
int i;
if(n == 2*num1)
{
if(testout() == 1)
{
none();
}
}
else
{
for(i=1;i >= 0;i--)
{
ans[n]=i;
search(n+1);
}
}
}
int testout()
{
int i;
int number1=0,number2=0;
for(i=0;i < 2*num1;i++)
{
if(ans[i] == 1)
{
number1=number1+1;
}
else
{
number2=number2+1;
}
}
if(number1 == number2)
{
return 1;
}
else
{
return 0;
}
}
void none()
{
int i,j;
int flag=1;
int a=0,b=0;
for(i=0;i < 2*num1;i++)
{
if(ans[i] == 1)
{
s.push(str1[a]);
a=a+1;
}
else
{
if(a>b)
{
tmp=s.top();
if(int(tmp) != int(str2[b]) )
{
flag=0;
}
b=b+1;
s.pop();
}
else
{
flag=0;
break;
}
}
}
if(flag == 1)
{
output();
}
}
void output()
{
int i,j;
for(i=0;i < 2*num1-1 ;i++)
{
if(ans[i] == 1)
{
printf("i ");
}
else
{
printf("o ");
}
}
if(ans[2*num1-1] == 1)
{
printf("i");
}
else
{
printf("o");
}
printf("\n");
}