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. EntityFramework走马观花之CRUD(中)

    如果是独立的实体对象,在底层数据库中它对应一张独立的表,那么,对它进行新建.删除和修改没有任何难度,实在不值浪费笔墨在它上头. 在现实项目中,完全独立的对象少之又少,绝大多数情况都是对象之间有着紧密的 ...

  2. DockPanel的使用与技巧

    DockPanel的使用 1.建立一个WinForm工程,默认生成了一个WinForm窗体Form1. 2.引用—>添加引用—>浏览—>weiFenLuo.winFormsUI.Do ...

  3. 九度OJ 1167:数组排序 (排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5395 解决:1715 题目描述: 输入一个数组的值,求出各个值从小到大排序后的次序. 输入: 输入有多组数据. 每组输入的第一个数为数组的 ...

  4. 【题解】 P5022旅行

    [题解]P5022 旅行 当给定你一颗树的时候,这题就是一道送分题,凉心啊! 但是给定你一颗基环树呢? 暴力断环直接跑. 但是数据范围\(n\le 1000\) 乱做就完事了. 考场上这样想的,对于\ ...

  5. Django框架ORM常用参数汇总_模型层

    primary_key 如果为True,那么这个字段就是模型的主键. 如果你没有指定任何一个字段的primary_key=True, Django就会自动添加一个IntegerField字段做为主键, ...

  6. 我的Android进阶之旅------>真正在公司看几天代码的感触

    仅以此文来回顾这一周我的工作情况,以及由此而触发的感想. ============================================================= 来到新公司5天了, ...

  7. 我的Java开发学习之旅------>工具类:Java获取字符串和文件进行MD5值

    ps:这几天本人用百度云盘秒传了几部大片到云盘上,几个G的文件瞬秒竟然显示"上传成功"!这真让我目瞪口呆,要是这样的话,那得多快的网速,这绝对是不可能的,也许这仅是个假象.百度了一 ...

  8. spring运行步骤

    Spring确实使你能通过最简单可行的解决的方法来解决你的问题. 而这是有有非常大价值的.同一时候他的源码的设计理念也受到非常多程序猿的追捧,简洁,易用.但是从哪着手研究Spring却是非常多新手头疼 ...

  9. [异步][jms][activeMq]怎样做到重试机制不会导致一条消息被多次运行.

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/fei33423/article/details/32723571 淘宝海量存储之单机事务面临的问题及 ...

  10. apache 网页301重定向、自定义400/403/404/500错误页面

    首先简单介绍一下,.htaccess文件是Apache服务器中的一个配置文件(Nginx服务器没有),它负责相关目录下的网页配置.通过对.htaccess文件进行设置,可以帮我们实现:网页301重定向 ...