UVa 200-rare Order

Source: Internet
Author: User

Title: give you a sequence of uppercase letters, which are given in a special dictionary order, to output the alphabetical sequence of known relationships.

Analysis: Graph theory, topological sequencing. Using DFS to sort the topology, log back the timestamp, reverse output can be.

Because it is a partial-order relationship, it has transitivity, so as long as you compare adjacent two strings.

Note: Finally, add a newline ╮(╯▽╰)╭.

 #include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <cmath>using namespace Std;char list[10001][22];int maps[27][27  ];int used[27];int stack[27];int stack_size = 0;void dfs (int s) {Used[s] = 0;for (int i = 0; i <) + + i) if (Used[i] && Maps[s][i]) {DFS (i);} Stack[stack_size + +] = s+ ' A ';} int main () {int count = 0;while (scanf ("%s", List[count]) && list[count][0]! = ' # ') + count;memset (maps, 0, sizeof (m  APS)); memset (used, 0, sizeof (used)); used[list[0][0]-' a '] = 1;//has only one set for (int i = 1; i < count; + + i) {for (int p = 0, q = 0; LIST[I-1][P] && list[i][q]; + + P, + + q) if (list[i-1][p]! = List[i][q]) {maps[list[i-1][p]-' a '][list[i][q]-' a '] = 1;used[list[i-1][p]-' a '] = Used[list I [q]-' A '] = 1;break;}}    for (int i = 0; i <, + + i) if (Used[i]) DFS (i), while (Stack_size-) printf ("%c", Stack[stack_size]);p rintf ("\ n"); return 0;} 

UVa 200-rare Order

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.