因为操作只有2种,每次不是i就是o。
根据这个展开搜索,i要判断是否还有元素可以进栈,o要判断栈顶元素与待比较的元素是否相等!
同时还要用数组记录操作。记住,每次递归都要新开一个栈和记录操作的数组!将传进来的栈和数组复制一遍在使用新开的,不然数据就混乱了...
不要忘记对于递归结束的处理!
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct
{
char str[1000];
int top;
}stack;
typedef struct
{
char r[1000];
int cnt;
}result;
char string[1000];
char str[1000];
void work(char *p,char *q,stack *now,result *t);
int main()
{
while(scanf("%s %s",str,string)!=EOF)
{
stack now;
result temp;
now.top = -1;
temp.cnt = 0;
printf("[\n");
work(str,string,&now,&temp);
printf("]\n");
}
return 0;
}
void work(char *p,char *q,stack *now,result *t)
{
stack st;
result rt;
int i;
st = *now;
rt = *t;
if(*q=='\0')
{
for(i = 0;i < rt.cnt;i++)
printf("%c ",rt.r[i]);
printf("\n");
return;
}
if(*p=='\0')
{
while(st.top>-1&&st.str[st.top]==*q)
{
q++;
st.top--;
rt.r[rt.cnt++] = 'o';
}
if(st.top<0)
{
for(i = 0;i < rt.cnt;i++)
printf("%c ",rt.r[i]);
printf("\n");
}
return;
}
st.str[++st.top] = *p;
rt.r[rt.cnt++] = 'i';
work(p+1,q,&st,&rt);
st.top--;
rt.cnt--;
if(st.top>-1&&st.str[st.top]==*q)
{
st.top--;
rt.r[rt.cnt++] = 'o';
work(p,q+1,&st,&rt);
}
}