题意:给你许多字符串,你可以改变字母序大小,问有哪些字符串可能成为字典序最小的字符串。

我们考虑把这些字符串都塞到\(trie\)树上。之后检索每一个字符串的时候,我们看和他同一层的地方是否有字符,如果有,我们就从他到同层字符连一条有向边,因为只有同层字符妨碍他可能会成为第一。之后进行拓扑排序,检查这种情况是否可以存在即可。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue> using namespace std; int n,len,ans,tot,cnt;
int trie[300900][130],op[30090],head[100],du[100];
bool end[300090];
string tmp;
string dic[30090];
struct node{
int to,next;
}edge[1000]; void insert()
{
int p=0;
for(int i=0;i<len;i++)
{
int qwq=tmp[i]-'a';
if(!trie[p][qwq]) trie[p][qwq]=++tot;
p=trie[p][qwq];
}
end[p]=1;
} void add(int x,int y)
{
edge[++cnt].to=y;
edge[cnt].next=head[x];
head[x]=cnt;du[y]++;
} bool work()
{
int p=0;
for(int i=0;i<len;i++)
{
int qwq=tmp[i]-'a';
if(end[p]) return 0;
for(int j=0;j<26;j++)
if(trie[p][j]&&qwq!=j) add(qwq,j);
p=trie[p][qwq];
}
return 1;
} bool topsort()
{
queue<int>q;
for(int i=0;i<26;i++)
if(du[i]==0) q.push(i);
while(!q.empty())
{
int u=q.front();q.pop();
for(int i=head[u];i;i=edge[i].next)
{
int v=edge[i].to;
if(--du[v]==0) q.push(v);
}
}
for(int i=0;i<26;i++)
if(du[i]) return 0;
return 1;
} void clear()
{
memset(du,0,sizeof(du));
cnt=0;
memset(head,0,sizeof(head));
} int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
cin>>dic[i];
len=dic[i].length();
tmp=dic[i];
insert();
}
for(int i=1;i<=n;i++)
{
tmp=dic[i];len=dic[i].length();
if(work()&&topsort()) op[++ans]=i;
clear();
}
printf("%d\n",ans);
for(int i=1;i<=ans;i++)
cout<<dic[op[i]]<<endl;
return 0;
}

注意几点:连边的\(tot\)和字典树的指针\(cnt\)开始用混了。然后不管判断是否成功,每次都要清空(可能跑拓扑判定了)

Luogu P3065 [USACO12DEC]第一!First!【字典树/拓扑排序】By cellur925的更多相关文章

  1. [USACO12DEC]第一!First! (Trie树,拓扑排序)

    题目链接 Solution 感觉比较巧的题啊... 考虑几点: 可以交换无数次字母表,即字母表可以为任意形态. 对于以其他字符串为前缀的字符串,我们可以直接舍去. 因为此时它所包含的前缀的字典序绝对比 ...

  2. 「Usaco2012 Dec」第一(字典树+拓扑排序)

    (我恨字符串) 惯例化简题目:给定n个字符串,可以改变字符的相对大小(在字典序中的大小),问:字符串i是否能成为最小的字符串(字典序) 解题过程: 首先你可以预处理出来26的全排列然后暴力然后你只要用 ...

  3. [luogu P3065] [USACO12DEC]第一!First!

    [luogu P3065] [USACO12DEC]第一!First! 题目描述 Bessie has been playing with strings again. She found that ...

  4. 洛谷P3065 [USACO12DEC]第一!First!(Trie树+拓扑排序)

    P3065 [USACO12DEC]第一!First! 题目链接:https://www.luogu.org/problemnew/show/P3065 题目描述 Bessie一直在研究字符串.她发现 ...

  5. P3065 [USACO12DEC]第一!First!

    题目描述 Bessie has been playing with strings again. She found that by changing the order of the alphabe ...

  6. BZOJ_3012_[Usaco2012 Dec]First!_trie树+拓扑排序

    BZOJ_3012_[Usaco2012 Dec]First!_trie树+拓扑排序 题意: 给定n个总长不超过m的互不相同的字符串,现在你可以任意指定字符之间的大小关系.问有多少个串可能成为字典序最 ...

  7. Trie树|字典树(字符串排序)

    有时,我们会碰到对字符串的排序,若采用一些经典的排序算法,则时间复杂度一般为O(n*lgn),但若采用Trie树,则时间复杂度仅为O(n). Trie树又名字典树,从字面意思即可理解,这种树的结构像英 ...

  8. BZOJ3832[Poi2014]Rally——权值线段树+拓扑排序

    题目描述 An annual bicycle rally will soon begin in Byteburg. The bikers of Byteburg are natural long di ...

  9. BZOJ4383 Pustynia(线段树+拓扑排序)

    线段树优化建图暴力拓扑排序即可.对于已确定的数,拓扑排序时dp,每个节点都尽量取最大值,如果仍与已确定值矛盾则无解.叶子连出的边表示大于号,其余边表示大于等于. #include<iostrea ...

随机推荐

  1. Android Weekly Notes Issue #276

    September 24th, 2017 Android Weekly Issue #276 本期内容包括LifeCycle与Architecture的相关文章,以及新的JSON解析库Moshi的介绍 ...

  2. LightOJ - 1274 Beating the Dataset —— 期望

    题目链接:https://vjudge.net/problem/LightOJ-1274 1274 - Beating the Dataset    PDF (English) Statistics ...

  3. Contiki Rtimer 模块

    一.rtimer概述 The Contiki rtimer library provides scheduling and execution of real-time tasks (with pre ...

  4. 左侧浮动html网页模板

    左侧浮动html网页模板是一款左侧导航菜单随网页滚动的html网站模板. 地址:http://www.huiyi8.com/sc/10617.html

  5. listen and translation exercise 52

    Two years ago my husband bought me a bicycle. Twenty of the thirty-five years I ran my own business. ...

  6. python之django入门

    一.搭建开发环境 使用virualenv创建虚拟python环境 pip install virtualenv [root@master djiango]# find / -name virtuale ...

  7. 瞎写的树dfs序

    这里枚举了树的DFS序来解决树上问题的多个板子,自己最好多看看. ↓改↓ ↓求↓ 点 点 ————————>>>这个就算了 点 树 简单, BIT 点 链 重点! 树 树 简单, 线 ...

  8. 使用django-extension扩展django的manage――runscript命令

    摘要:1.下载安装   1)$easy_installdjango-extensions   2)在INSTALLED_APP中添加'django_extensions'[python]INSTALL ...

  9. C++ 创建文件的方法

    CString getPath(){ CTime time = CTime::GetCurrentTime(); CString t = time.Format(_T("%Y%m%d%H%M ...

  10. hadoop之一:概念和整体架构

    什么是hadoop? Apache Hadoop是一款支持数据密集型分布式应用并以Apache 2.0许可协议发布的开源软件框架.它支持在商品硬件构建的大型集群上运行的应用程序.Hadoop是根据Go ...