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 ;
}

最新文章

  1. 下载apk文件浏览器会直接打开并显示乱码的问题
  2. 【PHP资源】PHP 资源大全
  3. Java多线程系列--“JUC线程池”06之 Callable和Future
  4. Python 开发轻量级爬虫06
  5. C#把datetime类型的日期转化成年月日或其他格式方法总结
  6. iOS - UISplitViewController
  7. Java Load Properties 文件,定义message信息
  8. Labview中引用,属性节点,局部变量之间的区别
  9. IQC,QA,FQC,OQC,IPQC的定义与职责
  10. 过目不忘JS正则表达式(转)
  11. Java学习之DAO设计模式
  12. 关于闭包与for循环的理解
  13. Jfreet 自动删除生成的图片
  14. voa 2015 / 4 / 15
  15. Fail2防止sshd暴力破解
  16. C# http 性能优化500毫秒到 60 毫秒
  17. 五、MongoDB的索引
  18. Zephyr的Logging
  19. 循环内的switch中break和continue使用区别
  20. vm15安装MACOS

热门文章

  1. TP框架部分--文件目录及作用
  2. EasyPusher进行Android UVC外接摄像头直播推送实现方法
  3. EasyNVR RTSP转RTMP-HLS流媒体服务器前端构建之:通过接口获取实时信息
  4. Centos7升级python版本
  5. mongo-connector导入数据到Es
  6. WCF基础之设计和实现服务协定
  7. 20170326 ABAP调用外部webservice实例
  8. SAP采购寄售业务操作步骤
  9. Oracle伪列rownum
  10. Automator 实例:使用快捷键 实现 快速在当前路径 打开 iTerm