UVa 124-following Orders

Source: Internet
Author: User

Title: give you some lowercase letters, and some of the partial order pairs of lowercase letters, to find out all the permutations that satisfy the partial order relationship.

Analysis: Graph theory, search, topological sort. Ontology can be directly topological partial order, here directly search;

It is possible to reach the next point by judging the partial order relationship.

Description: Last week, Sunday went home to ╮(╯▽╰)╭.

#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < Cstdio> #include <cmath>using namespace Std;char buf[55],temp[5000];int map[55][55],space[256],used[55], Head[55],save[55];void DFS (int s, int d, int n) {if (d = = N) {for (int i = 0; i < n; + + i) printf ("%c", Buf[save[i]);p rintf ("\ n"); return;} for (int i = 0; i < n; + + i) {int flag = 1;for (int j = 0; J < D; + + j) if (Map[i][save[j]]) {flag = 0;break;} if (flag &&!used[i]) {used[i] = 1;save[d] = I;dfs (i, d+1, n); used[i] = 0;}}} int main () {int t = 0;while (gets (BUF)) {if (T + +) printf ("\ n"), memset (space, 0, sizeof); for (int i = 0; buf[i]; + + i) space[buf[i] = 1;int count = 0;for (int i = ' a '; I <= ' z '; + + i) if (Space[i]) {Space[buf[count] = i] = Count;cou NT + +;} Gets (temp); int number = 0,move = 0;while (Temp[move]) {if (Temp[move] >= ' A ' && temp[move] <= ' z ') temp[numb ER + +] = Temp[move];move + +;} memset (map, 0, sizeof (map)); for (int i = 0; i < number; I + = 2) {map[space[temp[i]]][space[temp[i+1]] [= 1;map[i][i] = 1;}  Find starting point set int size = 0;for (int i = 0; i < count; + + i) {int flag = 1;for (int j = 0; J < Count; + + j) {if (i = = j) continue;if (Map[j][i]) {flag = 0;break;}} if (flag) Head[size + +] = i;} memset (used, 0, sizeof (used)); for (int i = 0; i < size; + + i) {Used[head[i]] = 1;save[0] = Head[i];d FS (Head[i], 1, CO    UNT); Used[head[i]] = 0;}} return 0;}


UVa 124-following Orders

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.