poj1509 Glass Beads 后缀数组

来源:互联网 发布:小猪熊网络 编辑:程序博客网 时间:2024/06/05 16:02
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 wants to test the robustness of a necklace so he needs a program that will be able to determine the worst possible 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 10000 characters. Each bead is represented by a lower-case character of the english alphabet (a--z), 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
4helloworldamandamandadontcallmebfuaaabaaa
Sample Output
10116

5

题意:给你个串(该串为环形),让你求出从何处断开得到的字符串的字典序最小

思路:如果不是环的话直接sa数组就行了,但此处是环形,其实也不难,将该串重复两边然后求sa数组和height数组,从sa[0]开始遍历sa,直到找到第一个sa[i]<len(串的长度),然后再利用高度数组height,因为有可能在sa[i]附近的后缀串的最大前缀height大于len,这是就求这些最大前缀大于len的字符串的最小值就行了

ac代码:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int mx=200100;char st[mx];int s[mx],sa[mx],t[mx],t2[mx],c[mx],n;int rank[mx],height[mx];void build_sa(int m){    int i,*x=t,*y=t2;    for (i=0;i<m;i++) c[i]=0;    for (i=0;i<n;i++) c[x[i]=s[i]]++;    for (i=1;i<m;i++) c[i]+=c[i-1];    for (i=n-1;i>=0;i--) sa[--c[x[i]]]=i;    for (int k=1;k<=n;k<<=1)    {        int p=0;        for (i=n-k;i<n;i++) y[p++]=i;        for (i=0;i<n;i++) if (sa[i]>=k) y[p++]=sa[i]-k;        for (i=0;i<m;i++) c[i]=0;        for (i=0;i<n;i++) c[x[y[i]]]++;        for (i=1;i<m;i++) c[i]+=c[i-1];        for (i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];        swap(x,y);        p=1;        x[sa[0]]=0;        for (i=1;i<n;i++)        x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;        if (p>=n) break;        m=p;    }}void getHeight(){    int i,j,k=0;    for (i=0;i<n;i++) rank[sa[i]]=i;    for (i=0;i<n;i++)    {        if (k) k--;        int j=sa[rank[i]-1];        while (s[i+k]==s[j+k]) k++;        height[rank[i]]=k;    }}int main(){    int x;    scanf("%d",&x);    getchar();    while(x--)    {        gets(st);        int len=strlen(st);        n=0;        for(int i=0;i<len;i++)        s[n++]=st[i]-'a'+1;        for(int i=0;i<len;i++)        s[n++]=st[i]-'a'+1;        build_sa(30);        getHeight();        for(int i=0;i<=n;i++)        {            if(sa[i]>=0&&sa[i]<len)            {                int min=sa[i];                for(int k=i+1;k<n;k++)                {                    if(height[k]>=len)                    {                        if(sa[k]<min)                        min=sa[k];                    }                    else                    break;                }                printf("%d\n",min+1);                break;            }        }    }}

其实这个题最简单的解决办法是用最小表示法http://blog.csdn.net/tianyuhang123/article/details/54919737,这里只是用来练后缀数组罢了

0 0
原创粉丝点击