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的更多相关文章

  1. POJ1270 Following Orders[拓扑排序所有方案 Kahn]

    Following Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4885   Accepted: 1973 ...

  2. 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 ...

  3. poj 1731 Orders

    http://poj.org/problem?id=1731 Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9 ...

  4. 8.1:SportsStore:Orders and Administration

    本章,作者将通过收集和验证购物明细,来完成SportsStore应用,并在Deployd服务器上存储该订单.作者也构建了一个管理应用,允许认证用户查看订单,和管理产品分类. 1.准备实例项目 2.获取 ...

  5. POJ1270 Following Orders (拓扑排序)

    Following Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4254   Accepted: 1709 ...

  6. 生成订单:三个表(Products,Orders,OrderItem)

    1.有三个表(Product上,Orders,OrderItem) 分别创建对应的三个实体类 OrderItem中有外键Order_id 参考Orders中的id :Product_id参考Produ ...

  7. POJ 1270 Following Orders

    Following Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4902   Accepted: 1982 ...

  8. 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 ...

  9. Orders

    The stores manager has sorted all kinds of goods in an alphabetical order of their labels. All the k ...

  10. 错误代码: 1142 REFERENCES command denied to user 'wuyong'@'localhost' for table 'orders'

    错误代码: 1142 REFERENCES command denied to user 'wuyong'@'localhost' for table 'orders' 原因:在使用SQLyog操作数 ...

随机推荐

  1. Bash简单介绍

    Bash不不过一个命令解析程序. 它本身拥有一种程序设计语言,使用这样的语言,能够编写shell脚本来完毕各种各样的工作.而这些工作是使用现成的命令所无法完毕的.Bash脚本能够使用if-then-e ...

  2. SEO中最常见的几个错误

    昨天答应给放点干活的,今天如约而来! SEO中最常见的几个错误: 1.关键词   人们往往想当然的选择自己喜欢的keyword,但用户在搜索时,根本不会使用它们.比方说,你选择"优化果酱&q ...

  3. virtualbox 虚拟3台虚拟机搭建hadoop集群

    用了这么久的hadoop,只会使用streaming接口跑任务,各种调优还不熟练,自定义inputformat , outputformat, partitioner 还不会写,于是干脆从头开始,自己 ...

  4. 使用r.js进行前端repuirejs的合并压缩

    安装 requirejs npm install -g requirejs 安装好后: 找到刚刚requirejs的安装目录,在该目录下找到r.js,并拷贝待压缩合并项目的根目录下 在项目根目录下创建 ...

  5. Thread和Runnable差别

    继承Thread类的,我们相当于拿出三件事即三个卖票10张的任务分别分给三个窗体,他们各做各的事各卖各的票各完毕各的任务.由于MyThread继承Thread类.所以在new MyThread的时候在 ...

  6. windows 与Linux 互传文件

    下载putty,将putty的安装路径添加到Windows的环境变量中:     我的电脑->属性->高级->环境变量->系统变量,双击其中的Path,在分号后添加putty的 ...

  7. linux与Windows共享文件配置

    linux与Windows共享文件配置: 1.进入超级用户:$su root 2.启动tftp服务器:#netstat -a | grep tftp,出现如图所示的消息表示tftp服务器已安装成功: ...

  8. java验证码Captcha

    import java.awt.Color; import java.io.IOException; import java.util.Random; import javax.servlet.htt ...

  9. 利用html+ashx实现aspx的功能

    最近准备学习下ASP.NET,初期在网上看了些视频教程,准备将自己学习的东西整理整理,留着日后可以参考参考. 本文采用了html.ashx实现aspx,实现了一个最简单的动态网页效果,开发环境是VS2 ...

  10. IIS部署.NET项目的有关事项_2015.07.02

    今天在做项目中的关于发送邮件的一些功能.在部署服务的时候遇到了一些奇葩的问题,基本上是和IIS有关的问题. 首先,项目是基于.NET Framework4.0 版本的,由于本人用的是一台新配置好的机器 ...