"Topological sequencing" CDOJ1635 Pipa string said Acacia, then the moon in, once according to Choi Wan

Source: Internet
Author: User

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

Related Article

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.