Description

Luogu3065

Solution

首先,一个串要是最小的,别的串不能是它的前缀,且和它有相同前缀的串字典序都比他小。

Trie树是显然要用的,难点在于如何判断能否最小。其实这之中蕴含了字母间的大小关系,即同一层中,钦定的最小串的字符要比其它的大。然后就可以建一个有向图跑拓扑排序判环就行。

Code

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector> namespace wyx { typedef int ll; ll read() {
ll ans = 0, fl = 1;
char c;
for (c = getchar(); c > '9' || c < '0'; c = getchar())
if (c == '-') fl = -1;
ans = c - '0';
for (c = getchar(); c >= '0' && c <= '9'; c = getchar())
ans = ans * 10 + c - '0';
return fl * ans;
} typedef double ld;
typedef unsigned long long ull;
const int N = 30010; struct Trie {
int to[N * 10][26], cnt, val[N * 10];
Trie() {
memset(to, 0, sizeof to);
memset(val, 0, sizeof val);
cnt = 0;
}
void add(const std::string& s) {
int len = s.length();
int now = 0;
for (int i = 0; i < len; ++i) {
if (!to[now][s[i] - 'a']) to[now][s[i] - 'a'] = ++cnt;
now = to[now][s[i] - 'a'];
}
val[now]++;
}
} Tr;
struct Graph {
std::vector<int> g[26];
int deg[26];
void init() {
for (int i = 0; i < 26; ++i) g[i].clear(), deg[i] = 0;
}
void adde(int x, int y) {
g[x].push_back(y);
deg[y]++;
}
bool toposort() {
std::queue<int> q;
for (int i = 0; i < 26; ++i)
if (deg[i] == 0) q.push(i);
while (!q.empty()) {
int x = q.front();
q.pop();
for (int i : g[x])
if (--deg[i] == 0) q.push(i);
}
for (int i = 0; i < 26; ++i)
if (deg[i]) return false;
return true;
}
} G;
std::string s[N];
int n, vis[N]; bool work(int x) {
int now = 0, len = s[x].length();
for (int i = 0; i < len; ++i) {
if (Tr.val[now]) return false;
for (int j = 0; j < 26; ++j) {
if (j != s[x][i] - 'a' && Tr.to[now][j]) G.adde(s[x][i] - 'a', j);
}
now = Tr.to[now][s[x][i] - 'a'];
}
return true;
} void main() {
n = read();
for (int i = 1; i <= n; ++i) std::cin >> s[i], Tr.add(s[i]);
int cnt = 0;
for (int i = 1; i <= n; ++i) {
G.init();
if (work(i) && G.toposort()) {
vis[i] = 1;
cnt++;
}
}
printf("%d\n", cnt);
for (int i = 1; i <= n; ++i)
if (vis[i]) {
std::cout << s[i] << std::endl;
}
} }; // namespace wyx int main() {
wyx::main();
return 0;
}

[USACO12DEC]First!的更多相关文章

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

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

  2. [USACO12DEC] 逃跑的BarnRunning Away From…(主席树)

    [USACO12DEC]逃跑的BarnRunning Away From- 题目描述 It's milking time at Farmer John's farm, but the cows hav ...

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

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

  4. luoguP3066 [USACO12DEC]逃跑的BarnRunning

    luoguP3066 [USACO12DEC]逃跑的BarnRunning 题目大意 给定一棵n个节点的树和参数L,查询每个节点子树中到达该节点距离<=L的数量(包括该节点) 偏模板的主席树 P ...

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

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

  6. [bzoj3012][luogu3065][USACO12DEC][第一!First!] (trie+拓扑排序判环)

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

  7. 洛谷 P3063 [USACO12DEC]牛奶的路由Milk Routing

    P3063 [USACO12DEC]牛奶的路由Milk Routing 题目背景 征求翻译.如果你能提供翻译或者题意简述,请直接发讨论,感谢你的贡献. 题目描述 Farmer John's farm ...

  8. [USACO12DEC]第一!First!(字典树,拓扑排序)

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

  9. P3065 [USACO12DEC]First! G

    题意描述 [USACO12DEC]First! G 不错的一道题. 给你 \(N\) 个字符串,要求你求出可能的字典序最小的字符串. 对于 可能的最小的字符串,你可以任意排列 \(26\) 个字母,使 ...

  10. [Luogu3066][USACO12DEC]逃跑的BarnRunning Away From…

    题面 题目描述 给出以1号点为根的一棵有根树,问每个点的子树中与它距离小于等于l的点有多少个. 输入格式: Line 1: 2 integers, N and L (1 <= N <= 2 ...

随机推荐

  1. mysql随机抽取数据

      -- 慢 ; -- 较慢 SELECT * FROM `table` WHERE id >= (SELECT floor( RAND() * ((SELECT MAX(id) FROM `t ...

  2. 彻底理解Windows认证1

    彻底理解Windows认证 一.Windows本地认证 1. 我的密码在哪里? 路径:C:\Windows\System32\config\SAM 当我们登录系统的时候,系统会自动的读取SAM文件中的 ...

  3. 架构师成长系列 | 从 2019 到 2020,Apache Dubbo 年度回顾与总结

    作者 | 刘军(陆龟)Apache Dubbo PMC 本文整理自架构师成长系列 2 月 18 日直播课程. 关注"阿里巴巴云原生"公众号,回复 "218",即 ...

  4. CentOS7 GlusterFS文件系统部署

    一.GlusterFS简介 GlusterFS(GNU ClusterFile System)是一种全对称的开源分布式文件系统,所谓全对称是指GlusterFS采用弹性哈希算法,没有中心节点,所有节点 ...

  5. PMP--3. 项目启动过程组

    ####################################################### 从第三章开始,我正式进入项目过程,启动.规划.执行.监控.收尾五大过程组的具体在之后依次 ...

  6. mybatis 配置--->确认jar包是否正确

    mybatis 配置之前,首先要确保服务器jar包是否成功 配置jar包如下添加mybaties-3.5.2. jar, maven 的 pom.xml 配置如下,查看配置是否成功见 如上分类 Mav ...

  7. 指数ETF基金的组合分析方法初探

    本文在Creative Commons许可证下发布 试想一下,大多数基金“推荐”的配置策略都假设某种股票/债券组合.如果我们想寻求成本最小收益最高的组合(以yahoo finance上的数据来分析,因 ...

  8. 文件下载(download) 前后台备注自用

    1.后台 1.1 ResponseBean 返回封装的格式 @Data @Accessors(chain = true) public class ResponseBean { private Str ...

  9. 关于iScroll在安卓移动端/chrome模拟移动端上下滑动卡顿问题处理!!!!真实可靠!!!已解决!!!

    滑动卡顿效果 安卓手机打开微信浏览网页,Chrome模拟手机浏览网页,都出现的问题滑动卡顿! 修改代码点: 1.    <style type="text/css">  ...

  10. openlayers轨迹匀速播放

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...