codeforces--510C--Fox And Names
2015-02-03 14:06
513 查看
Fox And Names
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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
output
input
output
input
output
input
output
给出按字典序排序好的字符串,问是否能写出字典序来,或者不能写出。
注意判断各种情况,两个字符串比较的时候,从左向右开始比较,当出现不同的字母后,不再向后比较。一个是另一个的前缀,那么长的在后面。
当给出的顺序出现环的时候,或者有串的前缀在串的后面时,出现错误。
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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
3 rivest shamir adleman
output
bcdefghijklmnopqrsatuvwxyz
input
10 tourist petr wjmzbmr yeputons vepifanov scottwu oooooooooooooooo subscriber rowdark tankengineer
output
Impossible
input
10 petr egor endagorion feferivan ilovetanyaromanova kostka dmitriyh maratsnowbear bredorjaguarturnik cgyforever
output
aghjlnopefikdmbcqrstuvwxyz
input
7 car care careful carefully becarefuldontforgetsomething otherwiseyouwillbehacked goodluck
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 ; }
相关文章推荐
- Codeforces 510C Fox And Names 拓扑序+判环
- 【CodeForces】510C - Fox And Names(拓扑 & STL)
- 拓扑排序 CodeForces - 510C Fox And Names
- codeforces 510C Fox And Names 拓扑排序
- 拓扑排序 Fox And Names : CodeForces - 510C
- Codeforces 510C Fox And Names【拓扑排序】
- codeforces-510C-Fox And Names【DFS】
- Codeforces 510C - Fox And Names
- 【codeforces 510C】Fox And Names
- [CodeForces 510C]Fox And Names[字典序][拓扑排序]
- CodeForces 510C Fox And Names
- Codeforces 510C Fox And Names 拓扑排序
- CodeForces - 510C Fox And Names
- Codeforces 510C - Fox And Names (拓扑排序)
- codeforces 510c Fox And Names 拓扑排序
- Codeforces 510C Fox And Names 拓扑排序
- codeforces 510C Fox And Names 拓扑
- CF 510C(Fox And Names-拓扑排序)
- Fox And Names - CodeForces 510 C
- codeforces290 A.Fox And Names