牛客练习赛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]内的 ...
随机推荐
- Mybatis中输出映射resultType与resultMap的区别
Mybatis中输出映射resultType与resultMap的区别 (原文地址:http://blog.csdn.net/acmman/article/details/46509375) 一.re ...
- C/S和B/S交互 2016-03-19 11:27 1275人阅读 评论(30) 收藏
最近一直在做C/S的项目,每天都超忙,抽个时间写篇博客,之前一直做C/S项目就是各种窗体,各种控件,拖来拖去,然后点进去写方法,做BS的时候呢,因为一直使用的是mvc,所以就是经常手写代码,或者拖引用 ...
- hdu 2196【树形dp】
http://acm.hdu.edu.cn/showproblem.php?pid=2196 题意:找出树中每个节点到其它点的最远距离. 题解: 首先这是一棵树,对于节点v来说,它到达其它点的最远距离 ...
- HashMap的运用 计算3个班级的总成绩和平均成绩
TestStudent.java package com.sxt.home; import java.util.ArrayList; import java.util.HashMap; import ...
- 设置select和option的文字居中
select{ width: auto; padding: 0 1%; //左右一定要设置 margin: 0; } option{ text-align:center; }
- C运行时库函数
C运行时库函数是指C语言本省支持的一些基本函数,通常是汇编直接实现的. API函数是操作系统提供给用户方便设计应用程序的函数,实现一些特定的功能,API函数也是C语言的函数实现的. 他们之间区别是: ...
- Android教程 -07 Activity的任务栈和启动模式
Activity是由任务栈管理的,一般情况下一个应用程序只有一个任务栈. 什么是栈? 栈是一种常用的数据结构,栈只允许访问栈顶的元素,栈就像一个杯子,每次都只能取杯子顶上的东西 栈的特点就是先进后出, ...
- Project Euler Problem 21-Amicable numbers
先说最暴力的算法,直接对一万内的每个数字暴力分解因子(对每个数字的时间复杂度是O(sqrt(n)的),然后,用个数组记录下来因子和,然后寻找 亲密数. 好一点:要先打个素数表,然后对每个数字,分解素因 ...
- ubuntu环境变量的三种设置方法
一:设置环境变量的三种方法 1.1 临时设置 export PATH=/home/yan/share/usr/local/arm/3.4.1/bin:$PATH 1.2 当前用户的全局设置 打开~/. ...
- iptables禁止代理端口
#iptables -A INPUT -p tcp --dport 3128 -j REJECT