题目大意:给一个变量列表和变量的大小关系,输出所有的满足约束的序列。

  构建为有向图,然后就是拓扑排序,使用回溯输出所有的结果。

 #include <cstdio>
#include <cstring>
#include <cctype>
#include <map>
#include <algorithm>
#include <vector>
using namespace std;
#define N 26 map<char, int> id;
map<int, char> var;
vector<int> AdjList[N], ans;
int n, indegree[]; void newNode(char c)
{
if (!id.count(c))
{
int t = id.size();
id[c] = t;
var[t] = c;
}
} void dfs(int cur)
{
if (cur == n)
{
for (int i = ; i < n; i++) printf("%c", var[ans[i]]);
printf("\n");
return;
}
for (int i = ; i < n; i++)
if (indegree[i] == )
{
indegree[i] = -;
ans.push_back(i);
vector<int> vt;
for (int j = ; j < AdjList[i].size(); j++)
{
int v = AdjList[i][j];
vt.push_back(v);
indegree[v]--;
}
dfs(cur+);
for (int j = ; j < vt.size(); j++)
indegree[vt[j]]++;
ans.pop_back();
indegree[i] = ;
}
} int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
freopen("out", "w", stdout);
#endif
char str[];
bool first = true;
while (gets(str))
{
int len = strlen(str);
id.clear();
var.clear();
vector<char> varList;
for (int i = ; i < len; i++)
if (islower(str[i]))
varList.push_back(str[i]);
sort(varList.begin(), varList.end());
for (int i = ; i < varList.size(); i++) newNode(varList[i]);
n = id.size();
gets(str);
len = strlen(str);
vector<int> rel;
for (int i = ; i < len; i++)
if (islower(str[i]))
rel.push_back(id[str[i]]);
for (int i = ; i < N; i++) indegree[i] = ;
for (int i = ; i < N; i++) AdjList[i].clear();
for (int i = ; i+ < rel.size(); i += )
{
AdjList[rel[i]].push_back(rel[i+]);
indegree[rel[i+]]++;
}
if (first) first = false;
else printf("\n");
dfs();
}
return ;
}

  第一次忘了给变量列表排序,结果WA了一次...

UVa 124 - Following Orders的更多相关文章

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

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

  2. POJ 1270 Following Orders

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

  3. poj1270Following Orders(拓扑排序+dfs回溯)

    题目链接: 啊哈哈.点我点我 题意是: 第一列给出全部的字母数,第二列给出一些先后顺序. 然后按字典序最小的方式输出全部的可能性.. . 思路: 整体来说是拓扑排序.可是又非常多细节要考虑.首先要按字 ...

  4. UVA - 1153 Keep the Customer Satisfied(贪心)

    UVA - 1153 Keep the Customer Satisfied Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: ...

  5. UVa 11729 - Commando War(贪心)

    "Waiting for orders we held in the wood, word from the front never came By evening the sound of ...

  6. uva 10192 Vacation(最长公共子)

    uva 10192 Vacation The Problem You are planning to take some rest and to go out on vacation, but you ...

  7. UVa 10012 - How Big Is It? 堆球问题 全排列+坐标模拟 数据

    题意:给出几个圆的半径,贴着底下排放在一个长方形里面,求出如何摆放能使长方形底下长度最短. 由于球的个数不会超过8, 所以用全排列一个一个计算底下的长度,然后记录最短就行了. 全排列用next_per ...

  8. [uva] 10067 - Playing with Wheels

    10067 - Playing with Wheels 题目页:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Ite ...

  9. UVA 1412 Fund Management (预处理+状压dp)

    状压dp,每个状态可以表示为一个n元组,且上限为8,可以用一个九进制来表示状态.但是这样做用数组开不下,用map离散会T. 而实际上很多九进制数很多都是用不上的.因此类似uva 1601 Mornin ...

随机推荐

  1. Maven常用插件配置和使用

    主要介绍Maven的几个常见第三方插件(cobertura.findbugs.source.assembly.插件开发)配置和使用,接Maven介绍 maven本质上是一个插件框架,它的所有工作都交给 ...

  2. CF 389 E 贪心(第一次遇到这么水的E)

    http://codeforces.com/contest/389/problem/E 这道题目刚开始想的特别麻烦...但是没想到竟然是贪心 我们只需要知道偶数的时候可以对称取的,然后奇数的时候没次取 ...

  3. Ubuntu将新增磁盘挂载到home下

    home磁盘空间不足,其他闲置硬盘是原来windows的,不能直接使用(磁盘格式及权限等原因),比如编译安卓源码等. 这样的话就需要将新的磁盘格式化成fat32后挂载到/home下的一个目录,这样就可 ...

  4. redmine配置邮件

    作为一个项目管理平台,必须能够通知项目成员有关项目和任务的各种状态变化.这也是一种典型的观察者模式.开发人员可以跟踪某个任务,当任务状态.备注.文档.优先级等变化时开发人员就可以得到通知. 对于新配置 ...

  5. div+css与table布局

    1:速度和加载方式方面的区别 div 的加载方式是即读即加载,遇到 <div> 没有遇到 </div> 的时候一样加载 div 中的内容,读多少加载多少:table 的加载方式 ...

  6. tabBarItem动画

    1.有时,我们需要为tabBarItem设置一些动画.在网上查了半天,没有结果.自己写了一个简单的动画 代码如下: - (void)tabBarController:(UITabBarControll ...

  7. jenkins 进阶系列网址

    http://www.cnblogs.com/zz0412/tag/jenkins/default.html?page=1

  8. [Java]读取文件方法大全(转)

    [Java]读取文件方法大全   1.按字节读取文件内容2.按字符读取文件内容3.按行读取文件内容 4.随机读取文件内容 public class ReadFromFile {     /**     ...

  9. HDU 3743 Frosh Week(归并排序求逆序数)

    归并排序求逆序数 #include <iostream> #include <cstdio> using namespace std; #define maxn 1000005 ...

  10. Using StructureMap DI and Generic Repository

    In this post, i will show how to use generic repository and dependency injection using structuremap. ...