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 should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: si ≠ ti. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters si and tiaccording to their order in alphabet.
Input
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string namei (1 ≤ |namei| ≤ 100), the 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
給出按字典序排序好的字串,問是否能寫出字典序來,或者不能寫出。
注意判斷各種情況,兩個字串比較的時候,從左向右開始比較,當出現不同的字母后,不再向後比較。一個是另一個的首碼,那麼長的在後面。
當給出的順序出現環的時候,或者有串的首碼在串的後面時,出現錯誤。
#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 ;}