现在的位置: 首页 > 综合 > 正文

第一个DFS,第一个递归 HDU1515

2013年08月18日 ⁄ 综合 ⁄ 共 947字 ⁄ 字号 评论关闭

递归真的很强大,对自己还算挺满意的,第一次就写出了不错的成果

#include<stdio.h>
#include<string.h>
void digui(char[],int ,int,int,int);
char s[1000];
char p[1000];
int flag[1000];
int flagtemp;
int sl,pl;
int main()
{
char stack[200];
while(scanf("%s",s)!=EOF)
{
memset(stack,0,sizeof(stack));
flagtemp=1;
flag[0]=0;
scanf("%s",p);
sl=strlen(s);
pl=strlen(p);
stack[0]=s[0];
flag[0]=0;
printf("[\n");
digui(stack,1,0,0,1);
printf("]\n");
}
return 0;
}
void digui(char stack[],int stemp,int ptemp,int top,int flagtemp)
{
char zhong[200];
int i;
if(ptemp==pl)
{
printf("i ");
for(i=1;i<flagtemp;i++)
{
if(!flag[i])
printf("i");
else printf("o");
putchar(' ');
}
printf("\n");
return ;
}
if(stemp==sl+1) 
{
return ;
}
if(top==-1||stack[top]!=p[ptemp])
{
flag[flagtemp]=0;
stack[++top]=s[stemp];
digui(stack,stemp+1,ptemp,top,flagtemp+1);
}
else if(stack[top]==p[ptemp])
{
flag[flagtemp]=0;
stack[++top]=s[stemp];
strcpy(zhong,stack);
digui(stack,stemp+1,ptemp,top,flagtemp+1);

strcpy(stack,zhong);
top--;
flag[flagtemp]=1;
digui(stack,stemp,ptemp+1,top-1,flagtemp+1);
}
}

抱歉!评论已关闭.