繁华模拟赛day8 字典序



/*
这个题要我们求一个字典序,字符串给出的顺序,会对字母的字典序前后相对顺序进行限定,如何用来表示这种限定,我们注意到这种一个之后接着一个,只有先输出他前面的才能输出他,很明显就是拓扑排序,最小方案只要优先队列随便搞一搞就行了
*/
#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 字典序的更多相关文章
- 繁华模拟赛day8 牛栏
/* 标称并没有用到题解中提到的那种奇妙的性质,我们可以证明,正常从1开始走的话,需要T次,如何使这个次数减小?题解中提到一个办法,有一步小于n/t,我们考虑这一步,如果把它匀到左右两步中,则可以减小 ...
- 繁华模拟赛day8 科技树
/* 贪心,很明显是越容易升级的越先升级 */ #include<iostream> #include<cstdio> #include<string> #incl ...
- [繁华模拟赛]Evensgn 剪树枝
Evensgn 剪树枝 题目 繁华中学有一棵苹果树.苹果树有 n 个节点(也就是苹果),n − 1 条边(也就 是树枝).调皮的 Evensgn 爬到苹果树上.他发现这棵苹果树上的苹果有两种:一 种是 ...
- 繁华模拟赛 Vicent坐电梯
/*n<=5000这样就不能用O(n)的转移了,而是要用O(1)的转移.注意我们每次的转移都来自一个连续的区间,而且我们是求和区间求和?前缀和!令sum[step][i]表示f[ste ...
- 繁华模拟赛 Vincent的城堡
#include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...
- 繁华模拟赛 vicent的字符串
#include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...
- 繁华模拟赛 Evensgn剪树枝
#include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...
- 繁华模拟赛 Evensgn玩序列
#include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...
- 繁华模拟赛 Evensgn的债务
#include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...
随机推荐
- Linux安装pdo_mysql模块
网站不能访问 查看apache日志 PHP Fatal error: Uncaught exception 'PDOException' with message 'could not find dr ...
- jpa findOne()用法
findOne(Interger id) 如果实体类没有id这个属性的话是会报错的 改别人的代码神烦...
- bash的变量设置
1. myname=zhangsan //设置变量 2. echo $myname //显示变量 或者:echo ${myname} 3. myname="my name is $mynam ...
- 加州大学伯克利分校Stat2.2x Probability 概率初步学习笔记: Section 4 The Central Limit Theorem
Stat2.2x Probability(概率)课程由加州大学伯克利分校(University of California, Berkeley)于2014年在edX平台讲授. PDF笔记下载(Acad ...
- 【转载】MFC键盘响应
转载自:传送门 一:首先介绍键盘消息 系统消息: ALT,F1,--F24等,是由系统内部处理的,程序本身就存在,比如F1是帮助键. WM_SYSKEYDOWN WM_SYSKEYUP WM_SYSC ...
- Android shell 下 busybox,clear,tcpdump、、众多命令的移植
http://my.oschina.net/mopidick/blog/167372 作为一名Android开发者,经常需要进入Android 的shell终端运行一些命令.但是我们发现Android ...
- 说说css3布局
使用float属性或position属性布局的缺点 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml&qu ...
- 安装和使用Linux花生壳(公网版)
一.安装说明 1.下载相应的安装包,安装程序 2.运行程序.配置程序(默认使用/etc/phlinux.conf,如果不存在这个文件则自动进入交互配置) [root@localhost -]# phd ...
- BZOJ4590: [Shoi2015]自动刷题机
显然看着就是二分,仔细看的话显然刷的题数随n增大单调不升. 挂了一发是因为无解输出一个-1而不是两个…… #include<cstdio> #include<algorithm> ...
- cocos2d 2.2.6 win7下的配置
我搭建cocos2.6的开发环境需要安装工具包括: 1.Visual Studio 2012(由于不兼容win7,需要安装Update 4)和虚拟光驱daemon tool,虚拟光驱的下载地址:htt ...