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. Java基础之一、入门知识

    资料来源于<明解 Java>日本作者 写的很详细 1:命令---java 类名 该命令不是去执行类名.class文件(文件名和类名有可能不一致),切记是表示执行具体的类: 2:“字符”+数 ...

  2. Jenkins集成jacoco收集集成测试覆盖率

    Jenkins集成jacoco收集集成测试覆盖率 2020-02-28 目录 0 整体思路1 安装版本2 全局工具配置3 Jenkins创建JacocoIntegrateTestDemo项目  3.1 ...

  3. 最全的计算机Java毕业设计题目大全 附 源码

    本文提供数百个计算机毕设题目可以参考 并提供成品源码下载,都是从网上收集而来 源码技术全部采用java+MySQL开发,并结合了其他技术如ssm,ssh,jsp等等. 下载链接在文末! 以下是项目名称 ...

  4. 第3章 关系数据库标准语言SQL(重点) | 数据库知识点整理

    第3章 关系数据库标准语言SQL(重点) 了解 SQL语言发展过程 关系数据库技术和关系数据库管理系统RDBMS产品的发展过程 掌握 SQL语言的特点和优点 面向过程的语言和SQL语言的区别 关系数据 ...

  5. java-局部变量,成员变量区别

    1. 内存中的位置 成员变量: 堆内存 局部变量: 栈内存 2. 生命周期 成员变量:随着对象的创建而存在,随着对象的消失而消失 局部变量:随着方法的调用而存在,随着方法调用完毕而消失 3. 注意事项 ...

  6. linq行转列

    using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Text; ...

  7. linux安装Nginx 以及 keepalived 管理Nginx

    linux安装Nginx 1.1将Nginx素材内容上传到/usr/local目录(pcre,zlib,openssl,nginx)(注意:必须登录用对这个文件具有操作权限的) 1.2安装pcre库 ...

  8. MVC开发之Razor的使用

    一.运用布局和视图起始文件 布局的使用,可以简化视图,允许用户创建能够运用于多个视图的通用HTML,并使得维护更加容易.下面是一个简单的例子: 域模型类: public class Product { ...

  9. STM32学习笔记 —— 0.1 Keil5安装和DAP仿真下载器配置的相关问题与注意事项

    Keil5安装的注意事项 安装细节在此不再做过多赘述,主要介绍一下注意事项: 安装路径中不能有中文. ARM的Keil的路径不能与51的Keil的有冲突,必须将目录分开. Keil5中不会自动添加芯片 ...

  10. Android 开发调试最优选项

    1 开发环境 VS2019 16.4.5 2 开发调试选项 Android 选项