拓扑排序——Codeforces Round #290 (Div. 2) C. Fox And Names
来源:互联网 发布:淘宝 假货 投诉 编辑:程序博客网 时间:2024/05/09 14:49
题目链接:http://codeforces.com/contest/510/problem/C
题意:见链接
分析:对于给出的N个字符串,我们按顺序判断相邻2个字符串,如果前者比后者长,肯定不构成字典序。否则按第一对不相等的字符建有向边。构图结束后,跑一遍拓扑排序,如果得到的字符数最后得到字符数小于26则不构成字典序。
AC代码:
/************************************************************************* > File Name: test.cpp > Author: Akira > Mail: qaq.febr2.qaq@gmail.com ************************************************************************/#include<bits/stdc++.h>typedef long long LL;typedef unsigned long long ULL;typedef long double LD;#define MST(a,b) memset(a,b,sizeof(a))#define CLR(a) MST(a,0)#define Sqr(a) ((a)*(a))using namespace std;#define MaxN 200#define MaxM 1000#define INF 0x3f3f3f3f#define PI 3.1415926535897932384626const int mod = 1E9+7;const double eps = 1e-6;#define bug cout<<88888888<<endl;#define debug(x) cout << #x" = " << x << endl;int n;char str[MaxN][110];struct Edge{ int u,v,next;}edge[MaxM];int cont,head[MaxN];void add(char u, char v){ edge[cont].u = u; edge[cont].v = v; edge[cont].next = head[u]; head[u] = cont++;}int index[MaxN];void init(){ cont = 0; MST(head,-1);}bool judge(){ for(int i=0;i<n-1;i++) { for(int j=1;j<=strlen(str[i]+1);j++) { if( str[i][j]!=str[i+1][j]) { if(str[i][j]==0) return false; add(str[i][j], str[i+1][j]); index[str[i+1][j]]++; break; } } } return true;}char ans[26];bool topo(){ int cnt = 0; for(int i='a';i<='z';i++) if(index[i]==0) ans[cnt++] = i; int h = 0; while(h<cnt) { int now = ans[h++]; for(int i=head[now];i!=-1;i=edge[i].next) { int v = edge[i].v; index[v]--; if(index[v]==0)ans[cnt++] = v; } } if(cnt<26) return false; printf("%s\n", ans); return true;}int main(){ init(); //std::ios::sync_with_stdio(false); scanf("%d", &n); for(int i=0;i<n;i++) scanf("%s", str[i]+1); if( judge() && topo()); else printf("Impossible\n"); //system("pause");}
0 0
- 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 拓扑排序
- Codeforces Round #290 (Div. 2) C. Fox And Names 拓扑排序
- Codeforces Round #290 (Div. 2) - 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 Round #290 (Div. 1) A Fox And Names
- Codeforces Round #290 (Div. 2) C. Fox And Names && D. Fox And Jumping
- Codeforces Round #290 (Div. 2) - C. Fox And Names(最短路)
- codeforces 510C Fox And Names 拓扑排序
- Codeforces 510C - Fox And Names (拓扑排序)
- Codeforces 510C Fox And Names 拓扑排序
- mysql5.7.17 centos6.4x86 source安装
- code1018 单词接龙 dfs,字符串处理
- 清明杂记
- 猴子分桃
- 【Tarjan】【SCC】【拓扑排序】Graph practice T3 graph 题解
- 拓扑排序——Codeforces Round #290 (Div. 2) C. Fox And Names
- 双显卡(Intel+Nvidia)笔记本配置cuda开发环境
- Qt5--学习笔记-TCPsocket文件发送、接收
- matlab的算术运算
- UVA 558 判断环 【炒鸡简单的图论题】
- 考研英语近义词与反义词·九
- 数据库优化
- 设计模式
- 设计模式之策略模式