题意:

先给你一个字符串,让你给他们排序;

再给你一行,在这一行,每两个就是第一个需要在第二个前面;

思路:

//DFS写多了感觉好有啊,就是排序过程中可能会有多种情况。

//我们考虑一下怎么排好一个位置,这个位置可能有多个东西可以占,那么这些东西对于这个位置都是等价的,

//那么我们可以采用深搜下+回溯来解决。

//其实还是蛮简单的

code:

#include<cstdio>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std; char ans[30];
char ss[30];
char s[100];
int ma[30][30];
int pre[30];
int gg[200];
int num; void tuopu(int u)
{
if(u==num)
{
ans[u]='\0';
printf("%s\n",ans);
return;
}
for(int i=0;i<num;i++)
{
if(pre[i]==0)
{
ans[u]=ss[i];
pre[i]--;
for(int j=0;j<num;j++)
{
if(ma[i][j])
{
pre[j]--;
}
}
tuopu(u+1);
//回溯;
pre[i]++;
for(int j=0;j<num;j++)
{
if(ma[i][j])
{
pre[j]++;
}
}
}
}
} int main()
{
while(gets(s))
{
int L=strlen(s);
num=0;
for(int i=0;i<L;i++)
{
if(s[i]>='a'&&s[i]<='z')
{
ss[num++]=s[i];
}
}
ss[num]='\0';
sort(ss,ss+num); for(int i=0;i<num;i++)
{
gg[ss[i]]=i;
} char s1[110];
gets(s1);
int LL=strlen(s1);
int j1,j2,flag;
flag=0;
memset(pre,0,sizeof(pre));
memset(ma,0,sizeof(ma)); for(int i=0;i<LL;i++)
{
if(s1[i]<'a'||s1[i]>'z')
continue;
if(!flag)
{
j1=s1[i];
flag=1;
}
else
{
j2=s1[i];
ma[gg[j1]][gg[j2]]=1;
pre[gg[j2]]++;
flag=0;
}
}
tuopu(0);
puts("");
}
return 0;
} /*
a b f g
a b b f
v w x y z
v y x v z v w v
*/

POJ1270【拓扑排序+DFS】的更多相关文章

  1. 拓扑排序+DFS(POJ1270)

    [日后练手](非解题) 拓扑排序+DFS(POJ1270) #include<stdio.h> #include<iostream> #include<cstdio> ...

  2. ACM/ICPC 之 拓扑排序+DFS(POJ1128(ZOJ1083)-POJ1270)

    两道经典的同类型拓扑排序+DFS问题,第二题较第一题简单,其中的难点在于字典序输出+建立单向无环图,另外理解题意是最难的难点,没有之一... POJ1128(ZOJ1083)-Frame Stacki ...

  3. 拓扑排序-DFS

    拓扑排序的DFS算法 输入:一个有向图 输出:顶点的拓扑序列 具体流程: (1) 调用DFS算法计算每一个顶点v的遍历完成时间f[v] (2) 当一个顶点完成遍历时,将该顶点放到一个链表的最前面 (3 ...

  4. Ordering Tasks(拓扑排序+dfs)

    Ordering Tasks John has n tasks to do. Unfortunately, the tasks are not independent and the executio ...

  5. HDU 5438 拓扑排序+DFS

    Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Sub ...

  6. POJ1128 Frame Stacking(拓扑排序+dfs)题解

    Description Consider the following 5 picture frames placed on an 9 x 8 array.  ........ ........ ... ...

  7. poj1270Following Orders(拓扑排序+dfs回溯)

    题目链接: 啊哈哈.点我点我 题意是: 第一列给出全部的字母数,第二列给出一些先后顺序. 然后按字典序最小的方式输出全部的可能性.. . 思路: 整体来说是拓扑排序.可是又非常多细节要考虑.首先要按字 ...

  8. Codeforces Round #292 (Div. 2) D. Drazil and Tiles [拓扑排序 dfs]

    传送门 D. Drazil and Tiles time limit per test 2 seconds memory limit per test 256 megabytes Drazil cre ...

  9. 拓扑排序/DFS HDOJ 4324 Triangle LOVE

    题目传送门 题意:判三角恋(三元环).如果A喜欢B,那么B一定不喜欢A,任意两人一定有关系连接 分析:正解应该是拓扑排序判环,如果有环,一定是三元环,证明. DFS:从任意一点开始搜索,搜索过的点标记 ...

  10. CodeForces-1217D (拓扑排序/dfs 判环)

    题意 https://vjudge.net/problem/CodeForces-1217D 请给一个有向图着色,使得没有一个环只有一个颜色,您需要最小化使用颜色的数量. 思路 因为是有向图,每个环两 ...

随机推荐

  1. window服务器开站点(不通用)

    此文章为记录自己的配置流程,其他人不通用 网站服务器:Windows server 2008 R2 (IIS6.1) + Asp.net 数据库服务器:Windows server 2008 R2 + ...

  2. 浅析怎样学好C语言

    今天,我能够自称是一个混IT的人,并能以此谋生,将来大家能一次谋生.都要感谢两个人:克劳德.香农和约翰.冯.诺依曼,是他们发现了全部的数字化信息,不论是一段程序,一封email.一部电影都是用一连串的 ...

  3. JQUERY多选框,单选框,检查选中的值

    var str=""; $(":checkbox:checked").each(function(){ if($(this).attr("checke ...

  4. DesiredSize,RenderSize&& Width ,ActualWidth

    做UI的时候刚入门,很多属性摸不着头脑,需要的功能和属性不能很快联系联想到,所以要慢慢积累UIElement 的DesiredSize 和 RenderSize UIElement 的DesiredS ...

  5. Intel graphics processing units

    http://en.wikipedia.org/wiki/Comparison_of_Intel_graphics_processing_units Comparison of Intel graph ...

  6. vim note (1)

    'vim' go into the vim mode 'i' 'a' 's'    is means insert mode 'v' is means visual mode 'esc' is mea ...

  7. [转载]php中深拷贝浅拷贝

    转自:http://cnn237111.blog.51cto.com/2359144/1283163 PHP中提供了一种对象复制的操作,clone.语法颇为简单: $a = clone $b; 1.浅 ...

  8. php 封装memcache类

    <?php /*  * memcache类   */ class Memcacheds{     //声明静态成员变量     private static $m = null;     pri ...

  9. 嵌入式开发之davinci---8148/8127/8168 中dsp c674的浮点和定点兼容

    c674: 是c67(浮点)+c64(定点) 兼容的 http://processors.wiki.ti.com/index.php/-mv_option_to_use_with_the_C674x ...

  10. 2016/06/13 phpexcel 未完待续

    ①准备工作: 1,php版本不能太低 2,去官网下载PHPExcel插件    http://phpexcel.codeplex.com/ 3,解压后提取classes文件夹到工作目录,并重命名为PH ...