/*
这个题要我们求一个字典序,字符串给出的顺序,会对字母的字典序前后相对顺序进行限定,如何用来表示这种限定,我们注意到这种一个之后接着一个,只有先输出他前面的才能输出他,很明显就是拓扑排序,最小方案只要优先队列随便搞一搞就行了
*/
#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. poj 2653 线段相交

    题意:一堆线段依次放在桌子上,上面的线段会压住下面的线段,求找出没被压住的线段. sol:从下向上找,如果发现上面的线段与下面的相交,说明被压住了.break掉 其实这是个n^2的算法,但是题目已经说 ...

  2. Bzoj1449 [JSOI2009]球队收益

    Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 741  Solved: 423 Description Input Output 一个整数表示联盟里所有球 ...

  3. UVa 1328 Period

    数据范围较大,故用KMP求循环节 之后由小到大枚举长度范围,若该长度下有循环节就输出答案 还要注意输出格式.之前测试时候连着一串presentation error也是悲伤 #include<b ...

  4. MVC5-5 Razor引擎及视图结构

    View结构 其实给我们提供了官方的MvcDemo,就是在我们直接去新建一个不为空的MVC项目. 这里就是一个MVC的Demo了,可以看一下这个Demo中View的结构是什么 上图可以发现,有一个Sh ...

  5. UVA 10679 I Love Strings

    传送门 题目大意 给定文本串$S$和若干模式串$\{T\}$, 对每个模式串$T$, 询问$T$是否为$S$的子串. Solution 裸的AC自动机, 也可以用后缀数组做. P.S. 这题数据很弱, ...

  6. tcpdump抓包命令

    本文转自 : http://www.cnblogs.com/ggjucheng/archive/2012/01/14/2322659.html http://www.itshouce.com.cn/l ...

  7. linux 相关快捷键

    linux 相关快捷键 http://linux.chinaunix.net/begin/2004-10-05/34.shtml#_Toc41417098 1.使用虚拟控制台登录后按“Alt+F2”键 ...

  8. C# unmanaged function pointers for iOS

    C# unmanaged function pointers for iOS Just a reminder to myself when I need this thing next time fo ...

  9. ecshop 点击领取红包

    思路: 1.领取红包:也就是传递一个红包ID,和user_id然后update更新过来 2.获取未领取过的红包列表:然后随机函数array_rand($arr,1),随机返回一个数组,从而获得红包列表 ...

  10. centos 7 DenyHosts 安装 防暴力破解ssh登陆

    为了减少软件扫描ssh登陆 还是用这个比较好点  默认端口号22 也要改 登陆密码也不要使用 弱口令 123456 这样的 Description DenyHosts is a python prog ...