牛客练习赛11 假的字符串 (Trie树+拓扑找环)
牛客练习赛11 假的字符串 (Trie树+拓扑找环)
链接:https://ac.nowcoder.com/acm/problem/15049
来源:牛客网
给定n个字符串,互不相等,你可以任意指定字符之间的大小关系(即重定义字典序),求有多少个串可能成为字典序最小的串,并输出它们
题解:对于第i个字符串来说,如果有一个串是他的前缀,那么这个前缀的字典序重定义后是肯定比他小的,所以我们用trie树保存前缀
对于当前字符串,从该字符串的第i个字母向其父亲节点上的其他字母连边,表示存在大小关系\(str[i]>str[others]\) 这样就指定了 第i个字母的与其他字母的大小关系了,如果当前关系成立的话,就不会出现a>b>a这样的情况 即不会出现环的情况,所以我们用拓扑排序判断是否有环即可
/**
* ┏┓ ┏┓
* ┏┛┗━━━━━━━┛┗━━━┓
* ┃ ┃
* ┃ ━ ┃
* ┃ > < ┃
* ┃ ┃
* ┃... ⌒ ... ┃
* ┃ ┃
* ┗━┓ ┏━┛
* ┃ ┃ Code is far away from bug with the animal protecting
* ┃ ┃ 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*/
// warm heart, wagging tail,and a smile just for you!
//
// _ooOoo_
// o8888888o
// 88" . "88
// (| -_- |)
// O\ = /O
// ____/`---'\____
// .' \| |// `.
// / \||| : |||// \
// / _||||| -:- |||||- \
// | | \ - /// | |
// | \_| ''\---/'' | |
// \ .-\__ `-` ___/-. /
// ___`. .' /--.--\ `. . __
// ."" '< `.___\_<|>_/___.' >'"".
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
// \ \ `-. \_ __\ /__ _/ .-` / /
// ======`-.____`-.___\_____/___.-`____.-'======
// `=---='
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// 佛祖保佑 永无BUG
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const int maxn = 2e5 + 5 + 3e4;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double Pi = acos(-1);
LL gcd(LL a, LL b) {
return b ? gcd(b, a % b) : a;
}
LL lcm(LL a, LL b) {
return a / gcd(a, b) * b;
}
double dpow(double a, LL b) {
double ans = 1.0;
while(b) {
if(b % 2)ans = ans * a;
a = a * a;
b /= 2;
} return ans;
}
LL quick_pow(LL x, LL y) {
LL ans = 1;
while(y) {
if(y & 1) {
ans = ans * x % mod;
} x = x * x % mod;
y >>= 1;
} return ans;
}
int du[27];
int mp[27][27];
int tuop() {
queue<int> qu;
while(!qu.empty()) qu.pop();
memset(du, 0, sizeof(du));
for(int i = 0; i < 26; ++i) {
for(int j = 0; j < 26; ++j) {
if(mp[i][j] == 1) du[j]++;
}
}
for(int i = 0; i < 26; ++i) {
if(du[i] == 0) qu.push(i);
}
int num = 0;
while(!qu.empty()) {
int u = qu.front();
qu.pop();
num++;
for(int i = 0; i < 26; ++i) {
if(mp[u][i] == 1) {
du[i]--;
if(du[i] == 0) qu.push(i);
}
}
}
return num;
}
const int maxChild = 26;
const int maxNode = maxn;
int tot = 0;
int child[maxNode | 10][maxChild];
//int cnt; //该结点后缀的数量
int isWord[maxNode];
void insert(string s) {
int pos, cur = 0;
for (int i = 0; i < s.length(); ++i) {
pos = s[i] - 'a';
if (child[cur][pos] == 0)
child[cur][pos] = ++tot;
cur = child[cur][pos];
//cnt++[cur];
}
isWord[cur] = 1;
}
int query(string s) {
int pos, cur = 0;
memset(mp, 0, sizeof(mp));
for (int i = 0; i < s.length(); ++i) {
pos = s[i] - 'a';
for(int j = 0; j < maxChild; ++j) {
if(pos == j) continue;
if(child[cur][j] != 0)
mp[pos][j] = 1;
}
if (child[cur][pos] == 0)
return 0;
if(isWord[cur])
return 0;
cur = child[cur][pos];
}
return tuop() == 26;
}
string str[maxn];
int flag[maxn];
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
IO;
int n;
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> str[i];
insert(str[i]);
}
int ans = 0;
for(int i = 1; i <= n; i++) {
if(query(str[i])) {
flag[i] = 1;
ans++;
}
}
// printf("%d\n", ans);
cout << ans << '\n';
for(int i = 1; i <= n; i++) {
if(flag[i]) {
cout << str[i] << '\n';
}
}
return 0;
}
牛客练习赛11 假的字符串 (Trie树+拓扑找环)的更多相关文章
- 牛客网 牛客练习赛11 A.假的线段树
看不懂题意,而且太菜,写了两道就溜了... A.假的线段树 链接:https://www.nowcoder.com/acm/contest/59/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2 ...
- 牛客练习赛16 B 漂亮的树【哈希hash/思维】
链接:https://www.nowcoder.com/acm/contest/84/B 来源:牛客网 题目描述 街上有n棵树,标号为1...n,第i棵树的高度为ai. 定义这n棵树是漂亮的,当且仅当 ...
- 牛客网 牛客练习赛11 D.求距离
D.求距离 链接:https://www.nowcoder.com/acm/contest/59/D来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 32768K,其他语言6 ...
- 牛客练习赛42 A 字符串
题目描述 给定两个等长的由小写字母构成的串 A,BA,B,其中 |A|=|B|=n|A|=|B|=n. 现在你需要求出一个子区间 [l,r][l,r] 使得 LCP(A[l,r],B[l,r])×LC ...
- 牛客练习赛52 B题【树状数组维护区间和{查询区间和,如果区间元素重复出现则计数一次}】补题ing
[题目] 查询区间和,如果区间元素重复出现则计数一次. 链接:https://ac.nowcoder.com/acm/contest/1084/B [题解] 将询问按r排序,维护每个数最后出现的位置, ...
- 牛客练习赛28B (经典)【线段树】
<题目链接> qn姐姐最好了~ qn姐姐给你了一个长度为n的序列还有m次操作让你玩, 1 l r 询问区间[l,r]内的元素和 2 l r 询问区间[l,r]内的元 ...
- 牛客练习赛42A(字符串)
传送门 结论是:一定是选取最长的那个AB连续子串. 把题面要求的a*b + a + b转化一下成(a + 1)*(b + 1) - 1,即可发现如果选取前缀后缀不连续的两段作为答案,则显然有更优解,即 ...
- 牛客练习赛1 矩阵 字符串二维hash+二分
题目 https://ac.nowcoder.com/acm/contest/2?&headNav=www#question 解析 我们对矩阵进行二维hash,所以每个子矩阵都有一个额hash ...
- 牛客练习赛28-B(线段树,区间更新)
牛客练习赛28 - B 传送门 题目 qn姐姐最好了~ qn姐姐给你了一个长度为n的序列还有m次操作让你玩, 1 l r 询问区间[l,r]内的元素和 2 l r 询问区间[l,r]内的 ...
随机推荐
- 【时光回溯】【JZOJ3568】【GDKOI2014】小纪的作业题
题目描述 输入 输出 有M行,每个询问一行,输出结果mod 1,000,000,007的值. 样例输入 10 3 3 5 1 2 3 1 3 5 2 1 7 9 3 9 2 3 样例输出 10 19 ...
- 2017校赛 问题 F: 懒人得多动脑
题目描述 小D的家A和学校B都恰好在以点F为焦点的双曲线上,而小D每日所需的生活水源在一条平行该双曲线准线的直线上,设它的值为v.大家都知道,每天都是要喝水的,但是小D有点懒,他希望自己能在去上学或者 ...
- Gym - 101962K _ Rei do Cangaço
题意:给予n个房间,每个房间可以的到x个金币(x可能为负数),可以进行两种操作: 右移3i个房间,并且打开除最后一个的所有房间,如在1号房间,第一次移动可以移动到4号,并且打开1,2,3三个房间. 只 ...
- NDK(1)简介
AndroidNDK Android NDK 是在SDK前面又加上了“原生”二字,即Native Development Kit,因此又被Google称为“NDK”. Android程序运行在Dalv ...
- Android依赖别的包时,出现的问题
项目和依赖的项目一定要在同一个文件夹下,不然会出现这种问题
- Launch configuration JUnitCore references non-existing project XXX.
- 2019-10-23-WPF-使用-SharpDx-渲染博客导航
title author date CreateTime categories WPF 使用 SharpDx 渲染博客导航 lindexi 2019-10-23 21:10:13 +0800 2019 ...
- HDU-1257_最少拦截系统
最少拦截系统 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Desc ...
- etcd 在超大规模数据场景下的性能优化
作者 | 阿里云智能事业部高级开发工程师 陈星宇(宇慕) 概述 etcd是一个开源的分布式的kv存储系统, 最近刚被cncf列为沙箱孵化项目.etcd的应用场景很广,很多地方都用到了它,例如kuber ...
- nginx简单使用(windows)
本篇文章对术语不作讲解 下载nginx 首先,进入nginx官网http://nginx.org/en/download.html. 找到Stable version,此处的版本是稳定版本: 下载完成 ...