链接:https://www.nowcoder.com/acm/contest/59/B

时间限制:C/C++ 1秒,其他语言2秒

空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

给定n个字符串,互不相等,你可以任意指定字符之间的大小关系(即重定义字典序),求有多少个串可能成为字典序最小的串,并输出它们

输入描述:

  1. 第一行一个数表示n
    之后n行每行一个字符串表示给定的字符串

输出描述:

  1. 第一行输出一个数x表示可行的字符串个数
    之后输出x行,每行输出一个可行的字符串
    输出的顺序和输入的顺序一致

输入例子:
  1. 6
  2. mcfx
  3. ak
  4. ioi
  5. wen
  6. l
  7. a
输出例子:
  1. 5
  2. mcfx
  3. ioi
  4. wen
  5. l
  6. a

-->

示例1

输入

  1. 6
  2. mcfx
  3. ak
  4. ioi
  5. wen
  6. l
  7. a

输出

  1. 5
  2. mcfx
  3. ioi
  4. wen
  5. l
  6. a

备注:

  1. 对于100%的数据,
    n <= 30000 , 字符串总长<= 300000
    字符集为小写字符
    //////////////////////////////////////////////////////////////////////////////

  1. ///////////////////////////////////////////////////////////////////////////////////////////////////
  1. #include <bits/stdc++.h>
  2. #define mst(a,b) memset((a),(b), sizeof a)
  3. #define lowbit(a) ((a)&(-a))
  4. #define IOS ios::sync_with_stdio(0);cin.tie(0);
  5. using namespace std;
  6. typedef long long ll;
  7. const int mod=1e9+;
  8. const int maxn=3e5+;
  9. string use[];
  10. int head,cnt;
  11. int nx[maxn][];
  12. int cc[maxn];
  13. bool co[maxn];
  14. void init(int k){
  15. int now=head;
  16. int sz=use[k].size();
  17. for(int i=;i<sz;++i){
  18. int d=use[k][i]-'a';
  19. if(!nx[now][d])nx[now][d]=++cnt;
  20. now=nx[now][d];
  21. }
  22. ++cc[now];
  23. }
  24. bool mp[][];
  25. int vis[];
  26. bool dfs(int pos){
  27. vis[pos]=-;
  28. for(int i=;i<;++i)if(i!=pos&&mp[pos][i]){
  29. if(!vis[i]){
  30. if(!dfs(i))return false;
  31. }else{
  32. if(vis[i]==-)return false;
  33. }
  34. }
  35. vis[pos]=;
  36. return true;
  37. }
  38. bool ok(int k){
  39. int now=head;
  40. int sz=use[k].size();
  41. mst(mp,);mst(vis,);
  42. for(int i=;i<sz;++i){
  43. int d=use[k][i]-'a';
  44. for(int j=;j<;++j)if(j!=d&&nx[now][j]){
  45. mp[j][d]=;
  46. }
  47. now=nx[now][d];
  48. if(i!=sz-&&cc[now])return false;
  49. }
  50. for(int i=;i<;++i)if(!vis[i]&&!dfs(i))return false;
  51. return true;
  52. }
  53. int main(){
  54. #ifdef local
  55. freopen("in.txt","r",stdin);
  56. //freopen("out.txt","w",stdout);
  57. #endif
  58. IOS
  59. int n;cin>>n;
  60. for(int i=;i<=n;++i){
  61. cin>>use[i];
  62. init(i);
  63. }
  64. int ans=;
  65. for(int i=;i<=n;++i)if(ok(i)){
  66. co[i]=true;
  67. ++ans;
  68. }
  69. cout<<ans<<endl;
  70. for(int i=;i<=n;++i)if(co[i])cout<<use[i]<<endl;
  71. return ;
  72. }

wannafly 练习赛11 B 假的字符串(字典树+建边找环)的更多相关文章

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

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

  2. wannafly 练习赛11 F 求子树(树上莫队+换根)

    链接:https://www.nowcoder.com/acm/contest/59/F 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K 64b ...

  3. Wannafly挑战赛11 D 题 字符串hash + 卡常

    题目链接 https://ac.nowcoder.com/acm/contest/73#question map与order_map https://blog.csdn.net/BillCYJ/art ...

  4. hihocoder offer收割编程练习赛11 A hiho字符串

    思路: 我用的尺取. 注意题目描述为恰好2个'h',1个'i',1个'o'. 实现: #include <iostream> #include <cstdio> #includ ...

  5. wannafly 练习赛11 E 求最值(平面最近点对)

    链接:https://www.nowcoder.com/acm/contest/59/E 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K 64bit ...

  6. 字符串hash与字典树

    title: 字符串hash与字典树 date: 2018-08-01 22:05:29 tags: acm 算法 字符串 概述 这篇主要是关于字符串里的 字符串hash 和 字符串字典树,,两个都是 ...

  7. 字典树Java实现

    Trie树的原理 Trie树也称字典树,因为其效率很高,所以在在字符串查找.前缀匹配等中应用很广泛,其高效率是以空间为代价的. 利用串构建一个字典树,这个字典树保存了串的公共前缀信息,因此可以降低查询 ...

  8. codeforces 1285D. Dr. Evil Underscores(字典树)

    链接:https://codeforces.com/problemset/problem/1285/D 题意:给n个数a1,a2,a3.....an,找到一个数X,使得X 异或所有的ai ,得到的ma ...

  9. 讲解——Trie树(字典树)

          Trie树(字典树) 一.引入 字典是干啥的?查找字的. 字典树自然也是起查找作用的.查找的是啥?单词. 看以下几个题: 1.给出n个单词和m个询问,每次询问一个单词,回答这个单词是否在单 ...

随机推荐

  1. cpu和内存的使用率统计

    统计cpu和内存一个月的平均使用率: #!/bin/bash totalcpu_rate=0.0 totalmem_rate=0.0 num_days=$(ls -l /var/log/sa/sa[0 ...

  2. Centos7搭建FTP服务

    1.安装 vsftpd ​[root@CentOS ftp]# yum -y install vsftpd2.启动 vsftpd 服务 ​[root@CentOS ~]# systemctl star ...

  3. Codeforces1256E_Yet Another Division Into Teams

    题意 n个人,每人有一个能力值a[i],要求分成多个队伍,每个队伍至少3个人,使得所有队伍的max(a[i])-min(a[i])之和最小. 分析 不会巧妙的dp,想了一天只想到了暴力的dp. 先排序 ...

  4. Python基础——函数入门

    等待更新…………………… 后面再写

  5. oracle获取年、月、日

    --获取年 select extract(year from date'2011-05-17') year from dual; --获取月 select extract(month from dat ...

  6. Linux练习例题(附答案)

    1.通过ps命令的两种选项形式查看进程信息 2.通过top命令查看进程 3.通过pgrep命令查看sshd服务的进程号 4.查看系统进程树 5.使dd if=/dev/zero of=/root/fi ...

  7. shell统计mysql当前连接数

    [root@push-- scripts]# mysql -S /var/lib/mysql//mysql.sock -uroot -phlsms_push_Zaq1xsw@ -e "sho ...

  8. Instr()函数用法

    返回 Variant (Long),指定一字符串在另一字符串中最先出现的位置. 语法 InStr([start, ]string1, string2[, compare]) InStr 函数的语法具有 ...

  9. Zookeeper客户端使用(使用zkclient)

    Zookeeper客户端使用 二.使用zkclient 在pom.xml中加入依赖 <dependency> <groupId>com.101tec</groupId&g ...

  10. yaourt

    https://blog.csdn.net/relcodego/article/details/50531379 https://blog.csdn.net/lsvtogergo/article/de ...