首页 > 代码库 > C. Fox And Names Codeforces Round #290 (Div. 2)
C. Fox And Names 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 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.
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
裸的topsort,结果在终判时挂了,就因为没有特判,杯具。。。。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <queue> #define maxn 111 using namespace std; int indegree[maxn],path[maxn]; int n,m; char w[105][105]; queue<int>Q; vector<int>edges[10000]; void topsort() { memset(path,0,sizeof(path)); while (!Q.empty()) Q.pop(); for (int i=0;i<26;i++) if (!indegree[i]) Q.push(i); int num=0; while (!Q.empty()) { int now=Q.front(); Q.pop(); path[num++]=now; for (int i=0;i<edges[now].size();i++) { --indegree[ edges[now][i]]; if (!indegree[ edges[now][i] ]) { Q.push(edges[now][i]); } } } if (num<26) { printf("Impossible\n"); return ; } for (int i=0;i<num;i++) printf("%c",path[i]+'a'); printf("\n"); } int main() { while (~scanf("%d",&n)) { memset(indegree,0,sizeof(indegree)); for (int i=1;i<=n;i++) edges[i].clear(); int u,v; for (int i=0;i<n;i++) scanf("%s",w[i]); int flag=0; for (int i=1;i<n;i++) { int a=i-1,b=i; int la=strlen(w[a]); int lb=strlen(w[b]); int aa=0,bb=0; while (aa<la&&bb<lb&&w[a][aa]==w[b][bb]) { aa++; bb++; } if (bb==lb&&aa<la) {flag=1; break;} //掉了这一句,结果挂了。。。 if (aa==la&&bb<=lb) continue; indegree[w[b][bb]-'a']++; edges[w[a][aa]-'a'].push_back(w[b][bb]-'a'); } if (flag) { printf("Impossible\n"); continue; } topsort(); } return 0; }
C. Fox And Names Codeforces Round #290 (Div. 2)