poj 1270(toposort)
http://poj.org/problem?id=1270
题意:给一个字符串,然后再给你一些规则,要你把所有的情况都按照字典序进行输出。
思路:很明显这肯定要用到拓扑排序,当然看到discuss里面有些人有bfs也可以做,有时候觉得搜索只要剪枝剪的好,啥都可以用搜索。
因为我也不是很会拓扑排序,所以在找这类的题来练习,增加对其的理解。我就发现了一个问题,为什么拓扑排序要构图?
其实也很简单,因为拓扑排序是对一个有向的无环图进行排序,有向指的是某个点排序后一定是出现在他的前一个点的后面。这个是一定的,所以要构图。
当然,我现在也只是浅显的理解。以后有了更深的理解会在写。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <map>
#define maxn 60 using namespace std; int indegree[ maxn ];
char ans[ maxn ];
int graph[ maxn ][ maxn ],num;
char str[ maxn ]; int cmp(const void *a,const void *b)
{
return (*(char *)a)-(*(char * )b);
} void toposort(int depth) //toposort+递归.
{
if(depth == num )
{
printf("%s\n",ans);
return;
}
for( int i = ; i < num ; i++)
{
if(!indegree[ i ])
{
indegree [ i ] --;
ans[ depth ] = str[ i ];
for ( int j = ; j < num ; j++ )
if( graph [ i ][ j ])
indegree[ j ] --;
toposort(depth+);
indegree [ i ] ++;
for( int j = ; j < num ; j++ )
if(graph[ i ][ j ])
indegree [ j ] ++;
}
}
} int main()
{
// freopen("in.txt","r",stdin);
char inp[ maxn ];
while(gets( inp ))
{
memset( graph , , sizeof( graph ) );
memset( str , , sizeof( str ) );
memset( ans , , sizeof( ans ) );
memset( indegree , , sizeof( indegree ) );
map<char,int >s;
int len = strlen( inp ), k = ;
for( int i = ; i < len ; i++ )
if(inp[ i ] >='a' && inp[ i ] <= 'z')
str[ k++ ] = inp[i];
qsort( str , k , sizeof( str[] ) , cmp );
num = k;
for( int i = ; i < len ; i++ )
s[ str[ i ] ] = i; //对点进行构图一定要在排序之后,不然会wa.
memset( inp , , sizeof( inp ) );
gets( inp );
len = strlen( inp );
for( int i = ; i < len ; i += )
{
graph[ s[ inp[ i ] ] ][ s[ inp[ i + ] ] ] = ;
indegree [ s[ inp[ i + ] ] ] ++;
}
toposort();
memset( inp , ,sizeof( inp ) );
printf("\n");
}
return ;
}
poj 1270(toposort)的更多相关文章
- POJ 1270 Following Orders
Following Orders Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4902 Accepted: 1982 ...
- poj 1270(dfs+拓扑排序)
题目链接:http://poj.org/problem?id=1270 思路:就是一简单的dfs+拓扑排序,然后就是按字典序输出所有的情况. http://paste.ubuntu.com/59872 ...
- POJ 1270 Following Orders 拓扑排序
http://poj.org/problem?id=1270 题目大意: 给你一串序列,然后再给你他们部分的大小,要求你输出他们从小到大的所有排列. 如a b f g 然后 a<b ,b< ...
- poj 1270 Following Orders (拓扑排序+回溯)
Following Orders Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5473 Accepted: 2239 ...
- POJ 1270 Following Orders (拓扑排序,dfs枚举)
题意:每组数据给出两行,第一行给出变量,第二行给出约束关系,每个约束包含两个变量x,y,表示x<y. 要求:当x<y时,x排在y前面.让你输出所有满足该约束的有序集. 思路:用拓扑排 ...
- POJ 1270 Following Orders(拓扑排序)题解
Description Order is an important concept in mathematics and in computer science. For example, Zorn' ...
- POJ 1270
#include<iostream> #include<algorithm> #define MAXN 26 #define MAX 300 using namespace s ...
- POJ 1270 Following Orders(拓扑排序)
题意: 给两行字符串,第一行为一组变量,第二行时一组约束(每个约束包含两个变量,x y 表示 x <y).输出满足约束的所有字符串序列. 思路:拓扑排序 + 深度优先搜索(DFS算法) 课本代码 ...
- Day4 - H - Following Orders POJ - 1270
Order is an important concept in mathematics and in computer science. For example, Zorn's Lemma stat ...
随机推荐
- Yeelink 初探
Yeelink可以作为中转服务器使用,在自己没有服务器的情况下,可以利用它传输自己的数据. 首先去申请一个帐号,然后添加一个设备. http://www.yeelink.net/user 在这里是用户 ...
- poj1986 LCA
Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 11759 Accepted: 4157 ...
- 高级数组-ArrayList
可以放入任意类型的数据 ArrayList alist=new ArrayList(); alist.Add(440;//装箱,讲int类型的值转换为引用类型 int i1=(int)alist[0] ...
- thinkphp __PUBLIC__的定义 __ROOT__等常量的定义
2 3 4 5 6 7 8 9 '__TMPL__' => APP_TMPL_PATH, // 项目模板目录 '__ROOT__' => __ROOT__, ...
- MFC用户自定义消息
之前做过佳能相机和位移平台的额二次开发,当时遇到一个棘手的问题,就是位移平台如何知道相机已经拍完照了,或者相机如何知道位移平台已经运行到指定位置,当时为了方便采用了定时器来定时检测位移平台的位置,结果 ...
- #MySQL 5.7.8 支持Json类型
As of MySQL 5.7.8, MySQL supports a native JSON data type that enables efficient access to data in J ...
- python标准模块(三)
本文会涉及到的模块: subprocess logging 1. subprocess 可以执行shell命令的相关模块和函数有: os.system os.spawn os.popen --废弃 p ...
- BZOJ3331: [BeiJing2013]压力
传送门 Tarjan的三大应用之一:求解点双联通分量. 求解点双联通分量.然后缩点,差分优化即可. //BZOJ 3331 //by Cydiater //2016.10.29 #include &l ...
- Hibernate的关联映射关系
一:多对一 <many-to-one 1.name:当前类的属性名(关联映射的类) 2.column:属性多对应的类的对应的表的外键(连接条件) 3.class:属性所对应的类的权限定名 4.n ...
- Samba服务器配置
Samba服务器配置流程: (1)安装samba服务器先用#rpm -ivh samba-列出与samba有关的rpm包然后选择第一个包,用tab键补齐文件名 (2)创建新用户和其密码#useradd ...