深搜,有点难
参考:http://zc634579757.blog.163.com/blog/static/1244974622009829263133/
#include "stdio.h"
#include "string.h"
#define M 100
char source[M], target[M];
int len;
char seq[2*M];
int p;
int stack[M];
int top;
void DFS(int ls, int lt){
int i;
char c;
if(lt == len){
for(i=0; i<2*len; i++)
printf("%c ", seq[i]);
printf("\n");
return;
}
if(ls < len){
seq[p++] = 'i';
stack[top++] = source[ls];
DFS(ls+1, lt);
top--;
p--;
}
if(top && stack[top-1] == target[lt]){
seq[p++] = 'o';
c = stack[--top];
DFS(ls, lt+1);
p--;
stack[top++] = c;
}
}
void main(){
freopen("in.txt", "r", stdin);
while(gets(source)){
gets(target);
printf("[\n");
if((len = strlen(source)) == (int)strlen(target)){
p = 0;
top = 0;
DFS(0, 0);
}
printf("]\n");
}
}
madam
adamm
ls指向source串,lt指向target串。建立一个stack,用来模拟进栈出栈。seq用来保存进出记录。
1、当ls指向的字符不等于lt指向的字符(下面直接简写为ls不等于lt)时,ls进栈
2、当ls等于lt时,分两步:将ls后面继续进栈;将ls弹出栈,lt++