Codeforces题号:#510C

出处: 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的更多相关文章

  1. 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 ...

  2. C. Fox And Names

    C. Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  3. CF Fox And Names (拓扑排序)

    Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  4. codeforce 510C Fox And Names(拓扑排序)

    Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  5. (CodeForces 510C) Fox And Names 拓扑排序

    题目链接:http://codeforces.com/problemset/problem/510/C Fox Ciel is going to publish a paper on FOCS (Fo ...

  6. Fox And Names

    Description Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce ...

  7. [CF #290-C] Fox And Names (拓扑排序)

    题目链接:http://codeforces.com/contest/510/problem/C 题目大意:构造一个字母表,使得按照你的字母表能够满足输入的是按照字典序排下来. 递归建图:竖着切下来, ...

  8. 拓扑排序 Codeforces Round #290 (Div. 2) C. Fox And Names

    题目传送门 /* 给出n个字符串,求是否有一个“字典序”使得n个字符串是从小到大排序 拓扑排序 详细解释:http://www.2cto.com/kf/201502/374966.html */ #i ...

  9. codeforces 510 C Fox And Names【拓扑排序】

    题意:给出n串名字,表示字典序从小到大,求符合这样的字符串排列的字典序 先挨个地遍历字符串,遇到不相同的时候,加边,记录相应的入度 然后就是bfs的过程,如果某一点没有被访问过,且入度为0,则把它加入 ...

随机推荐

  1. 朱晔的互联网架构实践心得S1E8:三十种架构设计模式(下)

    朱晔的互联网架构实践心得S1E8:三十种架构设计模式(下) [下载本文PDF进行阅读] 接上文,继续剩下的15个模式. 数据管理模式 16.分片模式:将数据存储区划分为一组水平分区或分片 一直有一个说 ...

  2. (通用版)salesforce中soql及sosl的伪‘Like’模糊检索

    salesforce里有soql.sosl两种查询语法,soql针对模糊搜索也有‘like’关键字,然而只能针对其自带字段如:Name.Id:对于自定义添加的字段如:Message__c.Note__ ...

  3. Linq中比较字符串类型的日期

    一.在使用Linq时,想要比较字符串类型的日期时,参考以下: SQL语句: )select * from TableName where StartTime > '2015-04-08' )se ...

  4. nginx 1.4.3能直接升到1.8.1吗

    nginx 1.4.3能直接升到1.8.1吗_百度知道https://zhidao.baidu.com/question/564529441847261484.html nginx-1.6.3平滑升级 ...

  5. pip ipython启动错误 Fatal error in launcher: Unable to create process using

    完整的错误提示: C:\Users\yyy>ipython3Fatal error in launcher: Unable to create process using '"c:\u ...

  6. Pseudo Registers

    Pseudoregister Description @ERR Last error value; the same value returned by the GetLastError() API ...

  7. 练习MD5加密jar包编写

    简介 参数签名可以保证开发的者的信息被冒用后,信息不会被泄露和受损.原因在于接入者和提供者都会对每一次的接口访问进行签名和验证. 签名sign的方式是目前比较常用的方式. 第1步:接入者把需求访问的接 ...

  8. scroll滚动条样式修改

    一般我们有两种情况会出现滚动条,一种是overflow,一种是使用scroll. 当我们需要改变这个滚动条样式的时候,我们需要做以下的修改: html: <div id="style- ...

  9. css引入的两种方法link和@import的区别和用法

    link和@import都是HTML中引入CSS的语法单词. 两者的基本语法 link语法结构 <link href="外部CSS文件的URL路径" rel="st ...

  10. Java多线程6:Synchronized锁代码块(this和任意对象)

    一.Synchronized(this)锁代码块 用关键字synchronized修饰方法在有些情况下是有弊端的,若是执行该方法所需的时间比较长,线程1执行该方法的时候,线程2就必须等待.这种情况下就 ...