(Codeforces 510C) Fox and Names topology sequencing

Source: Internet
Author: User

Title Link: Http://codeforces.com/problemset/problem/510/C

Fox Ciel isGoing to publish a paper on FOCS (Foxes operated computer Systems, pronounce:"Fox"). She heard a rumor:the authors list on the paper isAlways sortedinchThe lexicographical order. After checking some examples, she found outThat sometimes it wasn't true. On some papers authors'Names weren'T sorted in lexicographical order in normal sense. But it is always true this after some modification of the order of letters in alphabet, the order of authors becomes Lexi cographical!She wants to know,ifThere exists an order of lettersinchLatin alphabet such that the names on the paper she isSubmitting is followinginchThe lexicographical order. If So, you should find outAny such order. lexicographical Order isDefinedinchFollowing. When we compare s and T, first we find the leftmost position with differing characters:si≠ti. If there isNo such position (I. E. s isA prefix of T or vice versa) the shorteststring  isLess. Otherwise, we compare characters Si and ti according to their orderinchalphabet. Inputthe first line contains an integer n (1≤n≤ -): Number of names. Each of the following n lines contain onestringNamei (1≤|namei| ≤ -), the I-th name. Each name contains only lowercase Latin letters. All names is different. Outputif there exists such order of letters that the given names is sorted lexicographically, output any such order asA permutation of characters'a'–'Z'(i. E. First output the first letter of the modified alphabet, then the second, and so on). Otherwise output a single word"Impossible"(without quotes). Examplesinput3RivestshamiradlemanoutputbcdefghijklmnopqrsatuvwxyzinputTenTouristpetrwjmzbmryeputonsvepifanovscottwuoooooooooooooooosubscriberrowdarktankengineeroutputimpossibleinput TenPetregorendagorionfeferivanilovetanyaromanovakostkadmitriyhmaratsnowbearbredorjaguarturnikcgyforeveroutputaghjlnopefi Kdmbcqrstuvwxyzinput7carcarecarefulcarefullybecarefuldontforgetsomethingotherwiseyouwillbehackedgoodluckoutputacbdefhijklmnogpqrstuvwxyz 

Test instructions: Given a few sorted strings, the collation of the output string, such as no output impossible

#include <cstring>#include<cstdlib>#include<cstdio>#include<math.h>#include<algorithm>#include<queue>using namespacestd;#defineMet (b) memset (A,b,sizeof (a))#definell Long Long#defineN 123structnode{intV,next;} S[n];inta[n],num[n],vis[n],k;intLen[n],inch[N];voidAddintUintv) {S[K].V=u; S[k].next=A[v]; A[V]=k++; inch[u]++;}CharStr[n][n];intMain () {intN; scanf ("%d", &n); Met (a,-1); k=0;  for(intI=0; i<n;i++) {scanf ("%s", Str[i]); Len[i]=strlen (Str[i]); } Met (inch,0);intJ;  for(intI=1; i<n;i++)        {             for(j=0; J<min (len[i],len[i-1]); J + +)            {                if(str[i][j]!=str[i-1][J])///the same location comparison of two strings                {                    intx=str[i][j]-'a'; inty=str[i-1][j]-'a';                    Add (x, y);  Break; }            }            if(len[i]<len[i-1] && J==len[i])///the previous string is longer than the last one, and the output is impossible{printf ("impossible\n"); return 0; }} met (Vis,0);  for(intI=1; i<= -; i++)        {            intjj= -; intJ;  for(intj=0;j< -; j + +)///a one-character lookup matches the            {                if(!vis[j] &&inch[j]==0) {Vis[j]=1; Num[i]=J;  for(intk=a[j];k!=-1; k=s[k].next)inch[s[k].v]--; JJ=J;  Break; }            }            if(jj== -)///There is a break in the middle, there is no comparison of a character{printf ("impossible\n"); return 0; }        }         for(intI=1; i<= -; i++) printf ("%c", num[i]+'a'); Puts (""); return 0;}

(Codeforces 510C) Fox and Names topology sequencing

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.