首页 > 代码库 > poj1270Following Orders(拓扑排序+dfs回溯)
poj1270Following Orders(拓扑排序+dfs回溯)
题目链接:
啊哈哈,点我点我
题意是:
第一列给出所有的字母数,第二列给出一些先后顺序。然后按字典序最小的方式输出所有的可能性。。。
思路:
总体来说是拓扑排序,但是又很多细节要考虑,首先要按字典序最小的方式输出,所以自然输入后要对这些字母进行排列,然后就是输入了,用scanf不能读空格,所以怎么建图呢??设置一个变量判断读入的先后顺序,那么建图完毕后,就拓扑排序了,那么多种方式自然就是dfs回溯了。。那么这个问题就得到了解决。。
题目:
Following Orders
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 3800 | Accepted: 1502 |
Description
Order is an important concept in mathematics and in computer science. For example, Zorn‘s Lemma states: ``a partially ordered set in which every chain has an upper bound contains a maximal element.‘‘ Order is also important in reasoning about the fix-point semantics of programs.
This problem involves neither Zorn‘s Lemma nor fix-point semantics, but does involve order.
Given a list of variable constraints of the form x < y, you are to write a program that prints all orderings of the variables that are consistent with the constraints.
For example, given the constraints x < y and x < z there are two orderings of the variables x, y, and z that are consistent with these constraints: x y z and x z y.
This problem involves neither Zorn‘s Lemma nor fix-point semantics, but does involve order.
Given a list of variable constraints of the form x < y, you are to write a program that prints all orderings of the variables that are consistent with the constraints.
For example, given the constraints x < y and x < z there are two orderings of the variables x, y, and z that are consistent with these constraints: x y z and x z y.
Input
The input consists of a sequence of constraint specifications. A specification consists of two lines: a list of variables on one line followed by a list of contraints on the next line. A constraint is given by a pair of variables, where x y indicates that x < y.
All variables are single character, lower-case letters. There will be at least two variables, and no more than 20 variables in a specification. There will be at least one constraint, and no more than 50 constraints in a specification. There will be at least one, and no more than 300 orderings consistent with the contraints in a specification.
Input is terminated by end-of-file.
All variables are single character, lower-case letters. There will be at least two variables, and no more than 20 variables in a specification. There will be at least one constraint, and no more than 50 constraints in a specification. There will be at least one, and no more than 300 orderings consistent with the contraints in a specification.
Input is terminated by end-of-file.
Output
For each constraint specification, all orderings consistent with the constraints should be printed. Orderings are printed in lexicographical (alphabetical) order, one per line.
Output for different constraint specifications is separated by a blank line.
Output for different constraint specifications is separated by a blank line.
Sample Input
a b f g a b b f v w x y z v y x v z v w v
Sample Output
abfg abgf agbf gabf wxzvy wzxvy xwzvy xzwvy zwxvy zxwvy
Source
Duke Internet Programming Contest 1993,uva 124
代码为:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<map>
#include<cstring>
using namespace std;
const int maxn=26+2;
char str[maxn],ans[maxn],apa[maxn];
int in[maxn],gra[maxn][maxn],res;
map<char,int>mp;
void topo(int depth)
{
if(depth==res)
{
printf("%s\n",ans);
return;
}
for(int i=0;i<res;i++)
{
if(in[i]==0)
{
--in[i];
ans[depth]=apa[i];
for(int j=0;j<res;j++)
{
if(gra[i][j])
--in[j];
}
topo(depth+1);
++in[i];
for(int j=0;j<res;j++)
{
if(gra[i][j])
++in[j];
}
}
}
}
int main()
{
int flag,k,len;
char temp1,temp2;
while(gets(str))
{
k=0;
memset(ans,0,sizeof(ans));
memset(in,0,sizeof(in));
memset(gra,0,sizeof(gra));
len=strlen(str);
for(int i=0;i<len;i++)
if(str[i]>='a'&&str[i]<='z')
apa[k++]=str[i];
sort(apa,apa+k);
for(int i=0;i<k;i++)
mp[apa[i]]=i;
res=k;
gets(str);
len=strlen(str);
for(int i=0;i<len;i++)
if(str[i]>='a'&&str[i]<='z')
{
if(flag)
{
temp1=str[i];
flag=0;
}
else
{
temp2=str[i];
gra[mp[temp1]][mp[temp2]]=1;
in[mp[temp2]]++;
flag=1;
}
}
topo(0);
printf("\n");
}
return 0;
}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。