繁华模拟赛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 ...
随机推荐
- TYVJ1982 武器分配
描述 后勤部队运来一批武器(机枪和盔甲).你要把这些武器分配给手下的marine们(每人一部机枪,一套盔甲).可是问题来了... 这些武器的型号不相同(武器是由出价最低的承包商制造的), ...
- Bzoj1711 [Usaco2007 Open]Dining吃饭
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 872 Solved: 459 Description 农夫JOHN为牛们做了很好的食品,但是牛吃饭很挑食 ...
- [NOIP2014] 提高组 洛谷P1328 生活大爆炸版石头剪刀布
题目描述 石头剪刀布是常见的猜拳游戏:石头胜剪刀,剪刀胜布,布胜石头.如果两个人出拳一样,则不分胜负.在<生活大爆炸>第二季第8 集中出现了一种石头剪刀布的升级版游戏. 升级版游戏在传统的 ...
- ( 译、持续更新 ) JavaScript 上分小技巧(一)
感谢好友破狼提供的这篇好文章,也感谢写这些知识点的作者们和将他们整理到一起的作者.这是github上的一篇文章,在这里本兽也就只做翻译,由于本兽英语水平和编程能力都不咋地,如有不好的地方也请多理解体谅 ...
- (译)你应该知道的jQuery小技巧
帮助提高你jQuery应用的简单小技巧 回到顶部按钮 图片预加载 判断图片是否加载完 自动修补破损图像 Hover切换class类 禁用输入 停止正在加载的链接 toggle fade/slide 简 ...
- 【Alpha版本】 第十天 11.18
一.站立式会议照片: 二.项目燃尽图: 三.项目进展: 成 员 昨天完成任务 今天完成任务 明天要做任务 问题困难 心得体会 胡泽善 完成管理员的三大功能界面框架, 我要招聘查看报名者的列表显示 完成 ...
- mysql数据库创建函数过程
1.创建mysql数据库的存储过程,语句 2.选择执行创建的数据库存储过程即可
- electron-Node.js Error: Module version mismatch. Expected
本来以为是nodejs的版本问题,后来才发现是electron用本地包的问题 Installing modules and rebuilding for Electron You can also c ...
- VIM for Python and Django Development
VIM for Python and Django Development VIM-PyDjango created by Programmer for Programmer who work on ...
- jquery id选择器 id带"."问题
例如控件ID为user.id 使用$("#user.id")不能得到正确的结果 必须使用\\转义 即$("#user\\.id")