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,则把它加入 ...
随机推荐
- 来自后端的突袭? --开包即食的教程带你浅尝最新开源的C# Web引擎 Blazor
在今年年初, 恰逢新春佳节临近的时候. 微软给全球的C#开发者们, 着实的送上了一分惊喜. 微软正式开源Blazor ,将.NET带回到浏览器. 这个小惊喜, 迅速的在dotnet开发者中间传开了. ...
- 使用模块PIL 生成 随机验证码
--------------默认自己无能,无疑是给失败制造机会!你认为自己是什么样的人,就将成为什么样的人. 要使用PIL模块. 安装: 1 pip3 install pillow 基本使用 1. 创 ...
- Python全栈开发之路 【第六篇】:Python基础之常用模块
本节内容 模块分类: 好处: 标准库: help("modules") 查看所有python自带模块列表 第三方开源模块: 自定义模块: 模块调用: import module f ...
- Mysql:is not allowed to connect to this MySQL server
连接mysql的时候发生这个错误:ERROR 1130: Host '192.168.1.110' is not allowed to connect to this MySQL server 解决方 ...
- H5 31-CSS元素显示模式转换
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Randomized Online PCA Algorithms with Regret Bounds that are Logarithmic in the Dimension
目录 Setup of Batch PCA and Online PCA Hedge Algorithm 改进算法 用于矩阵 \(rounding()\) 前俩次,都用到了\(rounding()\) ...
- jconsole & jvisualvm远程监视websphere服务器JVM的配置案
jconsole是JDK里自带的一个工具,可以监测Java程序运行时所有对象的申请.释放等动作,将内存管理的所有信息进行统计.分析.可视化.我们可以根据这些信息判断程序是否有内存泄漏问题. 使用jco ...
- 分析一个react项目
目录结构 下面是使用npx create-react-app web-app来创建的一个项目(已经删除了多余的文件) web-app ├── node_modules │ ├── ....... ...
- Git Gerrit Code Review
Gerrit Code Review | Gerrit Code Reviewhttps://www.gerritcodereview.com/
- Jenkins deploy war to tomcat over https
ssl - HTTPS login with Spring Security redirects to HTTP - Stack Overflow https://stackoverflow.com/ ...