CF512A:Fox And Names(拓扑排序 & 字典树)
来源:互联网 发布:flash mac版下载 编辑:程序博客网 时间:2024/05/16 06:07
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).
3rivestshamiradleman
bcdefghijklmnopqrsatuvwxyz
10touristpetrwjmzbmryeputonsvepifanovscottwuoooooooooooooooosubscriberrowdarktankengineer
Impossible
10petregorendagorionfeferivanilovetanyaromanovakostkadmitriyhmaratsnowbearbredorjaguarturnikcgyforever
aghjlnopefikdmbcqrstuvwxyz
7carcarecarefulcarefullybecarefuldontforgetsomethingotherwiseyouwillbehackedgoodluck
acbdefhijklmnogpqrstuvwxyz
思路:字典序就是在同一个位置,不同字母按字典序排,相同字母按下一位排,把单词扔到字典树建边跑一遍拓扑排序就行,注意a,aa这种也是Impossible的。
# include <bits/stdc++.h>using namespace std;char s[108];int in[28], vis[28], cnt=0, icount=0;int q[500], ans[28], id[28], flag=0;vector<int>v[28];struct node{int Next[28];}tri[30000];bool cmp(int x, int y){return ans[x] > ans[y];}void query(int len){ int cur = 0; for(int i=0; i<len; ++i) { int c = s[i]-'a'; for(int j=0; j<26; ++j) { if(j!=c && tri[cur].Next[j]) { ++in[j]; v[c].push_back(j); if(++vis[c]==1) ++icount; if(++vis[j]==1) ++icount; } } if(i==len-1 && tri[cur].Next[c]) { cur = tri[cur].Next[c]; for(int k=0; k<26; ++k) if(tri[cur].Next[k]) flag=1; } if(tri[cur].Next[c]) cur = tri[cur].Next[c]; else break; }}void add(int len){ int cur = 0; for(int i=0; i<len; ++i) { int c = s[i]-'a'; if(tri[cur].Next[c]) cur = tri[cur].Next[c]; else { tri[cur].Next[c] = ++cnt; cur = cnt; } }}void solve(){ int l=0, r=0, _icount=0; for(int i=0; i<26; ++i) if(vis[i] && in[i] == 0) q[r++] = i, ans[i] = 1, ++_icount; while(l<r) { int s = q[l++]; for(auto e : v[s]) { if(ans[e]){flag = 1;return;} if(--in[e] == 0) { ans[e] = ans[s] + 1; q[r++] = e; ++_icount; } } } if(_icount != icount) flag = 1;}int main(){ int n; scanf("%d",&n); memset(tri, 0, sizeof(tri)); for(int i=0; i<n; ++i) { scanf("%s",s); int len = strlen(s); query(len); if(flag) return 0*puts("Impossible"); add(len); } solve(); if(flag) return 0*puts("Impossible"); for(int i=0; i<26; ++i) id[i] = i; sort(id, id+26, cmp); for(int i=0; i<26; ++i) printf("%c",id[i]+'a'); puts(""); return 0;}
- CF512A:Fox And Names(拓扑排序 & 字典树)
- [CodeForces 510C]Fox And Names[字典序][拓扑排序]
- CodeForces510 C. Fox And Names(拓扑排序)
- 510C Fox And Names(拓扑排序)
- CF 510C Fox And Names(拓扑排序)
- 拓扑排序 Codecorces510C Fox And Names
- codeforces510C. Fox And Names【拓扑排序】
- 拓扑排序 Fox And Names : CodeForces
- codeforces 510C Fox And Names 拓扑排序
- Codeforces 510C - Fox And Names (拓扑排序)
- CF 510C(Fox And Names-拓扑排序)
- Codeforces 510C Fox And Names 拓扑排序
- codeforces 510c Fox And Names 拓扑排序
- Codeforces 510C Fox And Names 拓扑排序
- [CodeForces] 510 C Fox And Names [拓扑排序]
- 拓扑排序 CodeForces - 510C Fox And Names
- CF 510C Fox And Names【拓扑排序】
- Codeforces 510C Fox And Names【拓扑排序】
- 中国科学院分子影像重点实验室田捷研究员团队自主研发的三维医学图像处理及分析平台3DMed更新到第五版
- Java集合体系总结
- Android源码目录结构详解
- ES6箭头函数
- 约瑟夫环问题
- CF512A:Fox And Names(拓扑排序 & 字典树)
- easyui filebox限制文件上传的类型accept (新旧版本)
- eclipse import Existing Projects into Workspace导入时没找到项目的解決办法
- 计算机体系架构rtl 第五章第六章
- mac上访问samba服务器
- 唯一分解定理(算术基本定理)
- Uniwebview1.9的使用总结发布ios和Android
- 【备忘】HTML5教程从入门到精通及网站源码下载
- 最长公共子串、最长公共子序列、最长回文子串、最长回文子序列、回文子串个数