Codeforces 510 C Fox and Names "topological sort"

Source: Internet
Author: User

Test instructions: gives the name of n string, which indicates the dictionary order from small to large, in order to conform to such a string permutation of the dictionary

Iterate through the string first, and when you encounter a different time, add edge, record the corresponding degree

Then is the BFS process, if a point has not been accessed, and the degree of 0, it is added to the queue, and the node it points to the degree minus 1

In addition, if LEN[I]<LEN[I-1], the I string is the prefix of the i-1 string, but it is also in the back, so that there is no dictionary order, direct output "impossible"

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <stack>6#include <vector>7#include <map>8#include <Set>9#include <queue>Ten#include <algorithm> One using namespacestd; A  -typedefLong LongLL; - Const intINF = (1<< -)-1; the Const intMod=1000000007; - Const intmaxn=100005; -  - intE[MAXN],FIRST[MAXN],NEXT[MAXN],ANS[MAXN],LEN[MAXN],VIS[MAXN],inch[MAXN]; + Chars[ the][ the]; - intecnt; +  A voidAddedges (intUintv) { ate[ecnt]=v; -next[ecnt]=First[u]; -first[u]=ecnt; -     inch[v]++; -ecnt++; - } in  - intMain () { to      + //freopen ("In.txt", "R", stdin); - //freopen ("OUT.txt", "w", stdout); the     intN; *Cin>>N; $     Panax Notoginsengmemset (first,-1,sizeof(first)); -      for(intI=1; i<=n;i++){ theCin>>S[i]; +len[i]=strlen (S[i]); A     } the      +      -     intJ; $Ecnt=0; $      for(intI=2; i<=n;i++){ -          for(j=0; J<min (len[i],len[i-1]); J + +){ -             if(s[i-1][j]!=S[i][j]) { theAddedges (s[i-1][j]-'a', s[i][j]-'a'); -                  Break;Wuyi             } the         } -         if(len[i]<len[i-1]&&j==Len[i]) { Wuprintf"impossible\n"); -             return 0; About         } $     } -      -      -memset (Vis,0,sizeof(Vis)); A      +      for(intI=1; i<= -; i++){ the         intjj= -; -         intJ; $          for(j=0;j< -; j + +){ the             if(!vis[j]&&inch[j]==0){ thevis[j]=1; theans[i]=J; the                  for(intk=first[j];k!=-1; k=Next[k]) { -                     inch[e[k]]--; in                 } thejj=J; the                  Break; About             } the         } the         if(jj== -){//The value of JJ has not changed, which means that the topological order cannot be found theprintf"impossible\n"); +             return 0;  -         } the     }Bayi      the      for(intI=1; i<= -; i++) theprintf"%c", ans[i]+'a'); -printf"\ n"); -     return 0; the}
View Code

Codeforces 510 C Fox and Names "topological sort"

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.