帮帮忙啊 算法实验题3.11 字符串变换问题 【用栈做】

来源:百度知道 编辑:UC知道 时间:2024/09/21 13:26:25
算法实验题3.11 字符串变换问题
«问题描述:
给定2 个字符串A 和B,字符串变换问题要求通过栈运算将字符串A 变换为字符串B。
例如,当A=”TROT”,B=”TORT”时,可以通过对字符串A 的字符进行如下栈运算变换为
字符串B:PUSH,PUSH,PUSH,PUSH,POP,POP,POP,POP。如果用1 表示栈运算PUSH,用
0 表示栈运算POP,则上述栈运算序列可表示为:11110000。类似的,栈运算序列10110010
也可将字符串A 变换为字符串B。
«实验任务:
对于给定的2 个字符串A 和B,计算将字符串A变换为字符串B的所有栈运算序列。
«数据输入:
由文件input.txt给出输入数据。文件共2 行,每行给出1 个字符串。
«结果输出:
将计算出的可将字符串A变换为字符串B的所有栈运算序列输出到文件output.txt。每
行输出1 个栈运算序列。如果不存在可将字符串A 变换为字符串B 的栈运算序列,则不输
出。
输入文件示例 输出文件示例
input.txt output.txt
madam
adamm
1111000100
1111000010
1101010100
1101010010

应该就是这道题吧
http://acm.zju.edu.cn/show_problem.php?pid=1004
以下是我的程序
#include <cstdio>
#include <memory>
#include <cstring>
char source[1000],target[1000];
int len;
int count[2][256];

bool operations[1000];
char stack[1000];

void Find(int depth,int push,int pop)
{
if (pop==len)
{
int i;
for (i=0;i<depth;i++)
if (operations[i]) printf("i ");
else printf("o ");
putchar('\n');
}
else
{
if (push<len)
{
operations[depth]=true;
stack[push-pop]=source[push];
Find(depth+1,push+1,pop);
stack[push-pop]='\0';
}
if (pop<push)