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操作数 ...
随机推荐
- MediaInfo源代码分析 4:Inform()函数
我们来看一下MediaInfo中的Inform()函数的内部调用过程 首先Inform()函数封装了MediaInfo_Internal类中的Inform()函数 //返回文件信息 String Me ...
- redundant 行记录格式
CREATE TABLE `mytest2` ( `t1` varchar() DEFAULT NULL, `t2` varchar() DEFAULT NULL, `t3` ) DEFAULT NU ...
- myeclipse一些技巧
ctrl+h-----------------查找字符串 ctrl+A 全选→ctrl+shift+f 代码格式化,排版 ctrl+shift+O 自动引用
- Java基础知识强化14:Java死亡竞赛题目解析
一个小型网站上发布了一个称为Java“死亡竞赛”的新项目.测验发布后,超过20000位开发者参加了测验.网站以20道关于Java的多选题为主.我们得到了众多开发者的测验统计数据,今天,我们非常乐意 ...
- 第二篇:python基础之文件读写
python基础之文件读写 python基础之文件读写 本节内容 os模块中文件以及目录的一些方法 文件的操作 目录的操作 1.os模块中文件以及目录的一些方法 python操作文件以及目录可以使 ...
- 11.1 afternoon
幸运数字(number)Time Limit:1000ms Memory Limit:64MB题目描述LYK 最近运气很差,例如在 NOIP 初赛中仅仅考了 90 分,刚刚卡进复赛,于是它决定使用一些 ...
- RPM工具
一.RMP工具管理RPM包 rpm命令是RPM软件包的管理工具.rpm原本是Red Hat Linux发行版专门用来管理Linux各项套件的程序,由于它遵循GPL规则且功能强大方便,因而广受欢迎.逐渐 ...
- 重新看php数组
闲来有空,最近看php手册数组这块,对于array_values() 还是第一次接触,array_values是不保留键名,只有键值的函数,还有一个作用就是 重新索引. unset() 函数,是删除 ...
- IDEA SVN1.8 问题解决
转自 http://blog.jetbrains.com/idea/2013/12/subversion-1-8-and-intellij-idea-13/
- CentOS 7重装mysql编译过程报错解决方法
错误记录: [ 82%] Building C object libmysql/CMakeFiles/clientlib.dir/__/sql-common/client.c.o/usr/local/ ...