TIANKENG’s restaurant(Ⅱ)

Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 130107/65536 K (Java/Others)
Total Submission(s): 456    Accepted Submission(s): 149

Problem Description
After improving the marketing strategy, TIANKENG has made a fortune and he is going to step into the status of TuHao. Nevertheless, TIANKENG wants his restaurant to go international, so he decides to name his restaurant in English. For the lack of English skills, TIANKENG turns to CC, an English expert, to help him think of a property name. CC is a algorithm lover other than English, so he gives a long string S to TIANKENG. The string S only contains eight kinds of letters-------‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’. TIANKENG wants his restaurant’s name to be out of ordinary, so the restaurant’s name is a string T which should satisfy the following conditions: The string T should be as short as possible, if there are more than one strings which have the same shortest length, you should choose the string which has the minimum lexicographic order. Could you help TIANKENG get the name as soon as possible?

Meanwhile, T is different from all the substrings of S. Could you help TIANKENG get the name as soon as possible?

 
Input
The first line input file contains an integer T(T<=50) indicating the number of case.
In each test case:
Input a string S. the length of S is not large than 1000000.
 
Output
For each test case:
Output the string t satisfying the condition.(T also only contains eight kinds of letters-------‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’.)
 
Sample Input
3
ABCDEFGH
AAABAACADAEAFAGAH
ACAC
 
Sample Output
AA
BB
B
 
题意:给一个主串s,然后要找出一个串ans,ans是s中没出现过的子串里字典序最小的
 
思路:很容易得知这个串的长度不会超过8,所以建一个trie树,把s中每个长度小于8的串都插入树中,最后bfs一遍看哪个结点没给标记到的就是答案。这个方法是我比赛一开始想到的好麻烦的方法。AC之后想到一个简便的方法,把他看成8进制数,abcdefg对应0到8,开一个vis数组把每个子串都标记一下,然后循环找一下就行了
 
trie代码: 模拟进制的懒得写了
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <queue>
  5. using namespace std;
  6. const int maxnode = ;
  7. typedef pair<int,int> pii;
  8. //const int maxnode = 1000000;
  9. const int sigma_size = ;
  10.  
  11. struct Trie{
  12. int ch[maxnode][sigma_size];
  13. int fa[maxnode];
  14. char let[maxnode];
  15. int sz;
  16. void init()
  17. {
  18. sz=;
  19. memset(ch[],,sizeof(ch[]));
  20. }
  21. int idx(char c) {return c-'A';}
  22. void insert(char *s,int n)
  23. {
  24. int u=;
  25. for(int i=;i<n;++i)
  26. {
  27. int c=idx(s[i]);
  28. if(!ch[u][c])
  29. {
  30. memset(ch[sz],,sizeof(ch[sz]));
  31. fa[sz]=u;
  32. let[sz]=c+'A';
  33. ch[u][c]=sz++;
  34. if(sz>=maxnode)
  35. cout<<"fuck this memory"<<endl;
  36. }
  37. u=ch[u][c];
  38. }
  39. }
  40. char ans[];
  41. int ansz;
  42. void bfs()
  43. {
  44. int u;
  45. queue<int> q;
  46. q.push();
  47. while(!q.empty())
  48. {
  49. u=q.front();
  50. q.pop();
  51. for(int i=;i<sigma_size;++i)
  52. {
  53. if(ch[u][i])
  54. {
  55. q.push(ch[u][i]);
  56. }
  57. else
  58. {//find
  59. // cout<<"find "<<u<<endl;
  60. ans[]=i+'A';
  61. ansz=;
  62. print(u);
  63. return;
  64. }
  65. }
  66. }
  67. }
  68. void print(int u)
  69. {
  70. while(u)
  71. {
  72. // cout<<".";
  73. ans[ansz++]=let[u];
  74. u=fa[u];
  75. }
  76. // cout<<"sz="<<ansz<<endl;
  77. for(int i=ansz-;i>=;--i)
  78. printf("%c",ans[i]);
  79. printf("\n");
  80. }
  81. void text()
  82. {
  83. int i;
  84. for(i=;i<sz;++i)
  85. {
  86. printf("%d: fa=%d let=%c\n",i,fa[i],let[i]);
  87. }
  88. }
  89. };
  90.  
  91. char ms[];
  92. Trie tree;
  93.  
  94. void run()
  95. {
  96. int i,len;
  97. scanf("%s",ms);
  98. len = strlen(ms);
  99. tree.init();
  100. for(i=;i<len;++i)
  101. {
  102. tree.insert(ms+i,min(,len-i));
  103. }
  104. // tree.text();
  105. tree.bfs();
  106. }
  107.  
  108. int main()
  109. {
  110. //freopen("in","r",stdin);
  111. int _;
  112. scanf("%d",&_);
  113. while(_--)
  114. run();
  115. return ;
  116. }

hdu4886 TIANKENG’s restaurant(Ⅱ) (trie树或者模拟进制)的更多相关文章

  1. HDU4814——数学,模拟进制转换

    本题围绕:数学公式模拟进制转换 HDU4814 Golden Radio Base 题目描述 将一个十进制的非负整数转换成E(黄金分割数)进制的数 输入 不大于10^9的非负整数,处理到文件尾 输出 ...

  2. hdu 4099 Revenge of Fibonacci Trie树与模拟数位加法

    Revenge of Fibonacci 题意:给定fibonacci数列的前100000项的前n位(n<=40);问你这是fibonacci数列第几项的前缀?如若不在前100000项范围内,输 ...

  3. 【CF888G】Xor-MST Trie树(模拟最小生成树)

    [CF888G]Xor-MST 题意:给你一张n个点的完全图,每个点有一个权值ai,i到j的边权使ai^aj,求这张图的最小生成树. n<=200000,ai<2^30 题解:学到了求最小 ...

  4. 【BZOJ4523】[Cqoi2016]路由表 Trie树模拟

    [BZOJ4523][Cqoi2016]路由表 Description 路由表查找是路由器在转发IP报文时的重要环节.通常路由表中的表项由目的地址.掩码.下一跳(Next Hop)地址和其他辅助信息组 ...

  5. 2016ACM-ICPC网络赛北京赛区 1001 (trie树牌大模拟)

    [题目传送门] 1383 : The Book List 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 The history of Peking University ...

  6. HDU 4883 TIANKENG’s restaurant Bestcoder 2-1(模拟)

    TIANKENG's restaurant Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/O ...

  7. 【BZOJ2741】【FOTILE模拟赛】L 分块+可持久化Trie树

    [BZOJ2741][FOTILE模拟赛]L Description FOTILE得到了一个长为N的序列A,为了拯救地球,他希望知道某些区间内的最大的连续XOR和. 即对于一个询问,你需要求出max( ...

  8. 2018.10.20 NOIP模拟 巧克力(trie树+dfs序+树状数组)

    传送门 好题啊. 考虑前面的32分,直接维护后缀trietrietrie树就行了. 如果#号不在字符串首? 只需要维护第一个#前面的字符串和最后一个#后面的字符串. 分开用两棵trie树并且维护第一棵 ...

  9. 【bzoj2741】[FOTILE模拟赛]L 可持久化Trie树+分块

    题目描述 FOTILE得到了一个长为N的序列A,为了拯救地球,他希望知道某些区间内的最大的连续XOR和. 即对于一个询问,你需要求出max(Ai xor Ai+1 xor Ai+2 ... xor A ...

随机推荐

  1. python 基础 1.3 使用pycharm给python传递参数及pycharm调试模式

    一.通过pycharm 给python传递函数 1. 在pycharm终端中写入要获取的参数,进行获取 1>启动pycharm 中Terminal(终端) 窗口 点击pycharm左下角的图标, ...

  2. 使用Scrapy采集

    1.有些站点通过robot协议,防止scrapy爬取,就会出现如下问题: DEBUG: Forbidden by robots.txt: <GET http://baike.baidu.com/ ...

  3. XShell 连接 vm虚拟机中的redhat Linux系统

    选择的是nat链接,因为nat链接是没有网络的情况下,也是可以链接操作的,当然bridge也可以,那我就从第一步开始; 因为有的人可能改过电脑上的虚拟适配器的ip地址,导致和虚拟机默认的不一样了.如果 ...

  4. Leslie Lamport

    http://lamport.azurewebsites.net/pubs/pubs.html paper

  5. python网络爬虫之使用scrapy自动登录网站

    前面曾经介绍过requests实现自动登录的方法.这里介绍下使用scrapy如何实现自动登录.还是以csdn网站为例. Scrapy使用FormRequest来登录并递交数据给服务器.只是带有额外的f ...

  6. SAP数据表相关

    [转]SAP 数据表相关信息 今天用到了根据字段取数据元素描述,以前做过忘啦,在谢兄的帮助下搞定,把他的总结粘出来记住. 存储域(Domain)信息的表为DD01L:存储数据元素(Data Eleme ...

  7. 【转】Unicode(UTF-8, UTF-16)令人混淆的概念

    参考地址:http://www.cnblogs.com/kingcat/archive/2012/10/16/2726334.html Java中,char类型用UTF-16编码描述一个代码单元 为啥 ...

  8. Understanding JDBC Internals & Timeout Configuration

    原版:http://www.cubrid.org/blog/dev-platform/understanding-jdbc-internals-and-timeout-configuration 中文 ...

  9. OTA升级

    除了云端平台这部分,还要有通讯协议层面.云端和汽车端之间指令的接口和协议的制定,不同车厂会有不同诉求.艾拉比既可以支持车厂私有化定制协议的要求,也可以提供基于OMA标准的协议. 第一,它既是云端的工具 ...

  10. Linux学习之路(五)压缩命令

    常用压缩格式: .zip .gz .bz2 常用压缩格式: .tar.gz .tar.bz2 .zip格式压缩 .zip 压缩文件名 源文件 #压缩文件 .zip -r 压缩文件名 源目录 #压缩目录 ...