Portal: 510C
C. 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 are always sorted in the lexicographical order.
After checking some examples, she found out of that sometimes it wasn ' t true. On some papers authors ' names weren ' t sorted inlexicographical order in normal sense. But it is always true this after some modification of the order of letters in alphabet, the order of authors becomes Lexicographical!
She wants to know, if there exists a order of letters in Latin alphabet such that the names on the paper she is Submittin G is following in the lexicographical order. If So, you should find off any such order.
lexicographicalOrder is defined in following. When we comparesandT, first we find the leftmost position with differing characters: si? ≠? T I . If There is no such position (I. E.sis a prefix ofTor vice versa) the shortest string is less. Otherwise, we compare characters si and Ti According to their order in alphabet.
Input
The first line contains an integer n (1?≤? N? ≤?100): Number of names.
Each of the followingNLines contain one string namei (1?≤?| name I|? ≤?100), theI-th name. Each name contains only lowercase Latin letters. All names is different.
Output
If there exists such order of letters that the given names is sorted lexicographically, output any such order as a Permut ation 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
Problem Solving method: First to find the relationship between the dictionary letters. Connected by graphs. Then sort by topology
If the topology sort has not been learned yet. You can see http://www.cnblogs.com/newpanderking/archive/2012/10/18/2729552.html, it's a good story.
#include <iostream> #include <string> #include <algorithm> #include <cstdlib> #include < cstdio> #include <set> #include <map> #include <vector> #include <cstring> #include <stack > #include <cmath> #include <queue> #define INF 0x0f0f0f0fusing namespace Std;char s[105][105],ans[30]; int Cou[30];bool link[30][30];queue<int> Q;bool Solve ()//topological sort {int i,j,k,l,font,cnt=0;for (i=0;i<26;i++) {if ( !cou[i]) {Q.push (i); ans[cnt++]= ' a ' +i;}} while (!q.empty ()) {Font=q.front (); Q.pop (), for (i=0;i<26;i++) {if (Link[font][i]) {cou[i]--;if (cou[i]==0) {Q.push ( i); ans[cnt++]= ' a ' +i;}}} ans[26]= ' + '; if (cnt<26) return false;else return true;} int main () {int i,j,k,l,m,n,l1,l2,jud;k=1;memset (link,0,sizeof (link)), scanf ("%d%*c", &n); for (i=0;i<n;i++) { scanf ("%s", &s[i]);} for (i=0;i<n-1;i++) {L1=strlen (s[i]); L2=strlen (s[i+1]); Jud=0;for (j=0;j<l1&&j<l2&&!jud;j + +) {if (S[i][j]!=s[i+1][j]) {jud=1;if (!link[s[i][j]-' A '][s[i+1][j]-' a ']) {link[s[i][j]-' A '][s[i+1][j]-' a ']=true;cou[s[i+1][j]-' a ']++;}} if (!JUD&&L1>L2) k=0;//One of the two string prefixes is identical, but the first string is larger, the two strings must not satisfy the dictionary order}if (!k) {printf ("impossible\n");} Else{k=solve (), if (!k) printf ("impossible\n"), Else printf ("%s\n", ans); return 0;}
Codeforces 510C Fox and Names topology sequencing