请教一个算法,ACM看得很简单的
时间:2010-09-01
来源:互联网
UVA 124同样也是POJ的1270.
我写的算法是
C/C++ code
在百练上是完全可以 通过的,内存也是相当少的,可是在UVA平台上就是通不过,麻烦做过这个 题的人提醒一下 ,我没有考虑到那一个特殊实例
我写的算法是
C/C++ code
#include <iostream> #include <fstream> #include <string> using namespace std; void MapInToMartix(const string &,const string &); void dp(int); void clearVar(void); int UsedAlpha[26]; int Martix[26][26]; int TotalAlpha=0; char OutArr[30]; int Visted[26]; int degree[26]; int main() { //ifstream cin("124.txt"); string str,str1; OutArr[26]=0; OutArr[27]=0; while(1){ if(cin.eof()) break; getline(cin,str); getline(cin,str1);//分别读入两 行 clearVar(); MapInToMartix(str,str1);//字串映射成矩阵 dp(0);//深度查找 cout<<endl; } return 0; } void MapInToMartix(const string &VarStr,const string &constraint) {//varstr是变量的字串,constraint是限制字串 int count; count=VarStr.length(); int i; for(i=0;i<count;i++){ char c=VarStr.at(i); if((c<='z')&&(c>='a')){ UsedAlpha[c-'a']=1;//记录26个字母中哪一个使用了 TotalAlpha++;//记录总变量数 } } count=constraint.length(); bool secondAlpha=false; int b=0; for(i=0;i<count;i++){ char c=constraint.at(i); if((c<='z')&&(c>='a')){ if(secondAlpha){ Martix[b][c-'a']=1;//为1表示从b到c-'a'有通路 degree[c-'a']++;//这个点的入度 } else{ b=c-'a'; } } else{ secondAlpha=!secondAlpha;//第二个字母的标记 } } } void dp(int loc) { if(loc==TotalAlpha){//如果达到所得 字母长度就 输出 cout<<OutArr<<endl; return; } int j; for(int i=0;i<26;i++){ if(UsedAlpha[i]){//判断这个存在变量? if((!Visted[i])&&(degree[i]==0)){//没有访问且度数 为0 OutArr[loc]='a'+i;//加入到输出数组中 Visted[i]=1;//标记 访问 for(j=0;j<26;j++){ if(Martix[i][j]==1){ degree[j]--;//修改所有相关的结点的度数 } } dp(loc+1);//递归 //下面是复原 for(j=0;j<26;j++){ if(Martix[i][j]==1){ degree[j]++; } } Visted[i]=0; OutArr[loc]=0; } } } } void clearVar() { TotalAlpha=0;
在百练上是完全可以 通过的,内存也是相当少的,可是在UVA平台上就是通不过,麻烦做过这个 题的人提醒一下 ,我没有考虑到那一个特殊实例
作者: news080 发布时间: 2010-09-01
Background
背景
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.
但是本问题并不涉及佐恩引理和定点语义这些复杂理论,仅仅是关于有序。
The Problem
问题
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.
给定一组变量和一个约束x < y,你要写一个程序打印出所有由这些变量组成并能满足该约束的有序集。
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.
举例来说,给定约束x < y和x < z,那么x、y和z三个变量就可以组成两个满足该约束的有序集:x y z和 x z y。
The 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 constraints on the next line. A constraint is given by a pair of variables, where x y indicates that x < y.
输入包括一系列的约束定义。每个定义包括两行:第一行是一组变量,第二行是一组约束。每个约束包含两个变量,如x y表示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.
所有变量都是单个小写字母。定义中最少有两个变量,最多不超过20个变量;最少有一个约束,最多不超过50个约束。存在的有序集最少一个,最多不超过300个。
Input is terminated by end-of-file.
输入由EOF表示结束。
The 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.
对于不同约束条件的有序集之间以空行隔开。
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
背景
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.
但是本问题并不涉及佐恩引理和定点语义这些复杂理论,仅仅是关于有序。
The Problem
问题
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.
给定一组变量和一个约束x < y,你要写一个程序打印出所有由这些变量组成并能满足该约束的有序集。
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.
举例来说,给定约束x < y和x < z,那么x、y和z三个变量就可以组成两个满足该约束的有序集:x y z和 x z y。
The 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 constraints on the next line. A constraint is given by a pair of variables, where x y indicates that x < y.
输入包括一系列的约束定义。每个定义包括两行:第一行是一组变量,第二行是一组约束。每个约束包含两个变量,如x y表示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.
所有变量都是单个小写字母。定义中最少有两个变量,最多不超过20个变量;最少有一个约束,最多不超过50个约束。存在的有序集最少一个,最多不超过300个。
Input is terminated by end-of-file.
输入由EOF表示结束。
The 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.
对于不同约束条件的有序集之间以空行隔开。
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
作者: news080 发布时间: 2010-09-01
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28