KunKun的征途

明天的明天,你还会送我水晶之恋吗?

导航

[ZOJ 1004] Anagrams by Stack (简单搜索)

Posted on 2014-11-06 01:06  西域小车  阅读(153)  评论(0)    收藏  举报

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004

题目大意:给你个栈,给你源串和目标串,按字典序输出符合要求的进站出站序列。

 

就是搜搜搜呗。。。

带上答案和模拟的栈。。

 

代码:

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <string>
 4 #include <iostream>
 5 #include <cstring>
 6 #include <algorithm>
 7 #include <cctype>
 8 #include <vector>
 9 #include <map>
10 #include <set>
11 #include <iterator>
12 #include <functional>
13 #include <cmath>
14 #include <numeric>
15 using namespace std;
16 
17 typedef long long LL;
18 
19 char s[1000],t[1000];
20 
21 void dfs(int now,int pp,int len,string ns,string st){
22     if( pp==len ){
23         for(int i=0;i<st.size();i++){
24             putchar(st[i]);
25             putchar(' ');
26             if( i==st.size()-1 ) puts("");
27         }
28         return;
29     }
30     if( st.size()==len+len ) return;
31     dfs(now+1,pp,len,ns+s[now],st+"i");
32     if( !ns.empty() && ns[ns.size()-1] == t[pp] ){
33         ns.erase(--ns.end());
34         dfs(now,pp+1,len,ns,st+"o");
35     }
36 }
37 
38 int main(){
39     while(scanf("%s%s",s,t)!=EOF){
40         puts("[");
41         int len = strlen(s);
42         string aa,bb;
43         dfs(0,0,len,aa,bb);
44         puts("]");
45     }
46     return 0;
47 }