For the two contiguous strings Si and si+1, if their first k-1 bits are the same, the K-bit is not the same, then in the dictionary order Si,k must be in front of si+1,k
Establish a forward edge from Si,k to si+1,k for topological sequencing
To ensure minimal dictionary ordering, a heap is required to maintain the topology process.
The trick is to put the blanks in the dictionary sequence, along with the topology. And if the blank dictionary order is not the smallest, there must be no solution.
#include <cstdio> #include <queue> #include <cstring>using namespace Std;int first[1010],__next[210*1010],v[210*1010],e;void addedge (int u,int v) {V[++e]=v;__next[e]=first[u]; First[u]=e;} Char a[1010][210],anss[101];int n,len[1010],ru[1010],ans;bool vis[1010];p riority_queue<int,vector<int> greater<int> >heap;int Main () {//freopen ("d.in", "R", stdin), scanf ("%d", &n); for (int i=1;i<=n;++i) { scanf ("%s", a[i]+1); Len[i]=strlen (a[i]+1);} for (int i=1;i<=200;++i) {for (int j=1;j<n;++j) {if (!vis[j]) {if (A[j][i]!=a[j+1][i]) {vis[j]=1; Addedge (A[j][i],a[j+1][i]); ++ru[a[j+1][i]];}}} for (int i= ' a '; i<= ' z '; ++i) {if (!ru[i]) {Heap.push (i);}} if (!ru[0]) {heap.push (0);} while (! Heap.empty ()) {int u=heap.top (); Heap.pop (); anss[++ans]=u;for (int i=first[u];i;i=__next[i]) {--ru[v[i]];if (!ru[v[i]]) {Heap.push (v[i]);}} Anss[ans+1]= ';p UTS ((ans==27 && anss[1]==0) anss+2: "-1"); return 0;}
"Topological ordering" CDOJ1635 Pipa string said Acacia, at that time, the moon in, once according to Choi Wan return