首页 > 代码库 > Codeforces Round #290 (Div. 2) 拓扑排序
Codeforces Round #290 (Div. 2) 拓扑排序
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 in lexicographical 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.
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.
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).
3
rivest
shamir
adleman
bcdefghijklmnopqrsatuvwxyz
10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer
Impossible
10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever
aghjlnopefikdmbcqrstuvwxyz
7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck
acbdefhijklmnogpqrstuvwxyz
题意:给你n个字符串 现在要求你重新定义字典序 使得n个字符串的顺序为升序 输出新的26个字母的字典序
题解:对相邻的两个字符串 在第一个字符不同的位置的两个字符间连一条单向边 拓扑排序
1 #pragma comment(linker, "/STACK:102400000,102400000") 2 #include <bits/stdc++.h> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <iostream> 6 #include <cstdlib> 7 #include <cstring> 8 #include <algorithm> 9 #include <cmath>10 #include <cctype>11 #include <map>12 #include <set>13 #include <queue>14 #include <bitset>15 #include <string>16 #include <complex>17 #define LL long long18 #define mod 100000000719 using namespace std;20 char a[105][1003];21 int g[26][26];22 int in[26];23 queue<int> q;24 int vis[26];25 int ans[26];26 int jishu=0;27 int n;28 bool topsort(){29 while(!q.empty())30 q.pop();31 for(int i=0;i<=25;i++){32 if(in[i]==0){33 q.push(i);34 vis[i]=1;35 }36 }37 jishu=0;38 int exm;39 while(!q.empty()){40 exm=q.front();41 ans[jishu++]=exm;42 q.pop();43 for(int i=0;i<=25;i++){44 if(g[exm][i]==1){45 if(--in[i]==0&&vis[i]==0){46 vis[i]=1;47 q.push(i);48 }49 }50 }51 }52 }53 int main()54 {55 scanf("%d",&n);56 for(int i=0;i<n;i++)57 scanf("%s",a[i]);58 for(int i=0;i<n-1;i++)59 {60 int l1,l2;61 l1=strlen(a[i]);62 l2=strlen(a[i+1]);63 int p=0;64 while(p<min(l1,l2)&&a[i][p]==a[i+1][p]) p++;65 if(p==l1&&l1<l2) continue;66 if(p==l2&&l2<l1) {67 printf("Impossible\n");//**68 return 0;69 }70 if(g[a[i][p]-‘a‘][a[i+1][p]-‘a‘]) continue;71 g[a[i][p]-‘a‘][a[i+1][p]-‘a‘]=1;72 in[a[i+1][p]-‘a‘]++;73 }74 topsort();75 if(jishu==26){76 for(int i=0;i<=25;i++)77 printf("%c",ans[i]+‘a‘);78 }79 else80 printf("Impossible\n");81 return 0;82 }
Codeforces Round #290 (Div. 2) 拓扑排序