ZOJ_1004 Anagrams by Stack
2015-10-21 18:45
351 查看
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4
代码:
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#include<cstring>
#include<string>
#include<utility>
#include<stack>
#include<vector>
#include<iostream>
using namespace std;
typedef long long ll;
string a,b;
stack<char> build;
vector<char> operate;
int length;
void dfs(int iPush,int iPop)
{
if(iPush==length && iPop==length)
{
for(int i=0;i<operate.size();i++)
{
cout<<operate[i]<<' '; //结尾会有多余的一个空格
}
cout<<endl;
}
if(iPush+1 <= length)
{
build.push(a[iPush]);
operate.push_back('i');
dfs(iPush+1,iPop);
build.pop();
operate.pop_back();
}
if(iPop+1 <= iPush && iPop+1 <= length && build.top() == b[iPop])
{
char tmp=build.top();
build.pop();
operate.push_back('o');
dfs(iPush,iPop+1);
build.push(tmp);
operate.pop_back();
}
}
int main()
{
while(cin>>a>>b)
{
length=a.length();
cout<<"["<<endl;
dfs(0,0);
cout<<"]"<<endl;
}
return 0;
}
代码:
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#include<cstring>
#include<string>
#include<utility>
#include<stack>
#include<vector>
#include<iostream>
using namespace std;
typedef long long ll;
string a,b;
stack<char> build;
vector<char> operate;
int length;
void dfs(int iPush,int iPop)
{
if(iPush==length && iPop==length)
{
for(int i=0;i<operate.size();i++)
{
cout<<operate[i]<<' '; //结尾会有多余的一个空格
}
cout<<endl;
}
if(iPush+1 <= length)
{
build.push(a[iPush]);
operate.push_back('i');
dfs(iPush+1,iPop);
build.pop();
operate.pop_back();
}
if(iPop+1 <= iPush && iPop+1 <= length && build.top() == b[iPop])
{
char tmp=build.top();
build.pop();
operate.push_back('o');
dfs(iPush,iPop+1);
build.push(tmp);
operate.pop_back();
}
}
int main()
{
while(cin>>a>>b)
{
length=a.length();
cout<<"["<<endl;
dfs(0,0);
cout<<"]"<<endl;
}
return 0;
}
相关文章推荐
- json反序列化
- Java 实现快速排序 递归和非递归
- java程序员菜鸟进阶(十七)linux基础入门(五)linux文件/目录的权限和归属管理
- iphone利用UIPopoverPresentationController实现popover
- Spark如何解决常见的Top N问题
- block的使用
- tMemMonitor (TMM) ----- 100%正确的内存泄漏分析工具
- 微软算法100题05 查找最小的k 个元素
- php面向对象编程_2
- Many2one类型的fields Compute得到的值 搜索
- Linux多线程基础学习(八)私有数据
- MySQL获取汉字的拼音首字母
- 项目3 -- 顺序串算法
- Google服务器架构图解简析
- 自定义控件时,初始化的注意事项
- xen虚拟化实战系列(十二)之xen虚拟机高可用之在线迁移
- Log4j源码阅读之四— 4000 doConfigure方法
- 用Redis存储Tomcat集群的Session
- Logcat过滤不需要的系统标签
- Android 内存管理 &Memory Leak & OOM 分析