wannafly 练习赛11 B 假的字符串(字典树+建边找环)
链接:https://www.nowcoder.com/acm/contest/59/B
时间限制:C/C++ 1秒,其他语言2秒
64bit IO Format: %lld
题目描述
给定n个字符串,互不相等,你可以任意指定字符之间的大小关系(即重定义字典序),求有多少个串可能成为字典序最小的串,并输出它们
输入描述:
- 第一行一个数表示n
之后n行每行一个字符串表示给定的字符串
输出描述:
- 第一行输出一个数x表示可行的字符串个数
之后输出x行,每行输出一个可行的字符串
输出的顺序和输入的顺序一致
输入例子:
- 6
- mcfx
- ak
- ioi
- wen
- l
- a
输出例子:
- 5
- mcfx
- ioi
- wen
- l
- a
-->
输入
- 6
- mcfx
- ak
- ioi
- wen
- l
- a
输出
- 5
- mcfx
- ioi
- wen
- l
- a
备注:
- 对于100%的数据,
n <= 30000 , 字符串总长<= 300000
字符集为小写字符
//////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- #include <bits/stdc++.h>
- #define mst(a,b) memset((a),(b), sizeof a)
- #define lowbit(a) ((a)&(-a))
- #define IOS ios::sync_with_stdio(0);cin.tie(0);
- using namespace std;
- typedef long long ll;
- const int mod=1e9+;
- const int maxn=3e5+;
- string use[];
- int head,cnt;
- int nx[maxn][];
- int cc[maxn];
- bool co[maxn];
- void init(int k){
- int now=head;
- int sz=use[k].size();
- for(int i=;i<sz;++i){
- int d=use[k][i]-'a';
- if(!nx[now][d])nx[now][d]=++cnt;
- now=nx[now][d];
- }
- ++cc[now];
- }
- bool mp[][];
- int vis[];
- bool dfs(int pos){
- vis[pos]=-;
- for(int i=;i<;++i)if(i!=pos&&mp[pos][i]){
- if(!vis[i]){
- if(!dfs(i))return false;
- }else{
- if(vis[i]==-)return false;
- }
- }
- vis[pos]=;
- return true;
- }
- bool ok(int k){
- int now=head;
- int sz=use[k].size();
- mst(mp,);mst(vis,);
- for(int i=;i<sz;++i){
- int d=use[k][i]-'a';
- for(int j=;j<;++j)if(j!=d&&nx[now][j]){
- mp[j][d]=;
- }
- now=nx[now][d];
- if(i!=sz-&&cc[now])return false;
- }
- for(int i=;i<;++i)if(!vis[i]&&!dfs(i))return false;
- return true;
- }
- int main(){
- #ifdef local
- freopen("in.txt","r",stdin);
- //freopen("out.txt","w",stdout);
- #endif
- IOS
- int n;cin>>n;
- for(int i=;i<=n;++i){
- cin>>use[i];
- init(i);
- }
- int ans=;
- for(int i=;i<=n;++i)if(ok(i)){
- co[i]=true;
- ++ans;
- }
- cout<<ans<<endl;
- for(int i=;i<=n;++i)if(co[i])cout<<use[i]<<endl;
- return ;
- }
wannafly 练习赛11 B 假的字符串(字典树+建边找环)的更多相关文章
- 牛客网 牛客练习赛11 A.假的线段树
看不懂题意,而且太菜,写了两道就溜了... A.假的线段树 链接:https://www.nowcoder.com/acm/contest/59/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2 ...
- wannafly 练习赛11 F 求子树(树上莫队+换根)
链接:https://www.nowcoder.com/acm/contest/59/F 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K 64b ...
- Wannafly挑战赛11 D 题 字符串hash + 卡常
题目链接 https://ac.nowcoder.com/acm/contest/73#question map与order_map https://blog.csdn.net/BillCYJ/art ...
- hihocoder offer收割编程练习赛11 A hiho字符串
思路: 我用的尺取. 注意题目描述为恰好2个'h',1个'i',1个'o'. 实现: #include <iostream> #include <cstdio> #includ ...
- wannafly 练习赛11 E 求最值(平面最近点对)
链接:https://www.nowcoder.com/acm/contest/59/E 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K 64bit ...
- 字符串hash与字典树
title: 字符串hash与字典树 date: 2018-08-01 22:05:29 tags: acm 算法 字符串 概述 这篇主要是关于字符串里的 字符串hash 和 字符串字典树,,两个都是 ...
- 字典树Java实现
Trie树的原理 Trie树也称字典树,因为其效率很高,所以在在字符串查找.前缀匹配等中应用很广泛,其高效率是以空间为代价的. 利用串构建一个字典树,这个字典树保存了串的公共前缀信息,因此可以降低查询 ...
- codeforces 1285D. Dr. Evil Underscores(字典树)
链接:https://codeforces.com/problemset/problem/1285/D 题意:给n个数a1,a2,a3.....an,找到一个数X,使得X 异或所有的ai ,得到的ma ...
- 讲解——Trie树(字典树)
Trie树(字典树) 一.引入 字典是干啥的?查找字的. 字典树自然也是起查找作用的.查找的是啥?单词. 看以下几个题: 1.给出n个单词和m个询问,每次询问一个单词,回答这个单词是否在单 ...
随机推荐
- cpu和内存的使用率统计
统计cpu和内存一个月的平均使用率: #!/bin/bash totalcpu_rate=0.0 totalmem_rate=0.0 num_days=$(ls -l /var/log/sa/sa[0 ...
- Centos7搭建FTP服务
1.安装 vsftpd [root@CentOS ftp]# yum -y install vsftpd2.启动 vsftpd 服务 [root@CentOS ~]# systemctl star ...
- Codeforces1256E_Yet Another Division Into Teams
题意 n个人,每人有一个能力值a[i],要求分成多个队伍,每个队伍至少3个人,使得所有队伍的max(a[i])-min(a[i])之和最小. 分析 不会巧妙的dp,想了一天只想到了暴力的dp. 先排序 ...
- Python基础——函数入门
等待更新…………………… 后面再写
- oracle获取年、月、日
--获取年 select extract(year from date'2011-05-17') year from dual; --获取月 select extract(month from dat ...
- Linux练习例题(附答案)
1.通过ps命令的两种选项形式查看进程信息 2.通过top命令查看进程 3.通过pgrep命令查看sshd服务的进程号 4.查看系统进程树 5.使dd if=/dev/zero of=/root/fi ...
- shell统计mysql当前连接数
[root@push-- scripts]# mysql -S /var/lib/mysql//mysql.sock -uroot -phlsms_push_Zaq1xsw@ -e "sho ...
- Instr()函数用法
返回 Variant (Long),指定一字符串在另一字符串中最先出现的位置. 语法 InStr([start, ]string1, string2[, compare]) InStr 函数的语法具有 ...
- Zookeeper客户端使用(使用zkclient)
Zookeeper客户端使用 二.使用zkclient 在pom.xml中加入依赖 <dependency> <groupId>com.101tec</groupId&g ...
- yaourt
https://blog.csdn.net/relcodego/article/details/50531379 https://blog.csdn.net/lsvtogergo/article/de ...