codeforces 290div2 C.Fox And Names
来源:互联网 发布:linux项目实战 编辑:程序博客网 时间:2024/05/18 01:16
拓扑排序。
#include<iostream>#include<string>#include<cstring>#include<cstdio>#include<cmath>#include<iomanip>#include<map>#include<algorithm>#include<queue>#include<set>#define inf 10000000#define pi acos(-1.0)#define eps 1e-8#define seed 131using namespace std;typedef pair<int,int> pii;typedef unsigned long long ULL;typedef long long LL;const int maxn=100005;char s[105][105];int n;int g[26][26];int d[26];int vis[26];int t;bool toposort();bool solve(int a,int b);int main(){ scanf("%d",&n); for(int i=0;i<n;i++) scanf("%s",s[i]); memset(g,0,sizeof(g)); for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) { if(!solve(i,j)) { printf("Impossible\n"); return 0; } } } if(toposort()) { for(int i=0;i<26;i++) printf("%c",(char)d[i]+'a'); } else printf("Impossible\n"); return 0;}bool solve(int a,int b){ int len1=strlen(s[a]); int len2=strlen(s[b]); int len=min(len1,len2); for(int i=0;i<len;i++) { if(s[a][i]!=s[b][i]) { g[s[a][i]-'a'][s[b][i]-'a']=1; return true; } } if(len1<=len2)//trick return true; return false;}bool dfs(int u){ vis[u]=-1;//正在访问中 for(int i=0;i<26;i++) { if(g[u][i]) { if(vis[i]==1) continue; if(vis[i]==-1)//有环,失败退出 return false; if(!dfs(i)) return false; } } vis[u]=1; d[--t]=u; return true;}bool toposort(){ memset(vis,0,sizeof(vis)); t=26; for(int i=0;i<26;i++) { if(!vis[i]) { if(!dfs(i)) return false; } } return true;}
0 0
- codeforces 290div2 C.Fox And Names
- CF 290Div2 C 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--510C--Fox And Names
- Codeforces 510C - Fox And Names
- Fox And Names - CodeForces 510 C
- codeforces 510C Fox And Names 拓扑
- codeforces-510C-Fox And Names【DFS】
- CodeForces 510C Fox And Names
- CodeForces - 510C Fox And Names
- [拓扑] Codeforces #510C. Fox And Names
- Codeforces Round #290 (Div. 2) C. Fox And Names && D. Fox And Jumping
- #290 (div.2) C. Fox And Names
- codeforces 510C Fox And Names 拓扑排序
- WebView组件学习以及WebViewClient 的方法解释
- swift基础知识点笔记
- getRequestDispatcher()与sendRedirect()的区别
- 程序员常去的14个顶级开发社区
- Matlab并行编程<cellfun & arrayfun>
- codeforces 290div2 C.Fox And Names
- USACO6.2.1 Calf Flac(calfflac)
- 图论——二分图匹配
- LeetCode-Climbing Stairs(爬楼梯问题)
- 最短路径B
- Matlab中巧用LaTex
- matlab乘与点乘的区别 (*与.* ^与.^)
- 谷歌三大核心技术(一)Google File System中文版
- Android手机分辨率基础知识(DPI,DIP计算)