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

    附件 http://download.csdn.net/detail/teststudio/6575241 主窗体UNIT unit MainForm; interface uses Windows, ...

  2. Codeforces Beta Round #10 D. LCIS(DP&amp;LCIS)

    D. LCIS time limit per test 1 second memory limit per test 256 megabytes input standard input output ...

  3. IOS—通过ChildViewController实现view的切换

    IOS-通过ChildViewController实现view的切换 在以前,一个UIViewController的View可能有很多小的子view.这些子view很多时候被盖在最后,我们在最外层Vi ...

  4. SA密钥长度、明文长度和密文长度

    本文介绍RSA加解密中必须考虑到的密钥长度.明文长度和密文长度问题,对第一次接触RSA的开发人员来说,RSA算是比较复杂的算法,RSA的复杂度是因为数学家把效率和安全也考虑进去的缘故. 本文先只谈密钥 ...

  5. AndroidStudio工程文件导入Jar包和So第三方库

    AndroidStudio 导入Jar包和第三方So库 在android开发中,需要导入许多第三方的jar包和so库来支持,包括像许多第三方的支持平台--友盟,环信.融云.极光推送.微博.腾讯等第三方 ...

  6. HTTP协议基础与实验

    一. HTTP协议(Hypetext Transfer Protoacal,超文本传输协议) HTTP协议规定了Web基本的运作过程,以及Web服务器之间的通信细节. Http协议采用客户端/服务器端 ...

  7. MySQL存储过程学习笔记

    MySQL在5.0以前并不支持存储过程,这使得MySQL在应用上大打折扣.MySQL 5.0终于开始支持存储过程了. MySQL的关键字大小写通用.该学习笔记对关键字使用大写:变量名,表名使用小写. ...

  8. mvc Action上面加 [HttpPost]

    mvc  Action上面加 [HttpPost]  意思就是这个action只能响应post请求. 如果发get请求这里是没有响应的

  9. ado.net与各种orm操作数据方式的比较

    ADO.NET与ORM的比较(1):ADO.NET实现CRUD http://zhoufoxcn.blog.51cto.com/792419/283952 ADO.NET与ORM的比较(2):NHib ...

  10. PL/SQL Developer简单使用

    表在以下: