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)的更多相关文章

  1. POJ 1270 Following Orders

    Following Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4902   Accepted: 1982 ...

  2. poj 1270(dfs+拓扑排序)

    题目链接:http://poj.org/problem?id=1270 思路:就是一简单的dfs+拓扑排序,然后就是按字典序输出所有的情况. http://paste.ubuntu.com/59872 ...

  3. POJ 1270 Following Orders 拓扑排序

    http://poj.org/problem?id=1270 题目大意: 给你一串序列,然后再给你他们部分的大小,要求你输出他们从小到大的所有排列. 如a b f g 然后 a<b ,b< ...

  4. poj 1270 Following Orders (拓扑排序+回溯)

    Following Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5473   Accepted: 2239 ...

  5. POJ 1270 Following Orders (拓扑排序,dfs枚举)

    题意:每组数据给出两行,第一行给出变量,第二行给出约束关系,每个约束包含两个变量x,y,表示x<y.    要求:当x<y时,x排在y前面.让你输出所有满足该约束的有序集. 思路:用拓扑排 ...

  6. POJ 1270 Following Orders(拓扑排序)题解

    Description Order is an important concept in mathematics and in computer science. For example, Zorn' ...

  7. POJ 1270

    #include<iostream> #include<algorithm> #define MAXN 26 #define MAX 300 using namespace s ...

  8. POJ 1270 Following Orders(拓扑排序)

    题意: 给两行字符串,第一行为一组变量,第二行时一组约束(每个约束包含两个变量,x y 表示 x <y).输出满足约束的所有字符串序列. 思路:拓扑排序 + 深度优先搜索(DFS算法) 课本代码 ...

  9. Day4 - H - Following Orders POJ - 1270

    Order is an important concept in mathematics and in computer science. For example, Zorn's Lemma stat ...

随机推荐

  1. 【swift学习笔记】三.使用xib自定义UITableViewCell

    使用xib自定义tableviewCell看一下效果图 1.自定义列 新建一个xib文件 carTblCell,拖放一个UITableViewCell,再拖放一个图片和一个文本框到tableviewc ...

  2. C#软件设计——小话设计模式原则之:单一职责原则SRP

    前言:上篇C#软件设计——小话设计模式原则之:依赖倒置原则DIP简单介绍了下依赖倒置的由来以及使用,中间插了两篇WebApi的文章,这篇还是回归正题,继续来写写设计模式另一个重要的原则:单一职责原则. ...

  3. 【JavaScript】操作Canvas画图

    1.页面添加 Canvas 标签 标签内可以写文字,浏览器不支持Canvas的情况下显示, 2.js获取 Canvas 标签 3.利用js函数画图,[线][图][文字] 源:http://www.li ...

  4. Chrome-Console( Command Line API Reference)

    来源于:https://developers.google.com/web/tools/chrome-devtools/console/command-line-reference The Comma ...

  5. 基于Emgu CV的人脸检测代码

    这个提供的代码例子是Emgu CV提供的源码里面自带的例子,很好用,基本不需要改,代码做的是人脸检测不是人脸识别,这个要分清楚.再就是新版本的Emgu CV可能会遇到系统32位和64位处理方式有区别的 ...

  6. bzoj4199:NOI2015D2T2品酒大会(SAM版)

    SAM感觉写起来比SA更直观(?) #include <iostream> #include <cstdio> #include <cstring> #includ ...

  7. iOS获取本机IP地址

    #import <ifaddrs.h> #import <arpa/inet.h> // Get IP Address - (NSString *)getIPAddress { ...

  8. 鼠标拖动在picturebox上画圆时

    注意MouseDown MouseMove  MouseUp三个事件: MouseMove事件中要实现实时绘制和下次绘制时自动清除重绘 需要: pictureBox1.Invalidate(); pi ...

  9. linux下压缩,解压缩的方法

    linux zip命令 zip -r myfile.zip ./* 将当前目录下的所有文件和文件夹全部压缩成myfile.zip文件,-r表示递归压缩子目录下所有文件. 2.unzip unzip - ...

  10. G-FAQ – Why is Bit Depth Important?

    直接抄: https://apollomapping.com/2012/August/article15.html For this month’s Geospatial Frequently Ask ...