Codeforces510 C. Fox And Names
出处: Codeforces
主要算法:判环+拓扑
难度:4.2
思路分析:
要是把这道题联系到图上就很容易想了。
如何建图?由于最后要求名字满足字典序,所以不妨以字母为节点,然后按照题意的顺序从小的到大的连边。建图了又什么用?如果图存在环,那么也就意味着矛盾了——因为这比自己小的节点比自己大。因此是否存在一个合法的字典序的判断依据就是建的图是否存在环。
第二步,如何输出任意一个方案?这很简单,由于有可能有的字母根本没有参与建图,所以这些字母就不需要管了。对于在图里的字母,入度为0的点就是已知的应当最小的点——所以我们进行一次拓扑排序来得到序列。最后和其它字母拼合起来就好了。
代码注意点:
注意有两个相同前缀的字符串时,如果前面的那个字符串比后面的还要长,那么一定不满足,直接输出impossible就好了。因为无论字典序如何,都不可能满足前面的字典序比后面的小。
Code
/** This Program is written by QiXingZhi **/
#include <cstdio>
#include <queue>
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define r read()
#define Max(a,b) (((a)>(b)) ? (a) : (b))
#define Min(a,b) (((a)<(b)) ? (a) : (b))
using namespace std;
typedef long long ll;
const int N = ;
const int INF = ;
inline int read(){
int x = ; int w = ; register int c = getchar();
while(c ^ '-' && (c < '' || c > '')) c = getchar();
if(c == '-') w = -, c = getchar();
while(c >= '' && c <= '') x = (x << ) +(x << ) + c - '', c = getchar();
return x * w;
}
string s[N];
queue <int> q;
vector <int> G[];
bool exist[N],vis[N],b[N],hh[N];
int rd[N],Q[N],ans_topo[N];
int n,len1,len2,flg,_cur,t,topo_num;
inline void AddEdge(int u, int v){
exist[u] = ;
exist[v] = ;
G[u].push_back(v);
++rd[v];
}
inline void ThrowOut(){
printf("Impossible");
exit();
}
inline bool BFS(int x){
while(!q.empty()) q.pop();
q.push(x);
int cur,sz,to;
while(!q.empty()){
cur = q.front();
q.pop();
if(cur == _cur){
if(flg == -){
++flg;
}
else if(flg == ){
return ;
}
}
if(vis[cur]) continue;
vis[cur] = ;
sz = G[cur].size();
for(int i = ; i < sz; ++i){
to = G[cur][i];
q.push(to);
}
}
return ;
}
inline bool Check_Circle(){
for(int i = 'a'; i <= 'z'; ++i){
memset(vis,,sizeof(vis));
if(exist[i]){
_cur = i;
flg = -;
if(BFS(_cur)) return ;
}
}
return ;
}
int main(){
// freopen(".in","r",stdin);
cin >> n;
for(int i = ; i <= n; ++i){
cin >> s[i];
}
for(int i = ; i <= n; ++i){
len1 = s[i-].size();
len2 = s[i].size();
flg = ;
for(int j = ; j < len2; ++j){
if(j >= len1){
flg = ;
break;
}
if(s[i-][j] != s[i][j]){
flg = ;
AddEdge(s[i-][j],s[i][j]);
break;
}
}
if(flg == && len1 > len2){
ThrowOut();
}
}
if(Check_Circle() == ){
ThrowOut();
}
int flg=,sz;
for(;;){
flg = , t = ;
for(int i = 'a'; i <= 'z'; ++i){
if(rd[i] == && !b[i] && exist[i]){
flg = ;
b[i] = ;
Q[++t] = i;
}
}
if(!flg) break;
for(int i = ; i <= t; ++i){
int cur = Q[i];
ans_topo[++topo_num] = cur;
hh[cur] = ;
sz = G[cur].size();
for(int j = ; j < sz; ++j){
--rd[G[cur][j]];
}
}
}
for(int i = ; i <= topo_num; ++i){
printf("%c",ans_topo[i]);
}
for(int i = 'a'; i <= 'z'; ++i){
if(!hh[i]){
printf("%c",i);
}
}
return ;
}
Codeforces510 C. Fox And Names的更多相关文章
- Codeforces Round #290 (Div. 2) C. Fox And Names dfs
C. Fox And Names 题目连接: http://codeforces.com/contest/510/problem/C Description Fox Ciel is going to ...
- C. Fox And Names
C. Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- CF Fox And Names (拓扑排序)
Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- codeforce 510C Fox And Names(拓扑排序)
Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- (CodeForces 510C) Fox And Names 拓扑排序
题目链接:http://codeforces.com/problemset/problem/510/C Fox Ciel is going to publish a paper on FOCS (Fo ...
- Fox And Names
Description Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce ...
- [CF #290-C] Fox And Names (拓扑排序)
题目链接:http://codeforces.com/contest/510/problem/C 题目大意:构造一个字母表,使得按照你的字母表能够满足输入的是按照字典序排下来. 递归建图:竖着切下来, ...
- 拓扑排序 Codeforces Round #290 (Div. 2) C. Fox And Names
题目传送门 /* 给出n个字符串,求是否有一个“字典序”使得n个字符串是从小到大排序 拓扑排序 详细解释:http://www.2cto.com/kf/201502/374966.html */ #i ...
- codeforces 510 C Fox And Names【拓扑排序】
题意:给出n串名字,表示字典序从小到大,求符合这样的字符串排列的字典序 先挨个地遍历字符串,遇到不相同的时候,加边,记录相应的入度 然后就是bfs的过程,如果某一点没有被访问过,且入度为0,则把它加入 ...
随机推荐
- 关于NETCORE中的捆绑与最小化 以及与CDN连用
参考文档:MSDN Bundling and minification in ASP.NET Core 细说ASP.NET Core静态文件的缓存方式
- 四、Input框改placeholder中字体的颜色
Input框改placeholder中字体的颜色 input::-webkit-input-placeholder { color: #ccc; font-size: 12px; }
- H5 30-CSS元素的显示模式
30-CSS元素的显示模式 我是div 我是段落 我是标题 我是span 我是加粗 我是强调 <!DOCTYPE html><html lang="en"> ...
- 论一类每次修改log个结点更新的线段树标记方法
楼房重建(BZOJ2957) 多次询问一个区间中大于区间内这个数之前所有数的数的数量. 每个线段树结点维护该节点的答案c和区间内最大值m.假设有函数get(x,cm)=结点x中答案>cm的长度. ...
- Day7 Ubantu学习(一)
Linux是多用户操作系统 Ubantu学习参考网址:https://www.cnblogs.com/resn/p/5800922.html 1.虚拟机网络类型的理解 bridged(桥接模式) : ...
- iOS iCloud云存储数据
https://www.jianshu.com/p/ce8cfaf6030e 2017.11.29 16:05* 字数 452 阅读 302评论 0喜欢 1 因为上一次做了用keychain来持久化存 ...
- openstack-KVM安装与使用
一.KVM安装 1.安装条件 VT-x BIOS Intel9R) Virtualization Tech [Enabled] cat /proc/cpuinfo | grep -e vmx -e n ...
- awk+sed编程
- ocrosoft 1015 习题1.22 求一元二次方程a*x^2 + b*x + c = 0的根
http://acm.ocrosoft.com/problem.php?id=1015 题目描述 求一元二次方程a*x2 + b*x + c = 0的根.系数a.b.c为浮点数,其值在运行时由键盘输入 ...
- mysql 无法退出sql命令行编辑
mysql 无法退出sql命令行编辑 | ANBOBhttp://www.anbob.com/archives/579.html mysql 无法退出sql命令行编辑 - 码农甲乙丙 - CSDN博客 ...