POJ1270【拓扑排序+DFS】
题意:
先给你一个字符串,让你给他们排序;
再给你一行,在这一行,每两个就是第一个需要在第二个前面;
思路:
//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】的更多相关文章
- 拓扑排序+DFS(POJ1270)
[日后练手](非解题) 拓扑排序+DFS(POJ1270) #include<stdio.h> #include<iostream> #include<cstdio> ...
- ACM/ICPC 之 拓扑排序+DFS(POJ1128(ZOJ1083)-POJ1270)
两道经典的同类型拓扑排序+DFS问题,第二题较第一题简单,其中的难点在于字典序输出+建立单向无环图,另外理解题意是最难的难点,没有之一... POJ1128(ZOJ1083)-Frame Stacki ...
- 拓扑排序-DFS
拓扑排序的DFS算法 输入:一个有向图 输出:顶点的拓扑序列 具体流程: (1) 调用DFS算法计算每一个顶点v的遍历完成时间f[v] (2) 当一个顶点完成遍历时,将该顶点放到一个链表的最前面 (3 ...
- Ordering Tasks(拓扑排序+dfs)
Ordering Tasks John has n tasks to do. Unfortunately, the tasks are not independent and the executio ...
- HDU 5438 拓扑排序+DFS
Ponds Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Sub ...
- POJ1128 Frame Stacking(拓扑排序+dfs)题解
Description Consider the following 5 picture frames placed on an 9 x 8 array. ........ ........ ... ...
- poj1270Following Orders(拓扑排序+dfs回溯)
题目链接: 啊哈哈.点我点我 题意是: 第一列给出全部的字母数,第二列给出一些先后顺序. 然后按字典序最小的方式输出全部的可能性.. . 思路: 整体来说是拓扑排序.可是又非常多细节要考虑.首先要按字 ...
- 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 ...
- 拓扑排序/DFS HDOJ 4324 Triangle LOVE
题目传送门 题意:判三角恋(三元环).如果A喜欢B,那么B一定不喜欢A,任意两人一定有关系连接 分析:正解应该是拓扑排序判环,如果有环,一定是三元环,证明. DFS:从任意一点开始搜索,搜索过的点标记 ...
- CodeForces-1217D (拓扑排序/dfs 判环)
题意 https://vjudge.net/problem/CodeForces-1217D 请给一个有向图着色,使得没有一个环只有一个颜色,您需要最小化使用颜色的数量. 思路 因为是有向图,每个环两 ...
随机推荐
- js正則表達式:验证邮箱格式、password复杂度、手机号码、QQ号码
$(function () { $("input[name='sub']").on("click", function () { if (!isEmail($( ...
- 生成可重集的排序 (白书P184)
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> us ...
- postgres启动过程分析
今天来学习一下pg启动的过程. pg的启动命令./bin/postgres -D path/to/data. 1.主体监控进程 postmaster进程进入无限循环,等待客户端请求并为之提供请求的服务 ...
- SPOJ VLATTICE Visible Lattice Points (莫比乌斯反演基础题)
Visible Lattice Points Consider a N*N*N lattice. One corner is at (0,0,0) and the opposite one is at ...
- spi flash 操作
W25Q16V 是华邦出的一颗 spi flash. 25系列是比较通用的一个系列. 后面的数字 16 跟容量有关, 16 表示 16Mbits, 相当于 2MB. 与此类似的还有 W25Q128V ...
- openwrt mt7620 内存大小检测
单独编译内核: make V=s target/linux/install 相调函数调用流程: init/main.c : start_kernel() -> setup_arch(&c ...
- appium安装报错但运行成功
npm install -g appium ERR! fetch failed https://registry.npmjs.org/appium-uiauto/-/appium-uiauto-1. ...
- CodeChef - COUNTARI FTT+分块
Arithmetic Progressions Given N integers A1, A2, …. AN, Dexter wants to know how many ways he can ch ...
- PHP的date 函数
<!DOCTYPE html> <html> <body> <?php echo "今天是 " . date("Y/m/d&qu ...
- Codeforces Round #106 (Div. 2) D. Coloring Brackets —— 区间DP
题目链接:https://vjudge.net/problem/CodeForces-149D D. Coloring Brackets time limit per test 2 seconds m ...