codeforce 510C Fox And Names
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了codeforce 510C Fox And Names相关的知识,希望对你有一定的参考价值。
原题地址:http://codeforces.com/problemset/problem/510/C
题意:
给定一组字符串,要求找到一个字符排列,使得该字符串的排序满足字符序列对应地字典序
题解
拓扑排序……
#include<bits/stdc++.h> #define clr(x,y) memset((x),(y),sizeof(x)) using namespace std; typedef long long LL; const int maxn=300; int c[maxn+5]; int topo[maxn+5]; int cnt; vector <int> G[maxn+5]; char Name[105][105]; bool dfs(int u) { c[u]=-1; for (int i=0;i<G[u].size();++i) { int v=G[u][i]; if (c[v]<0) return false; else if (!c[v] && !dfs(v)) return false; } c[u]=1; topo[--cnt]=u; return true; } bool toposort(int n) { cnt=n; clr(c,0); for (int u=0;u<n;++u) { if (!c[u] && !dfs(u)) return false; } return true; } int main(void) { #ifdef ex freopen ("../in.txt","r",stdin); //freopen ("../out.txt","w",stdout); #endif int n; scanf("%d",&n); for (int i=1;i<=n;++i) { scanf("%s",Name[i]); } for (int i=1;i<=n-1;++i) { int len1=strlen(Name[i]); int len2=strlen(Name[i+1]); for (int j=0;j<min(len1,len2);++j) { int t1=Name[i][j]-‘a‘; int t2=Name[i+1][j]-‘a‘; if (t1!=t2) { G[t1].push_back(t2); //printf("%c %c\n",Name[i][j],Name[i+1][j]); break; } if (len1>len2 && j==min(len1,len2)-1) { printf("Impossible\n"); return 0; } } } bool f=toposort(26); if (!f) printf("Impossible\n"); else { for (int i=0;i<26;++i) printf("%c",topo[i]+‘a‘); } }
以上是关于codeforce 510C Fox And Names的主要内容,如果未能解决你的问题,请参考以下文章
网络流(最大流)CodeForces 512C:Fox And Dinner
Codeforces 388 D. Fox and Perfect Sets
[Codeforces#510C] Fox And Names
[Codeforces#510D] Fox And Jumping