牛客练习赛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树+拓扑找环)的更多相关文章

  1. 牛客网 牛客练习赛11 A.假的线段树

    看不懂题意,而且太菜,写了两道就溜了... A.假的线段树 链接:https://www.nowcoder.com/acm/contest/59/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2 ...

  2. 牛客练习赛16 B 漂亮的树【哈希hash/思维】

    链接:https://www.nowcoder.com/acm/contest/84/B 来源:牛客网 题目描述 街上有n棵树,标号为1...n,第i棵树的高度为ai. 定义这n棵树是漂亮的,当且仅当 ...

  3. 牛客网 牛客练习赛11 D.求距离

    D.求距离 链接:https://www.nowcoder.com/acm/contest/59/D来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 32768K,其他语言6 ...

  4. 牛客练习赛42 A 字符串

    题目描述 给定两个等长的由小写字母构成的串 A,BA,B,其中 |A|=|B|=n|A|=|B|=n. 现在你需要求出一个子区间 [l,r][l,r] 使得 LCP(A[l,r],B[l,r])×LC ...

  5. 牛客练习赛52 B题【树状数组维护区间和{查询区间和,如果区间元素重复出现则计数一次}】补题ing

    [题目] 查询区间和,如果区间元素重复出现则计数一次. 链接:https://ac.nowcoder.com/acm/contest/1084/B [题解] 将询问按r排序,维护每个数最后出现的位置, ...

  6. 牛客练习赛28B (经典)【线段树】

    <题目链接> qn姐姐最好了~     qn姐姐给你了一个长度为n的序列还有m次操作让你玩,     1 l r 询问区间[l,r]内的元素和     2 l r 询问区间[l,r]内的元 ...

  7. 牛客练习赛42A(字符串)

    传送门 结论是:一定是选取最长的那个AB连续子串. 把题面要求的a*b + a + b转化一下成(a + 1)*(b + 1) - 1,即可发现如果选取前缀后缀不连续的两段作为答案,则显然有更优解,即 ...

  8. 牛客练习赛1 矩阵 字符串二维hash+二分

    题目 https://ac.nowcoder.com/acm/contest/2?&headNav=www#question 解析 我们对矩阵进行二维hash,所以每个子矩阵都有一个额hash ...

  9. 牛客练习赛28-B(线段树,区间更新)

    牛客练习赛28 - B 传送门 题目 qn姐姐最好了~ ​ qn姐姐给你了一个长度为n的序列还有m次操作让你玩, ​ 1 l r 询问区间[l,r]内的元素和 ​ 2 l r 询问区间[l,r]内的 ...

随机推荐

  1. Python学习之路2☞数据类型与变量

    变量 变量作用:保存状态:说白了,程序运行的状态就是状态的变化,变量是用来保存状态的,变量值的不断变化就产生了运行程序的最终输出结果 一:声明变量 #!/usr/bin/env python # -* ...

  2. Vue指令:v-for的用法;v-bind绑定class的几种写法;tab标签切换

    一.v-for 的用法 循环指令,可以遍历 Number.String.Object.Array: 循环数字.字符串:有2个参数,分别是value和索引值: 循环对象:有3个参数,分别是 属性值.属性 ...

  3. Resharper 如何把类里的类移动到其他文件

    有时候,看到一个类里有很多类,需要把他移动其他文件 假如有一个类 class A { class B { } } 如何把 B 移动文件 B里? 一般使用 快捷键是 Resharper 的快捷键,如果不 ...

  4. oracle函数 LENGTH(c1)

    [功能]返回字符串的长度; [说明]多字节符(汉字.全角符等),按1个字符计算 [参数]C1 字符串 [返回]数值型 [示例] SQL> select length('高乾竞'),length( ...

  5. 关于6410板文件的dm9000的平台设备地址

    转自csdn #define CONFIG_DM9000_BASE 0x20000300#define DM9000_IO                      0x20000000#define ...

  6. 阿里云PolarDB发布重大更新 支持Oracle等数据库一键迁移上云

    5月21日,阿里云PolarDB发布重大更新,提供传统数据库一键迁移上云能力,可以帮助企业将线下的MySQL.PostgreSQL和Oracle等数据库轻松上云,最快数小时内迁移完成.据估算,云上成本 ...

  7. OracleSpatial函数

    Oracle_spatial的函数 一sdo_Geom包的函数: 用于表示两个几何对象的关系(结果为True/False)的函数:RELATE,WITHIN_DISTANCE 验证的函数:VALIDA ...

  8. poj 3334 Connected Gheeves (Geometry + BInary Search)

    3334 -- Connected Gheeves 题意是,给出两个尖形的相连的容器,要求向其中灌水.它们具有日常的物理属性,例如两个容器中水平面高度相同以及水高于容器顶部的时候就会溢出.开始的时候打 ...

  9. H3C PPP基本配置

  10. [转]来自后端的逆袭 blazor简介 全栈的福音

    背景 什么是SPA 什么是MPA MPA (Multi-page Application) 多页面应用指的就是最传统的 HTML 网页设计,早期的网站都是这样的设计,所之称为「网页设计」.使用 MPA ...