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. http协议详解-经典篇

    本文转载至 http://www.cnblogs.com/flychen/archive/2012/11/28/2792206.html   ————————————————————————————— ...

  2. EasyPlayer Android安卓RTSP服务器低延时再优化策略

    EasyPlayer低延迟再优化策略 EasyPlayer是一款专门针对RTSP协议进行过优化的播放器.其中两个我们引以为傲的的优点就是起播快和低延迟.最近我们遇到一些需求,其对延迟要求非常苛刻,于是 ...

  3. cmake使用第三方库

    1 link_directories和target_link_libraries 1.1 link_directories 告诉linker去这些目录去找library. 1.2 target_lin ...

  4. IIS的ARR实现站点的负载均衡 nginx 对比

    windows下使用IIS的ARR实现站点的负载均衡 - CSDN博客 https://blog.csdn.net/zzy7075/article/details/73294713 IIS的ARR实现 ...

  5. /etc/init.d/iptables stop

    /etc/init.d/iptables stop     systemctl stop firewalld.service   [root@bigdata-server-01 myrestserve ...

  6. Database: key

    super-key: Any key that has more columns than necessary to uniquely identify each row in the table i ...

  7. poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13611   Accepted: 6138 ...

  8. HDU 1201 Fibonacci Again

    Fibonacci Again Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  9. C++之封装

    希望暴露public 希望隐藏private 对象实例化有两种方式,从栈实例化,从堆(new出来的)实例化. 以谁做什么作为核心. public 放前面,private放后面(属性可以定义为priva ...

  10. BZOJ 1638 [Usaco2007 Mar]Cow Traffic 奶牛交通:记忆化搜索【图中边的经过次数】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1638 题意: 给你一个有向图,n个点,m条有向边. 对于所有从入度为0的点到n的路径,找出 ...