博客园 首页 私信博主 显示目录 隐藏目录 管理 动画

poj 1509 Glass 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 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

4
helloworld
amandamanda
dontcallmebfu
aaabaaa

Sample Output

10
11
6
5

给一个字符串的环,求从哪个位置起字符串的字典序最小。

SAM:复制一发,建个后缀自动机,从上往下跑就好。
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MN 20002
using namespace std;

struct po{
    int t[26],f,l;
    po(){
        f=-1;l=0;
    }
}t[MN];
int num=0,n,la,T,MMH;
char s[10000];
inline void add(int x){
    int p=++num,o,ne;
    memset(t[p].t,0,sizeof(t[p].t));t[p].f=-1;t[p].l=0;
    t[p].l=t[la].l+1;
    while (la!=-1&&!t[la].t[x]) t[la].t[x]=p,la=t[la].f;
    if (la==-1) t[p].f=0;else{
        o=t[la].t[x];
        if (t[o].l==t[la].l+1) t[p].f=o;else{
            ne=++num;
            memset(t[ne].t,0,sizeof(t[ne].t));t[ne].f=-1;t[ne].l=0;
            t[ne]=t[o];
            t[ne].l=t[la].l+1;
            t[o].f=t[p].f=ne;
            while (la!=-1&&t[la].t[x]==o) t[la].t[x]=ne,la=t[la].f;
        }
    }
    la=p;
}
int main(){
    scanf("%d",&T);
    while(T--){
        num=0;la=0;
        memset(t[0].t,0,sizeof(t[0].t));t[0].f=-1;t[0].l=0;
        scanf("%s",s);n=strlen(s);
        for (int i=0;i<n;i++) add(s[i]-'a');
        for (int i=0;i<n;i++) add(s[i]-'a');
        MMH=0;
        for (int i=1;i<=n;i++)
        for (int j=0;j<26;j++)
        if (t[MMH].t[j]){
            MMH=t[MMH].t[j];
            break;
        }
        printf("%d\n",t[MMH].l-n+1);
    }
}
2588K 0MS G++ 1113B

 

 显然SA也是可做的,同样复制,然后找到第一个小于原长度的SA,再找找后面有没有同样可行且小于当前解的SA。

慢是肯定的了。

#include<cstdio>
#include<cstring>
#include<algorithm>
#define MN 200003
using namespace std;

int n,m,l,r,T,x,mmh,la;
char a[MN];
int s[MN];
int v[MN],sa[MN],q[MN],ran[MN],h[MN];
bool w[101];
int read_p,read_ca;
inline int read(){
    read_p=0;read_ca=getchar();
    while(read_ca<'0'||read_ca>'9') read_ca=getchar();
    while(read_ca>='0'&&read_ca<='9') read_p=read_p*10+read_ca-48,read_ca=getchar();
    return read_p;
}
inline void gr(int x){
    ran[sa[1]]=1;
    for (int i=2;i<=n;i++) ran[sa[i]]=(s[sa[i]]==s[sa[i-1]]&&s[sa[i]+x]==s[sa[i-1]+x])?ran[sa[i-1]]:ran[sa[i-1]]+1;
    for (int i=1;i<=n;i++) s[i]=ran[i];
}
inline void gv(){memset(v,0,sizeof(v));for (int i=1;i<=n;i++) v[s[i]]++;for (int i=1;i<=2e5;i++)v[i]+=v[i-1];}
inline void gsa(){
    gv();for (int i=n;i>=1;i--) sa[v[s[i]]--]=i;gr(0);
    for (int i=1;i<n;i<<=1){
        gv();for (int j=n;j>=1;j--) if (sa[j]>i) q[v[s[sa[j]-i]]--]=sa[j]-i;
        for (int j=n-i+1;j<=n;j++) q[v[s[j]]--]=j;
        for (int j=1;j<=n;j++) sa[j]=q[j];gr(i);
        if (ran[sa[n]]==n) return;
    }
}
inline void gh(){for (int i=1,k=0,j;i<=n;h[ran[i++]]=k) for (k?k--:0,j=sa[ran[i]-1];a[i+k]==a[j+k]&&i+k<=n&&j+k<=n;k++);}
inline int min(int a,int b){return a<b?a:b;}
int main(){
    T=read();
    while(T--){
        scanf("%s",a+1);n=strlen(a+1);
        for (int i=1;i<=n;i++) s[i+n]=a[i+n]=s[i]=a[i];n+=n;
        gsa();gh();
        for (int i=1;i<=n;i++) if (sa[i]<=n/2){
            mmh=sa[i];x=n;
            for (int j=i+1;j<=n;j++){
                x=min(x,h[j]);
                if (x!=min(n-sa[i]+1,n-sa[j]+1)) break;
                mmh=min(x,sa[j]);
            }
            break;
        }
        printf("%d\n",mmh);
    }
}
1528K 94MS G++ 1732B

 

 
posted @ 2016-05-08 19:43  swm_sxt  阅读(134)  评论(0编辑  收藏  举报