UVA - 719Glass Beads(后缀数组)

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...ai1 is lexicografically smaller than the string ajaj+1...ana1...aj1 . String a1a2...an is lexicografically smaller than the string b1b2... bn if and only if there exists an integer i,in , so that aj=bj , for each j,1j<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 10000 characters. Each bead is represented by a lower-case character of the english alphabet (az) , where a<b<...<z

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 A[i] is lexicographically smallest among all the n possible disjoinings of a necklace. If there are more than one solution, print the one with the lowest i .

Sample Input

4
helloworld
amandamanda
dontcallmebfu
aaabaaa

Sample Output

10
11
6
5

题意

给定一个字符串S,将这个字符串的第一个字符移到最后面,让它的字典序最小,最少要移动多少个字符?

解题思路:后缀数组

将这个 S 重复一次,拼接成SS,然后进行一次后缀数组,将sa数组和lcp(高度数组)构建好,然后从开头找小于 S 字符串长度的下标,然后从这个下标开始连续寻找下标小于S字符串长度,并且lcp数组的大小要大于等于 S 字符串长度的几个下标,因为他们的拥有最长公共前缀。
[本人使用的模板是倍增法,挑战程序设计竞赛上的,理解的时候是依靠上面的代码所以就使用了,可以用kuangbin大牛们的模板 ] <script type="math/tex" id="MathJax-Element-1397">]</script>

代码

时间复杂度:后缀树组

/*为了便于查看,代码头文件部分去除,需要的可以复制博客中的头文件模板*/

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值