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 ...
随机推荐
- Lucene 简单手记http://www.cnblogs.com/hoojo/archive/2012/09/05/2671678.html
什么是全文检索与全文检索系统? 全文检索是指计算机索引程序通过扫描文章中的每一个词,对每一个词建立一个索引,指明该词在文章中出现的次数和位置,当用户查询时,检索程序就根据事先建立的索引进行查找,并将查 ...
- 如何用好 Google 搜索引擎?
1.双引号 把搜索词放在双引号中,代表完全匹配搜索,也就是说搜索结果返回的页面包含双引号中出现的所有的词,连顺序也必须完全匹配.bd和Google 都支持这个指令.例如搜索: “seo方法图片” 2. ...
- android 多个shortCut快捷方式实现以及对58同城快捷方式的实现思路的研究
这几天,项目中有个新需求,需要按照模块添加不同的快捷方式到桌面上,从而方便用户的使用.特意进行了研究并分析了下58上面桌面快捷方式的实现. 首先多个shortcut的实现: <activity ...
- HDU 1814 Peaceful Commission
2-SAT,输出字典序最小的解,白书模板. //TwoSAT输出字典序最小的解的模板 //注意:0,1是一组,1,2是一组..... #include<cstdio> #include&l ...
- ubuntu ssh重启
SSH分客户端openssh-client和openssh-server 如果你只是想登陆别的机器的SSH只需要安装openssh-client(ubuntu有默认安装,如果没有则sudo apt-g ...
- cakephp , the subquery (2)
Cakephp 框架帮我们做了很多的工作,的确省了我们很多工作,提高了效率. 但是,碰到一些比较复杂的查询时,还是有些问题,官方的cookbook api 有说明一些详细的用法,但感觉还是不太够,有些 ...
- gnu make
http://stackoverflow.com/questions/448910/makefile-variable-assignment 更加全面的介绍 http://blog.csdn.net/ ...
- zencart 具体页面调用规则: $body_code变量解析
zencart $body_code变量解析 修改centerColumn 可以修改中间产品方框的大小 2.2.5 .BODY文件在这个文件生效 require($body_code) include ...
- Htpasswd 给网站后台目录加密
http://www.l0phtcrack.com/download.html打开apache配置文件 httpd.conf 配置如下: <Directory "/var/www/ ...
- 关于 Equal Override Overload 和 IEquatable
namespace TestEqual { class Program { static void Main(string[] args) { Point2D a = new Point2D { X ...