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
题意:
给定一个由A~H8种字符组成,长度不超过1000000的字符串。问在这个字符串的最短不存在连续子串。若存在多个最短连续子串,输出字典序最小的一个。
题解:
先估计最长的答案是多少?我的估计是6个字符长,为什么?长度为6的字符串共有8^6≈250000,如果不存在公用的情况,需要长度250000*6的长度,已经长过1000000了。(有可能是7个字符长的,但7个字符的样例必须考虑字符共用,很难弄出来,所以6个字符就够了,如果想要保证正确性,可以用7个字符的)。
然后找出所有长度小于等于6的所有子串,由于子串是连续的,所以只用6*n的时间去查找。用f[i][j]标记存在的子串,i表示长度,j表示字符的哈希值(字符种类只有8个,用i位8进制就可以表示所有的情况)。最后遍历f数组,找出第一个未被标记的子串即可。
#include <bits/stdc++.h>
using namespace std;
int vis[9000005];
char s[1000005];
char zimu[9]={'A','B','C','D','E','F','G','H'};
int len;
char ans[1000005];
int solve(int x)
{
memset(vis,0,sizeof(vis));
for(int i=0;i<=len-x;i++)
{
int k=0;
int sum=0;
while(k<x)
{
sum=sum*8+s[i+k]-'A';
k++;
}
vis[sum]=1;
}
int t=pow(8,x);
int kk=0;
for(int i=0;i<t;i++)
{
if(!vis[i])
{
int kk=x;
while(x)
{
int tt=i%8;
ans[x]=zimu[tt];
i/=8;
x--;
}
for(int j=1;j<=kk;j++)
printf("%c",ans[j] );
return 1;
}
}
return 0;
}
int main()
{
int t;
scanf("%d",&t);
getchar();
while(t--)
{
gets(s);
len=strlen(s);
for(int i=1;!solve(i);i++) ;
printf("\n");
}
}