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代码: 模拟进制的懒得写了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxnode = ;
typedef pair<int,int> pii;
//const int maxnode = 1000000;
const int sigma_size = ; struct Trie{
int ch[maxnode][sigma_size];
int fa[maxnode];
char let[maxnode];
int sz;
void init()
{
sz=;
memset(ch[],,sizeof(ch[]));
}
int idx(char c) {return c-'A';}
void insert(char *s,int n)
{
int u=;
for(int i=;i<n;++i)
{
int c=idx(s[i]);
if(!ch[u][c])
{
memset(ch[sz],,sizeof(ch[sz]));
fa[sz]=u;
let[sz]=c+'A';
ch[u][c]=sz++;
if(sz>=maxnode)
cout<<"fuck this memory"<<endl;
}
u=ch[u][c];
}
}
char ans[];
int ansz;
void bfs()
{
int u;
queue<int> q;
q.push();
while(!q.empty())
{
u=q.front();
q.pop();
for(int i=;i<sigma_size;++i)
{
if(ch[u][i])
{
q.push(ch[u][i]);
}
else
{//find
// cout<<"find "<<u<<endl;
ans[]=i+'A';
ansz=;
print(u);
return;
}
}
}
}
void print(int u)
{
while(u)
{
// cout<<".";
ans[ansz++]=let[u];
u=fa[u];
}
// cout<<"sz="<<ansz<<endl;
for(int i=ansz-;i>=;--i)
printf("%c",ans[i]);
printf("\n");
}
void text()
{
int i;
for(i=;i<sz;++i)
{
printf("%d: fa=%d let=%c\n",i,fa[i],let[i]);
}
}
}; char ms[];
Trie tree; void run()
{
int i,len;
scanf("%s",ms);
len = strlen(ms);
tree.init();
for(i=;i<len;++i)
{
tree.insert(ms+i,min(,len-i));
}
// tree.text();
tree.bfs();
} int main()
{
//freopen("in","r",stdin);
int _;
scanf("%d",&_);
while(_--)
run();
return ;
}

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. struts2 eclipse集成jdk与tomcat (2)

    Eclipse 中集成jdk与tomcat 1. 首次打开Eclipse为让你选择工作空间,选择合适即可. 添加JDK (1) 在Eclipse的菜单中选择window选项,单击 perference ...

  2. iOS SQLite使用

    数据库的特征: 以一定方式存储在一起 能为多个用户分享 具有尽可能少的冗余代码 与程序彼此独立的数据集 SQLite SQLite是一个轻量级关系数据库,最初的设计目标是用于嵌入式系统,它占用资源非常 ...

  3. 查看SqlServer安装的log文件

    SqlServer安装时产生的log被保存在这个目录下: "%programfiles%\Microsoft SQL Server\[SQL_VERSION]\Setup Bootstrap ...

  4. ul和li 基本用法分析(这里主要想学习怎么用在导航栏中)

    常用作:导航,少量数据表格,居中 一.做导航,居中 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  5. python3用pdfminer3k在线读取pdf文件

    import importlib import sys import random from urllib.request import urlopen from urllib.request imp ...

  6. 怎样避免C#中将小数转换为字符串时出现科学记数法

    在C#中如果float.double.decimal类型的值,小数点后的0太多时,C#会用科学记数法来表示小数的值. 例如下面的double类型0.00009,如果我们直接将其用ToString()方 ...

  7. vue应用示例

    1 . 实现轮播图 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset=&q ...

  8. ZOJ - 3866 Cylinder Candy 【数学】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3866 思路 积分 参考博客 https://blog.csdn. ...

  9. <JAVA图像学习笔记>十字路口交通模拟--操作系统模拟课后小项目

    项目的要求很简单: 模拟出十字路口的交通控制情况: 秒. 当东西(或南北)方向红灯时,所有车辆(除了消防车.救护车.警车)均排队等待,当东西(或南北)方向绿灯时,所有车辆按序行驶(不准超车). 制作这 ...

  10. POJ 2063 Investment (完全背包)

    A - Investment Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Subm ...