Codeforces Round #290 (Div. 1)A. Fox And Names
来源:互联网 发布:大数据 计算机学报 编辑:程序博客网 时间:2024/05/21 19:28
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 thelexicographical 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 becomeslexicographical!
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 thelexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compares 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 characterssi andti according 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 stringnamei (1 ≤ |namei| ≤ 100), thei-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).
3rivestshamiradleman
bcdefghijklmnopqrsatuvwxyz
10touristpetrwjmzbmryeputonsvepifanovscottwuoooooooooooooooosubscriberrowdarktankengineer
Impossible
10petregorendagorionfeferivanilovetanyaromanovakostkadmitriyhmaratsnowbearbredorjaguarturnikcgyforever
aghjlnopefikdmbcqrstuvwxyz
7carcarecarefulcarefullybecarefuldontforgetsomethingotherwiseyouwillbehackedgoodluck
acbdefhijklmnogpqrstuvwxyz
拓扑排序即可,注意前一个串包含后一个串的情况
/************************************************************************* > File Name: cf290a.cpp > Author: ALex > Mail: zchao1995@gmail.com > Created Time: 2015年02月03日 星期二 01时03分58秒 ************************************************************************/#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const double pi = acos(-1);const int inf = 0x3f3f3f3f;const double eps = 1e-15;typedef long long LL;typedef pair <int, int> PLL;int ord[33];char mat[110][110];int edge[110][110];int in[33];vector <int> s;bool vis[33];char ans[33];void topo (){bool flag = 1;int ret = 0;for (int i = 1; i <= 26; ++i){ret += vis[i];}memset (in, 0, sizeof(in));s.clear();int cnt = 1;queue <int> qu;while (!qu.empty()){qu.pop();}int x = 0;for (int i = 1; i <= 26; ++i){if (!vis[i]){continue;}int t = 0;for (int j = 1; j <= 26; ++j){t += edge[j][i];}in[i] = t;if (!t){qu.push (i);++x;s.push_back (i);}}while (!qu.empty()){int u = qu.front();qu.pop();for (int i = 1; i <= 26; ++i){if (!edge[u][i]){continue;}edge[u][i] = 0;in[i]--;if (in[i] == 0){++x;qu.push (i);s.push_back (i);}}}if (x < ret){printf("Impossible\n");return;}x = 0;memset (vis, 0, sizeof(vis));for (int i = 0; i < s.size(); ++i){ans[x++] = s[i] - 1 + 'a';vis[s[i]] = 1;//printf("%c\n", s[i] + 'a' - 1);}for (int i = 1; i <= 26; ++i){if (!vis[i]){vis[i] = 1;ans[x++] = i - 1 + 'a';}}for (int i = x - 1; i >= 0; --i){printf("%c", ans[i]);}printf("\n");}int main (){int n;while (~scanf("%d", &n)){memset (vis, 0, sizeof(vis));memset (edge, 0, sizeof(edge));for (int i = 1; i <= n; ++i){scanf("%s", mat[i]);}bool x = true;for (int i = 2; i <= n; ++i){int len1 = strlen (mat[i - 1]);int len2 = strlen (mat[i]);bool flag = false;char s, b;for (int j = 0; j < min (len1, len2); ++j){if (mat[i][j] == mat[i - 1][j]){continue;}else{s = mat[i - 1][j];b = mat[i][j];flag = true;break;}}if (!flag && len1 > len2){x = false;break;}else if (flag){if (edge[b - 'a' + 1][s - 'a' + 1] == 0){edge[b - 'a' + 1][s - 'a' + 1] = 1;vis[b - 'a' + 1] = 1;vis[s - 'a' + 1] = 1;}else if (edge[s - 'a' + 1][b - 'a' + 1]){x = false;break;}}}if (!x){printf("Impossible\n");continue;}topo();}return 0;}
- Codeforces Round #290 (Div. 1)A. Fox And Names
- Codeforces Round #290 (Div. 1) A. Fox And Names
- 【拓扑排序】 Codeforces Round #290 (Div. 1) A Fox And Names
- C. Fox And Names Codeforces Round #290 (Div. 2)
- C. Fox And Names(Codeforces Round #290 (Div. 2))
- Codeforces Round #290 (Div. 2) C. Fox And Names
- Codeforces Round #290 (Div. 2) C题Fox And Names
- Codeforces Round #290 (Div. 2)-C. Fox And Names
- Codeforces Round #290 (Div. 2) C. Fox And Names && D. Fox And Jumping
- A. Fox And Snake(Codeforces Round #290 (Div. 2))
- Fox And Snake(Codeforces Round #290 (Div. 2)A)
- Codeforces Round #290 (Div. 2) A. Fox And Snake
- Codeforces Round #228 (Div. 1) A. Fox and Box Accumulation
- Codeforces Round #290 (Div. 2)C. Fox And Names(拓扑排序)
- Codeforces Round #290 (Div. 2)C - Fox And Names——拓扑排序
- Codeforces Round #290 (Div. 2) C. Fox And Names 拓扑排序
- Codeforces Round #290 (Div. 2) C. Fox And Names 拓扑排序
- Codeforces Round #290 (Div. 2) C. Fox And Names 拓扑排序
- 阿里云yum源没有glibc.i686
- CMD无法切换盘符
- android 教你怎么获取AndroidManifest.xml文件中的meta-data数据
- Java 调用图灵机器人
- android 反编译
- Codeforces Round #290 (Div. 1)A. Fox And Names
- Centos安装32位库
- ListView滑动到底部自动加载
- 使用monogodbexport导出query中符号“$”转义
- 第一篇文章
- How to Build a Strong A/B Testing Plan That Gets Results
- 微信营销有哪些微技巧呢? 转载
- 纯CSS实现表单验证
- 三个小伙子同时爱上一个姑娘,决斗来解决谁娶这个姑娘