/*
这个题要我们求一个字典序,字符串给出的顺序,会对字母的字典序前后相对顺序进行限定,如何用来表示这种限定,我们注意到这种一个之后接着一个,只有先输出他前面的才能输出他,很明显就是拓扑排序,最小方案只要优先队列随便搞一搞就行了
*/
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn = ;
struct edge{
int v;
int nxt;
}e[maxn*];
int n,l[maxn],ans[maxn],orz;
int head[maxn],cnt,du[maxn];
char sb[maxn][maxn];
priority_queue< int , vector<int> , greater<int> > q;
int read(){
char ch=getchar();
int x=,f=;
while(!(ch>=''&&ch<='')){if(ch=='-')f=-;ch=getchar();};
while(ch>=''&&ch<=''){x=x*+(ch-'');ch=getchar();};
return x*f;
}
void ins(int u,int v){
cnt++;
e[cnt].v = v;
e[cnt].nxt = head[u];
head[u] = cnt;
}
void build(){
int j;
for(int i = ;i <= n;i++){
j = ;
while(j <= l[i-] && j <= l[i] && sb[i][j] == sb[i-][j]) j++;
if(j > l[i-] || j > l[i] || sb[i][j] == sb[i-][j]) continue;
ins(sb[i-][j]-'a',sb[i][j]-'a');
du[sb[i][j]-'a']++;
}
}
bool topo(){
int u,to;
for(int i = ;i < ;i++){
if(!du[i]) q.push(i);
}
while(!q.empty()){
ans[++orz] = q.top();
q.pop();
u = ans[orz];
for(int i = head[u];i;i=e[i].nxt){
to = e[i].v;
du[to]--;
if(!du[to]){
q.push(to);
}
}
}
return orz == ;
}
int main(){
freopen("lexicographical.in","r",stdin);
freopen("lexicographical.out","w",stdout);
n = read();
for(int i = ;i <= n;i++){
scanf("%s",sb[i]+);
l[i] = strlen(sb[i]+);
}
build();
if(!topo()) cout<<"Impossible";
else{
for(int i = ;i <= ;i++){
char tmp = ans[i] + 'a';
cout<<tmp;
}
}
return ;
}

繁华模拟赛day8 字典序的更多相关文章

  1. 繁华模拟赛day8 牛栏

    /* 标称并没有用到题解中提到的那种奇妙的性质,我们可以证明,正常从1开始走的话,需要T次,如何使这个次数减小?题解中提到一个办法,有一步小于n/t,我们考虑这一步,如果把它匀到左右两步中,则可以减小 ...

  2. 繁华模拟赛day8 科技树

    /* 贪心,很明显是越容易升级的越先升级 */ #include<iostream> #include<cstdio> #include<string> #incl ...

  3. [繁华模拟赛]Evensgn 剪树枝

    Evensgn 剪树枝 题目 繁华中学有一棵苹果树.苹果树有 n 个节点(也就是苹果),n − 1 条边(也就 是树枝).调皮的 Evensgn 爬到苹果树上.他发现这棵苹果树上的苹果有两种:一 种是 ...

  4. 繁华模拟赛 Vicent坐电梯

    /*n<=5000­这样就不能用O(n)的转移了,而是要用O(1)的转移.­注意我们每次的转移都来自一个连续的区间,而且我们是求和­区间求和?­前缀和!­令sum[step][i]表示f[ste ...

  5. 繁华模拟赛 Vincent的城堡

    #include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...

  6. 繁华模拟赛 vicent的字符串

    #include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...

  7. 繁华模拟赛 Evensgn剪树枝

    #include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...

  8. 繁华模拟赛 Evensgn玩序列

    #include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...

  9. 繁华模拟赛 Evensgn的债务

    #include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...

随机推荐

  1. python 处理CSV数据

    从CS中导入数据 Python中有一个CSV模块支持读写各种方言格式的CSV文件.方言是很重要的,因为没有一个同意的CSV标准,不同的应用实现CSV的方式略有不同,当看到文件的内容的时候你往往很容易第 ...

  2. 时间日期----java

    Date类 在JDK1.0中,Date类是唯一的一个代表时间的类,但是由于Date类不便于实现国际化,所以从JDK1.1版本开始,推荐使用Calendar类进行时间和日期处理.这里简单介绍一下Date ...

  3. Nginx个人简单理解

    首先我们来补充下一些基本知识: 什么是代理服务器? 先举个简单的例子,现在我们在百度访问谷歌的网站,发现现在进不去,这个时候我们可以FQ(关于FQ,可以借鉴下这个博文:http://zhangge.n ...

  4. [NOIP2014] 解方程&加强版 (bzoj3751 & vijos1915)

    大概有$O(m)$,$O(n\sqrt{nm})$,$O(n\sqrt{m})$的3个算法,其中后2个可以过加强版.代码是算法3,注意bzoj的数据卡掉了小于20000的质数. #include< ...

  5. Wget下载终极用法和15个详细的例子

    Wget下载终极用法和15个详细的例子 备注:wget  不支持https 下载,也没有相关https参数,当下载https的时候或以改用 axelWget是一种很好用的因特网下载工具,他具有的很多特 ...

  6. hdu 1873 看病要排队(优先级队列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1873 题目大意: 三个医生看病,病人排队看病,病人有优先级,优先级高的提前看病,同样的优先级按先后.I ...

  7. SRS(simple-rtmp-server)

     1.Push h.264 to rtmp server from tcp.

  8. centos忘记开机密码

    系统:centos6.6,忘记开机密码,进入单用户模式进行重置,以下为操作过程. 1. reset(重启)Linux系统,在出现如下图的界面时,请点Enter键,确保一定要快,只存在几秒.. 2.点击 ...

  9. nginx和fpm的进程数配置和502,504错误

    502 和 php-fpm.conf 1.php-cgi进程数不够用.php执行时间长,导致没有空闲进程处理新请求. 2.php-cgi进程死掉.php-fpm超时时间短,当前进程执行超时关闭连接. ...

  10. How to convert any valid date string to a DateTime.

    DateTimeFormatInfo pattern = new DateTimeFormatInfo() { ShortDatePattern = "your date pattern&q ...