Approximate test instructions: Each sample contains two lines, the first line is entered n characters, and may be unordered. The second line is entered in pairs of a B, representing a to precede B. All of the outputs conform to such sequences.
Idea: A very obvious sort of topology. To output all of the sequences, DFS is performed from a point with a degree of 0, each time selecting a point with an entry level of 0, adding an output sequence and reducing the degree of the point adjacent to it. After Dfs is over, change the status back.
#include <stdio.h> #include <algorithm> #include <set> #include <map> #include <vector># Include <math.h> #include <string.h> #include <stack> #include <queue> #define LL long long# Define _ll __int64using namespace Std;const int maxn = 110;char contents[30];bool mapp[60][60];int n = 0;char t,t1,t2;int Flag;int in[30];int vis[30];int ans[30];void dfs (int num) {if (num = = N) {for (int i = 0; i < n; i++) printf ("%c", Ans[i]); printf ("\ n"); Return } for (int i = 0; i < n; i++) {if (!vis[i] &&!in[contents[i]-' A "]) {for (int j = 0; J < N; J + +) {if (mapp[contents[i]-' a '][contents[j]-' a ']) {in[Conten ts[j]-' a ']--; }} Ans[num] = Contents[i]; Vis[i] = 1; DFS (NUM+1); Note Change the status back to for (int j = 0; J < N J + +) {if (mapp[contents[i]-' a '][contents[j]-' a ']) {in[Conten ts[j]-' a ']++; }} Vis[i] = 0; }}}int Main () {flag = 0; while (~SCANF ("%c", &t)) {if (T >= ' a ' && t <= ' z ') {contents[n++] = t; Continue } else if (t = = ") continue; else {if (flag) printf ("\ n"); flag = 1; Sort (contents,contents+n); Because the input may be unordered, sort memset (in,0,sizeof (in)) first; memset (mapp,false,sizeof (MAPP)); memset (vis,0,sizeof (VIS)); while (~scanf ("%c%c", &t1,&t2)) {mapp[t1-' a '][t2-' a '] = 1; in[t2-' a ']++; if (getchar () = = ' \ n ') break; } dfs (0); n = 0; }} return 0;}