Codeforces -- 510C -- Fox And Names, codeforces
Fox And Namestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox "). she heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn' t true. on some papers authors 'names WEREN' t sorted inlexicographical order in normal sense. but it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. if so, you shoshould find out any such order.
Lexicographical order is defined in following way. When we compareSAndT, First we find the leftmost position with differing characters:SI =TI. If there is no such position (I. e.SIs a prefixTOr vice versa) the shortest string is less. Otherwise, we compare charactersSIAndTIAccording to their order in alphabet.
Input
The first line contains an integerN(1 digit ≤ DigitNLimit ≤ limit 100): number of names.
Each of the followingNLines contain one stringNameI(1 hour ≤ hour |NameI| Limit ≤ limit 100),I-Th name. Each name contains only lowercase Latin letters. All names are different.
Output
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a 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 ).
Sample test (s) input
3rivestshamiradleman
Output
bcdefghijklmnopqrsatuvwxyz
Input
10touristpetrwjmzbmryeputonsvepifanovscottwuoooooooooooooooosubscriberrowdarktankengineer
Output
Impossible
Input
10petregorendagorionfeferivanilovetanyaromanovakostkadmitriyhmaratsnowbearbredorjaguarturnikcgyforever
Output
aghjlnopefikdmbcqrstuvwxyz
Input
7carcarecarefulcarefullybecarefuldontforgetsomethingotherwiseyouwillbehackedgoodluck
Output
acbdefhijklmnogpqrstuvwxyz
A string sorted by Lexicographic Order is provided. You can ask whether the Lexicographic Order can be written or not.
When comparing two strings, compare them from left to right. When different letters appear, do not compare them backward. One is the prefix of another, so long is behind.
An error occurs when the given sequence appears or when the prefix of a string is behind the string.
#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace std ;struct node{ int v , next ;} p[10000000];int head[50] , cnt ;int in[50] , vis[110] ;char str[110][110] ;char s[50] , num ;queue <int> que ;void add(int u,int v){ p[cnt].v = v ; p[cnt].next = head[u] ; head[u] = cnt++ ; return ;}int main(){ int n , i , j , m = 0 ,x , k , l ; memset(head,-1,sizeof(head)) ; memset(in,0,sizeof(in)) ; memset(vis,0,sizeof(vis)) ; cnt = 0 ; scanf("%d", &n) ; for(i = 0 ; i < n ; i++) { scanf("%s", str[i] ) ; x = strlen(str[i]) ; m = max(m,x) ; } int flag = 0 ; for(i = 0 ; i < m ; i++) { for(j = 0 ; j < n ; j++) { for(k = j-1 ; k >= 0 ; k--) { if( !(str[k][i] >= 'a' && str[k][i] <= 'z') ) continue ; for(l = 0 ; l < i ; l++) if( str[j][l] != str[k][l] ) break ; if( l < i ) continue ; if( str[j][i] == '\0' && str[k][i] != '\0' ) flag = 1 ; if( flag ) { flag = 1 ; break ; } if( str[j][i] != str[k][i] ) { add(str[k][i]-'a',str[j][i]-'a') ; in[ str[j][i]-'a' ]++ ; } } if( flag ) break ; } if( flag ) break ; } if( flag ) { printf("Impossible\n") ; return 0; } num = 0 ; while( !que.empty() ) que.pop() ; for(i = 0 ; i < 26 ; i++) if( in[i] == 0 ) { que.push(i) ; s[num++] = i + 'a' ; } int u , v ; while( !que.empty() ) { u = que.front() ; que.pop() ; for(i = head[u] ; i != -1 ;i = p[i].next) { v = p[i].v ; in[v]-- ; if( in[v] == 0 ) { que.push(v) ; s[num++] = v + 'a' ; } } } s[num] = '\0' ; if( num == 26 ) { printf("%s", s); } else printf("Impossible\n") ; return 0 ;}