POJ

来源:互联网 发布:sql 验证用户名和密码 编辑:程序博客网 时间:2024/06/04 00:58

题意:

         求串的最小表示

思路:

         求串的最小表示,可以利用后缀数组求得,但也有更快的思维。

代码:

更快的思维

#include <iostream>#include <string>using namespace std;int MinimumRepresentation(string s){    int i = 0, j = 1, k = 0,t;    int l=s.size();    while (i < l && j < l && k < l){        t = s[(i+k)%l] - s[(j+k)%l];        if (t == 0)            k++;        else{            if (t > 0)                i += k + 1;            else                j += k + 1;            if (i == j)                j++;            k = 0;        }    }    return min(i,j);}int main(){    int n;    cin>>n;    while(n--){        string str;        cin>>str;        cout<<MinimumRepresentation(str)+1<<endl;    }}
后缀数组:

#include <stdio.h>#include <algorithm>#include <iostream>#include <string.h>#include <queue>using namespace std;/*suffix array*倍增算法  O(n*logn)*待排序数组长度为n,放在0~n-1中,在最后面补一个0*build_sa(,n+1, ); //注意是n+1;*getHeight(,n);*例如:*n   = 8;*num[]   = { 1, 1, 2, 1, 1, 1, 1, 2, $ };注意num最后一位为0,其他大于0*rank[]  = { 4, 6, 8, 1, 2, 3, 5, 7, 0 };rank[0~n-1]为有效值,rank[n]必定为0无效值*sa[]    = { 8, 3, 4, 5, 0, 6, 1, 7, 2 };sa[1~n]为有效值,sa[0]必定为n是无效值*height[]= { 0, 0, 3, 2, 3, 1, 2, 0, 1 };height[2~n]为有效值**/const int MAXN=20100;int sa[MAXN];   //SA数组,表示将S的n个后缀从小到大排序后把排好序的                //的后缀的开头位置顺次放入SA中int t1[MAXN],t2[MAXN],c[MAXN];//求SA数组需要的中间变量,不需要赋值int rank[MAXN],height[MAXN];//待排序的字符串放在s数组中,从s[0]到s[n-1],长度为n,且最大值小于m,//除s[n-1]外的所有s[i]都大于0,r[n-1]=0//函数结束以后结果放在sa数组中void build_sa(int s[],int n,int m){    int i,j,p,*x=t1,*y=t2;    //第一轮基数排序,如果s的最大值很大,可改为快速排序    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(j=1; j<=n; j<<=1)    {        p=0;        //直接利用sa数组排序第二关键字        for(i=n-j; i<n; i++)y[p++]=i; //后面的j个数第二关键字为空的最小        for(i=0; i<n; i++)if(sa[i]>=j)y[p++]=sa[i]-j;        //这样数组y保存的就是按照第二关键字排序的结果        //基数排序第一关键字        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];        //根据sa和x数组计算新的x数组        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]+j]==y[sa[i]+j]?p-1:p++;        if(p>=n)break;        m=p;//下次基数排序的最大值    }    int k = 0;    n--;    for(i = 0; i <= n; i++)        rank[sa[i]] = i;    for(i = 0; i < n; i++)    {        if(k) k--;        j = sa[rank[i]-1];        while(s[i+k] == s[j+k])            k++;        height[rank[i]] = k;    }}int main(){    int T;    scanf("%d",&T);    getchar();    char buf[MAXN];    int s[MAXN];    while(T--){        gets(buf);        int len=strlen(buf);        for(int i=0;i<len;i++) s[i]=buf[i];        for(int i=len;i<=len*2;i++) s[i]=buf[i-len];        build_sa(s,len*2+1,128);        int st=*min_element(rank,rank+len);        int ans=sa[st];        st++;        while(st<=2*len){            if(sa[st]<len){                if(height[st]>=len){                    ans=min(ans,sa[st]);                }else break;            }else break;            st++;        }        cout<<ans+1<<endl;    }}
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
101165


0 0
原创粉丝点击