您的位置:首页 > 其它

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

2014-11-06 01:06 441 查看
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004

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

就是搜搜搜呗。。。

带上答案和模拟的栈。。

代码:

#include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <vector>
#include <map>
#include <set>
#include <iterator>
#include <functional>
#include <cmath>
#include <numeric>
using namespace std;

typedef long long LL;

char s[1000],t[1000];

void dfs(int now,int pp,int len,string ns,string st){
if( pp==len ){
for(int i=0;i<st.size();i++){
putchar(st[i]);
putchar(' ');
if( i==st.size()-1 ) puts("");
}
return;
}
if( st.size()==len+len ) return;
dfs(now+1,pp,len,ns+s[now],st+"i");
if( !ns.empty() && ns[ns.size()-1] == t[pp] ){
ns.erase(--ns.end());
dfs(now,pp+1,len,ns,st+"o");
}
}

int main(){
while(scanf("%s%s",s,t)!=EOF){
puts("[");
int len = strlen(s);
string aa,bb;
dfs(0,0,len,aa,bb);
puts("]");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: