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. HDU2255 奔小康赚大钱【二分图最佳匹配】

    题目链接: http://acm.hdu.edu.cn/showproblem.php? pid=2255 题目大意: 村里要分房子. 有N家老百姓,刚好有N间房子.考虑到每家都要有房住,每家必须分配 ...

  2. [转][JAVA]定时任务之-Quartz使用篇

    [BAT][JAVA]定时任务之-Quartz使用篇 定时任务之-Quartz使用篇 Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与 ...

  3. block代码块介绍

    关于block的简单介绍 什么是block? Block是C语言的一个语法特性,同时也是C语言的运行时特性,它很像C中的函数指针,因为你可以像使用函数指针一样的去使用block对象:它也很像C++中的 ...

  4. Android(java)学习笔记199:Android中补间动画(Tween Animation)

    本文主要简单介绍补间动画使用代码实现, 关于使用xml实现补间动画,可以参看:自定义控件三部曲之动画篇(一)——alpha.scale.translate.rotate.set的xml属性及用法 1. ...

  5. CentOS修改主机名hostname

    方法一:即时生效,重启后失效 hostname 新主机名 方法二:永久生效 1.修改/etc/hosts vim /etc/hosts 127.0.0.1 localhost 新主机名 2.修改/et ...

  6. 16、SQL Server 复制及常见错误处理

    SQL Server 复制 复制是一组技术的组合,可以用此组合对数据和数据库对象进行复制由一个数据库移动到另一个数据库. 复制的英文是Replication,重复的意思,而不是Copy.复制的核心功能 ...

  7. fiddler了解

    常常听到有人会所抓包什么的,自己电脑上有一段fiddler软件,但是一直没有使用,因为不了解.今天终于看视频,看博客,大致了解了fiddler这个软件,看着是非常强大啊.那么fiddler到底是什么, ...

  8. 查找被锁对象的名称、sid,锁定的类型-1123

    select lk.sid,lk_obj.object_id,obj.object_name,DECODE(LK.LMODE,0,'None',1,'Null',2,'Row-S (SS)',3,'R ...

  9. 【模板】【凸包】Graham_scan

    /* 唐代李白 <江夏别宋之悌> 楚水清若空,遥将碧海通.人分千里外,兴在一杯中. 谷鸟吟晴日,江猿啸晚风.平生不下泪,于此泣无穷. */ #include <iostream> ...

  10. OA系统权限管理设计方案学习

    学习之:http://www.cnblogs.com/kivenhou/archive/2009/10/19/1586106.html 此为模型图: 据此写了sql语句: drop table if ...