Following Orders
uva124:http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=60
题意: 第一行给出字符串(小写字母),表示出现的字符类型第二行是约束关系,a1 b1 a2 b2 a3 b3.....ai bi,表示ai必须在bi前面按照字典序输出所有满足约束条件的序列
题解:题解:由题意会想到用拓扑排序,但是题目要求是字典序输出,所以可以用dfs来处理。
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<iostream>
#include<vector>
#include<iterator>
#include<string>
using namespace std;
char map[];//储存原图
int g[][];//topsort的图
bool visit[];//标记已经出现的字母
int counts[];//记录出现过字母的入度
int num;//出现的字母的个数
vector<char> ans;//储存字母序列
void init(){//初始化
num=;
memset(g,,sizeof(g));
memset(visit,,sizeof(visit));
memset(counts,,sizeof(counts));
memset(map,,sizeof());
}
void dfs(int depth,int count){
if(depth>=count){//如果当前的字母个数已经达到num,则可以输出然后返回。
copy(ans.begin(), ans.end(), ostream_iterator<char>(cout));
cout<<endl;
return;
}
for(int i=;i<;i++){//否则查找下一个字母
if(visit[i]){//如果该字母已经出现过则,进入
if(counts[i]==){
counts[i]=-;
ans.push_back(i+'a'-);//把当前点加入结果中
for(int j=;j<;j++){//把与此点相关的边删去
if(g[i][j]==){
counts[j]--;
}
}
dfs(depth+,count);//递归往下
ans.pop_back();//记得恢复现场1,结果集的删除以及入度的增加
counts[i]=;
for(int k=;k<;k++){
if(g[i][k]==){
counts[k]++;
}
}
}
}
}
} int main(){
string line ;
int aa=;
while(getline(cin,line)){//读取一行
init();
int len=line.length();
for(int i=;i<len;i+=){//标记出现过的字母以及个数
visit[line[i]-'a'+]=true;
num++;}
getline(cin,line);//读取第二行
int len1=line.length();
for(int i=;i<len1;i+=){//建图
g[line[i]-'a'+][line[i+]-'a'+]=;
counts[line[i+]-'a'+]++;
}
if(aa)
printf("\n");//注意这里的处理,如果是在poj上,你在每次结果后面直接打空行可以
dfs(,num);//但是如果在uva上,就会wa,因为在uva上的判断是两组之间打空行,多余空行算错
aa=; } }
Following Orders的更多相关文章
- POJ1270 Following Orders[拓扑排序所有方案 Kahn]
Following Orders Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4885 Accepted: 1973 ...
- Cause: org.apache.ibatis.reflection.ReflectionException: Could not set property 'orderdetails' of 'class com.luchao.mybatis.first.po.Orders' with value 'Orderdetail [id=null, ordersId=3, itemsId=1, it
从上面异常的解释来看是因为反射不能将Orders设置到orderdetails属性上,仔细检查了MyBatis的配置文件,发现: <collection property="order ...
- poj 1731 Orders
http://poj.org/problem?id=1731 Orders Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9 ...
- 8.1:SportsStore:Orders and Administration
本章,作者将通过收集和验证购物明细,来完成SportsStore应用,并在Deployd服务器上存储该订单.作者也构建了一个管理应用,允许认证用户查看订单,和管理产品分类. 1.准备实例项目 2.获取 ...
- POJ1270 Following Orders (拓扑排序)
Following Orders Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4254 Accepted: 1709 ...
- 生成订单:三个表(Products,Orders,OrderItem)
1.有三个表(Product上,Orders,OrderItem) 分别创建对应的三个实体类 OrderItem中有外键Order_id 参考Orders中的id :Product_id参考Produ ...
- POJ 1270 Following Orders
Following Orders Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4902 Accepted: 1982 ...
- How to Manage Amazon-Fulfilled Orders - Cancel an Amazon-Fulfilled Order
You may request to cancel customer orders that have a status of "Pending" or "Unshipp ...
- Orders
The stores manager has sorted all kinds of goods in an alphabetical order of their labels. All the k ...
- 错误代码: 1142 REFERENCES command denied to user 'wuyong'@'localhost' for table 'orders'
错误代码: 1142 REFERENCES command denied to user 'wuyong'@'localhost' for table 'orders' 原因:在使用SQLyog操作数 ...
随机推荐
- myeclipse6.0下载及注冊码
myeclipse6.0 下载地址.官方下载地址: http://www.myeclipseide.com/module-htmlpages-display-pid-4.html 本地快速下载地址: ...
- Delphi中一些常用的组合键值
Delphi中一些常用的组合键值 CTRL+A: #1 CTRL+B: #2 CTRL+C: #3 CTRL+D: #4 CTRL+E: #5 CTRL+F: #6 CTRL+G: #7 ...
- MyEclipse_6.0.1GA_E3.3.1集成版下载地址
因在开发中经常使用到myeclipse 对比相关版本,还是觉得6.0 –6.5 比较适合开发,其他的开发起来比较卡,下面是下载地址 MyEclipse_6.0.1GA_E3.3.1集成版下载地址: ...
- 全面分析 Spring 的编程式事务管理及声明式事务管理--转
开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...
- c++中返回对象与返回引用的区别
这几天在做用C++做课程设计,对其返回对象的实现感到迷惑. 通过对汇编代码的分析,可以清楚的看到,直接返回引用和返回对象的区别到底是什么. 分析的程序如下 #include<cstdio> ...
- 最近的两个小项目,2:Python webapp的docker镜像
时间过得真快,一眨眼一个多月没更新了,但这一个月我可没偷懒啊,真的是忙.粘上两篇ReadMe勉强凑合一下,保持博客更新是好习惯. 基于Flask框架,uwsgi起服务,supervisor做管理,应该 ...
- ubuntu下git clone 出现Permission denied (publickey).
今天在ubuntu上使用git 克隆 github上面的库,一直权限拒绝Permission denied (publickey). 公钥绑了好几次,都不行: 最后怀疑是git配置公钥地址有问题:打开 ...
- J2EE 读取文件路径
在J2ee中实现java类读取webcontent/web-inf/config.xml的实现代码 ,其中../config.xml相对于classes的路径 java.net.URL url = t ...
- php 两个数组是否相同,并且输出全面的数据,相同的加一个字段标示
方法一: $date是数组,数组中有字段id,name; $data1是数组,数组中有字段sort_id,name; 所以要通过$date[$i]['id']==$data1[$j]['sort_id ...
- java开发webservice的几种方式(转载)
webservice的应用已经越来越广泛了,下面介绍几种在Java体系中开发webservice的方式,相当于做个记录. 1.Axis2方式 Axis是apache下一个开源的webservice开发 ...