Description
Once upon a time there was a famous actress. As you may expect, she played mostly Antique Comedies most of all. All the people loved her. But she was not interested in the crowds. Her big hobby were beads of any kind. Many bead makers were working for her and they manufactured new necklaces and bracelets every day. One day she called her main Inspector of Bead Makers (IBM) and told him she wanted a very long and special necklace. The necklace should be made of glass beads of different sizes connected to each other but without any thread running through the beads, so that means the beads can be disconnected at any point. The actress chose the succession of beads she wants to have and the IBM promised to make the necklace. But then he realized a problem. The joint between two neighbouring beads is not very robust so it is possible that the necklace will get torn by its own weight. The situation becomes even worse when the necklace is disjoined. Moreover, the point of disconnection is very important. If there are small beads at the beginning, the possibility of tearing is much higher than if there were large beads. IBM wantsto test the robustness of a necklace so he needs a program that will be able to determine the worstpossible point of disjoining the beads.
The description of the necklace is a string
A=a1a2...
am specifying sizes of the particular beads,where the last character am is considered to precede character
a1
in circular fashion.
The disjoint point i is said to be worse than the disjoint point j if and only if the string
aiai+1...ana1...ai−1
is lexicografically smaller than the string
ajaj+1...ana1...aj−1
. String
a1a2...an
is lexicografically smaller than the string
b1b2...
bn if and only if there exists an integer
i,i≤n
, so that
aj=bj
, for each
j,1≤j<i
and
ai<bi
.
Input
The input consists of N cases. The first line of the input contains only positive integer
N
. Then follow the cases. Each case consists of exactly one line containing necklace description. Maximal length of each description is
Output
For each case, print exactly one line containing only one integer — number of the bead which is the first at the worst possible disjoining, i.e. such
i
, that the string
Sample Input
4
helloworld
amandamanda
dontcallmebfu
aaabaaa
Sample Output
10
11
6
5
题意
给定一个字符串
解题思路:后缀数组
将这个
S
重复一次,拼接成
代码
时间复杂度:后缀树组
/*为了便于查看,代码头文件部分去除,需要的可以复制博客中的头文件模板*/
const double PI = 3.1415926535898;
const double eps = 1e-10;
const int MAXM = 1e5 + 5;
const int MAXN = 2e4 + 5;
const int INF = 0x3f3f3f3f;
int N, k;
char S[MAXN];
int _rank[MAXN], tmp[MAXN];
int sa[MAXN], lcp[MAXN], slen;
bool compare_sa(int i, int j) {
if(_rank[i] != _rank[j]) return _rank[i] < _rank[j];
else {
int ri = i + k <= slen ? _rank[i + k] : -1;
int rj = j + k <= slen ? _rank[j + k] : -1;
return ri < rj;
}
}
void construct_sa(char buf[]) {
int len = strlen(buf);
slen = len;
for(int i = 0; i <= len; i ++) {
sa[i] = i;
_rank[i] = i < len ? S[i] : -1;
}
for(k = 1; k <= len; k <<= 1) {
sort(sa, sa + len + 1, compare_sa);
tmp[sa[0]] = 0;
for(int i = 1; i <= len; i ++) {
tmp[sa[i]] = tmp[sa[i - 1]] + (compare_sa(sa[i - 1], sa[i]) ? 1 : 0);
}
for(int i = 0; i <= len; i ++) {
_rank[i] = tmp[i];
}
}
}
void construct_lcp(char buf[]) {
int len = strlen(buf);
for(int i = 0; i <= len; i ++) _rank[sa[i]] = i;
int h = 0;
lcp[0] = 0;
for(int i = 0; i < len; i ++) {
int j = sa[_rank[i] - 1];
if(h > 0) h --;
for(; j + h < len && i + h < len; h ++) {
if(S[j + h] != S[i + h]) break;
}
lcp[_rank[i] - 1] = h;
}
}
void solve(char buf[]) {
int len = strlen(buf);
for(int i = len; i < len * 2; i ++) {
buf[i] = buf[i % len];
}
len <<= 1;
buf[len] = '\0';
construct_sa(buf);
construct_lcp(buf);
int minv = INF;
for(int i = 0; i <= len; i ++) {
if(sa[i] < len / 2) {//小于S字符串长度,这里因为*2所以要/2
minv = sa[i];
while(lcp[i] >= len / 2 && i <= len) {//连续并且最长公共前缀大于等于S字符串的长度
i ++;
if(sa[i] < len) {
minv = min(minv, sa[i]);
}
}
break;
}
}
printf("%d\n", minv + 1);
}
int main() {
#ifndef ONLINE_JUDGE
//FIN;
//FOUT;
#endif
IO_Init();
while(~scanf("%d", &N)) {
for(int i = 0; i < N; i ++) {
scanf("%s", S);
solve(S);
}
}
return 0;
}