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. GitLab Runner

    GitLab Runner是一个开源项目,用于运行你的作业(jobs)并将结果发送回GitLab.它与GitLab CI结合使用,GitLab CI是GitLab用于协调jobs的开源持续集成服务. ...

  2. hadoop3自学入门笔记(3)-java 操作hdfs

    1.core-site.xml <configuration> <property> <name>fs.defaultFS</name> <val ...

  3. SQL Server等待事件—PAGEIOLATCH_EX

    什么是PAGEIOLATCH_EX等待事件? 下面我们将对PAGEIOLATCH_EX等待事件的相关资料做一个简单的归纳.整理.关于PAGEIOLATCH_EX,官方文档的简单介绍如下: PAGEIO ...

  4. nginx基础(一)

    一.nginx的安装.启动.停止及文件解读 yum -y install gcc gcc-c++ autoconf pcre-devel make automake yum -y install wg ...

  5. LAMP环境搭建+配置虚拟域名

    Centos下PHP,Apache,Mysql 的安装 安装Apache yum -y install httpd systemctl start httpd 添加防火墙 firewall-cmd - ...

  6. ggEditor流程图增加网格背景

    参考官方文档: https://www.yuque.com/antv/g6/plugin.tool.grid react-ggEditor如何使用 import { Flow } from 'gg-e ...

  7. NIO学习笔记,从Linux IO演化模型到Netty—— Java NIO零拷贝

    同样只是大致上的认识. 其中,当使用transferFrom,transferTo的时候用的sendfile(). 如果系统内核不支持 sendfile,进一步执行 transferToTrusted ...

  8. js有关字符串拼接问题

    我们经常写代码要遇见要拼接字符串,比如说我们要把     "yyy" 和一个动态数字拼接,接下来我们怎么办? 其实我们都会想到直接用“yyy”  + 一个数字不就可以了吗? 对的, ...

  9. Flex布局如何实现最后一个元素右对齐,或者第一个元素左对齐

    先来看看一个例子 在一个div我们把四个按钮全部放到右边去了,看下效果↓ 这个时候我们想把第一个按钮左对齐,其他保持不变 这时候我们来个第一个按钮样式上加上 :margin-right: auto; ...

  10. 聊聊智能指针 auto_ptr、shared_ptr、weak_ptr和unique_ptr

    本文为转载:https://www.cnblogs.com/zeppelin5/p/10083597.html,对作者有些地方做了修正. 手写代码是理解C++的最好办法,以几个例子说明C++四个智能指 ...