UVa 124 - Following Orders
题目大意:给一个变量列表和变量的大小关系,输出所有的满足约束的序列。
构建为有向图,然后就是拓扑排序,使用回溯输出所有的结果。
#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的更多相关文章
- POJ1270 Following Orders[拓扑排序所有方案 Kahn]
Following Orders Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4885 Accepted: 1973 ...
- POJ 1270 Following Orders
Following Orders Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4902 Accepted: 1982 ...
- poj1270Following Orders(拓扑排序+dfs回溯)
题目链接: 啊哈哈.点我点我 题意是: 第一列给出全部的字母数,第二列给出一些先后顺序. 然后按字典序最小的方式输出全部的可能性.. . 思路: 整体来说是拓扑排序.可是又非常多细节要考虑.首先要按字 ...
- UVA - 1153 Keep the Customer Satisfied(贪心)
UVA - 1153 Keep the Customer Satisfied Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: ...
- UVa 11729 - Commando War(贪心)
"Waiting for orders we held in the wood, word from the front never came By evening the sound of ...
- uva 10192 Vacation(最长公共子)
uva 10192 Vacation The Problem You are planning to take some rest and to go out on vacation, but you ...
- UVa 10012 - How Big Is It? 堆球问题 全排列+坐标模拟 数据
题意:给出几个圆的半径,贴着底下排放在一个长方形里面,求出如何摆放能使长方形底下长度最短. 由于球的个数不会超过8, 所以用全排列一个一个计算底下的长度,然后记录最短就行了. 全排列用next_per ...
- [uva] 10067 - Playing with Wheels
10067 - Playing with Wheels 题目页:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Ite ...
- UVA 1412 Fund Management (预处理+状压dp)
状压dp,每个状态可以表示为一个n元组,且上限为8,可以用一个九进制来表示状态.但是这样做用数组开不下,用map离散会T. 而实际上很多九进制数很多都是用不上的.因此类似uva 1601 Mornin ...
随机推荐
- pat L2-006. 树的遍历
L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...
- cocos2d-x 3.3 显示中文
Resources文件夹下的strings.xml: <dict> <key>targetScore</key> <string>目标分数</st ...
- 转:selenium 并行启动多个浏览器
https://github.com/fool2fish/selenium-doc/blob/master/official-site/selenium-grid.md Selenium Grid 快 ...
- 通过ReconstructMe实现3D扫描
实物3D建模 目前在3D游戏制作过程中,需要专业人士花几天甚至数星期的时间,借助于Autodesk 3ds Max和Maya等昂贵的软件工具制作3D模型.纹理和动画.游戏制作中经常使用一种方法,即设计 ...
- Chapter 1 First Sight——15
The red-haired woman looked up. "Can I help you?" 红头发的女人抬头看了一眼说,有什么我能帮助你的吗? "I'm Isab ...
- hdu_2159_FATE(完全背包)
题目连接:hdu_2159_FATE 题意:完全背包的题意 题解:把杀敌数看成背包的容量,维护一个经验的最大值,我是多开一维来记录最大的忍耐度,当然你也可以直接开一位,并记录忍耐度,最后扫一遍 #in ...
- the.book.of.gimp.pdf文字不显示
逆天了,不是中文也不显示. https://bugs.freedesktop.org/show_bug.cgi?id=70529 说要升级libfreetype,可是已经是wheezy最新了,其他不稳 ...
- 关于NIOS ii烧写的几种方式(转)
源:http://www.cnblogs.com/bingoo/p/3450850.html 1. 方法一:.sof和.elf全部保存在FPGA内,程序加载和运行也是在FPGA内部. 把FPGA的配置 ...
- C++Builder String 转 char* (转)
源:http://blog.csdn.net/bannico/article/details/7577728 使用C++ Builder 处理字符串经常会遇到兼容性问题. 这次要将String 类型 ...
- 用SqlBulkCopy批量插入数据 遇到的错误
在将txt文本格式的数据导入到数据库中时候,使用的是SqlBulkCopy.但是出现了多处错误,在网上查到得资料如下: 错误一:来自数据源的 String 类型的给定值不能转换为指定目标列的类型 nv ...