题意:告诉你两个字符串a和b,要求对a进行栈的操作而产生b串,输出操作的顺序,如果有多组输出就按字典序输出。
Sample Input
madam
adamm
long
short
Sample Output
[
i i i i o o o i o o
i i i i o o o o i o
i i o i o i o i o o
i i o i o i o o i o
]
[
i i o i o i o o
]
dfs一直比较弱,虽然想法有,但是总写不好
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #include<map> 8 #include<stack> 9 using namespace std; 10 #define MOD 1000000007 11 const int INF=0x3f3f3f3f; 12 const double eps=1e-5; 13 #define cl(a) memset(a,0,sizeof(a)) 14 #define ts printf("***** "); 15 const int MAXN=1005; 16 int n,m,tt; 17 stack<int> s; 18 char s1[500],s2[500]; 19 int len1,len2; 20 int num[500]; 21 void dfs(int i,int j,int k) 22 { 23 if(j==len2) 24 { 25 for(int p=0;p<k;p++) 26 { 27 if(num[p]) printf("i "); 28 else printf("o "); 29 } 30 printf(" "); 31 return; 32 } 33 if(i<len1) 34 { 35 s.push(s1[i]); 36 num[k]=1; 37 dfs(i+1,j,k+1); 38 s.pop(); 39 } 40 if(!s.empty()&&s.top()==s2[j]) 41 { 42 char mm=s.top(); 43 s.pop(); 44 num[k]=0; 45 dfs(i,j+1,k+1); 46 s.push(mm); 47 } 48 } 49 int main() 50 { 51 int i,j,k; 52 #ifndef ONLINE_JUDGE 53 freopen("1.in","r",stdin); 54 #endif 55 while(scanf("%s%s",s1,s2)!=EOF) 56 { 57 len1=strlen(s1); 58 len2=strlen(s2); 59 printf("[ "); 60 dfs(0,0,0); 61 printf("] "); 62 } 63 }