foj_1532

又是棧的問題

#include <iostream>
#include <stack>
#include <memory.h>
#include <string.h>
using namespace std;

char s1[18],s2[18];
 int step[34]; char  s[34];
int n;
void dfs(int top,int m,int p1,int k){
   int i;
   while(1)
   {
     if(s[top]==s2[k]&&top>-1)
	 {
	  
	   if(s1[p1]!='\0')
	   {	   s[top+1]=s1[p1];
			step[m]=1;
	   }
	  
	   dfs(top+1,m+1,p1+1,k);

	   top--;
	   k++;
	   step[m++]=0;
	 }
	 else
	 {
	  
		 if(s1[p1]!='\0')
		 {	   s[++top]=s1[p1++];
			step[m++]=1;
		 }
		 else
		 { 
		   if(top==-1&&s2[k]=='\0')
		   {  
			   for(i=0;i<m;i++)
			   {   cout<<step[i];
			   }
		      cout<<endl;}
			    return ;
			}
	   
			 }
   
   }

}

int main()
{
  while(~scanf("%s",s1))
  {

     scanf("%s",s2);
	 n=5;
	 memset(step,0,sizeof(step));
	step[0]=1;
	 s[0]=s1[0];
	 if(strlen(s1)!=strlen(s2))continue;
	 dfs(0,1,1,0);

	  
  }
  return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章