uva 719 Glass Beads (后缀自动机)

题意:

给出一个串,如bcdaf长度n=5,可以将串头尾相连,在这个循环串中求字典序最小且长度为n的串的开头点位置。

题解:

后缀自动机中遍历dfs找到第n个点对应的肯定是长度为n字典序最小的子串,并且结尾点就是遍历的结尾点。


#include<iostream>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define B(x) (1<<(x))
typedef long long ll;
const int oo=0x3f3f3f3f;
const ll OO=1LL<<61;
const int MOD=10007;
const int maxn=10050;
const int maxm=200005;
const int SIZE=maxn<<2;
const int type=26;
char str[maxn<<1];
int n;

struct SuffixAutoMaton{

    int next[SIZE][type],fa[SIZE],len[SIZE],num[SIZE];
    int tol,last;
    int newNode(int x){

        len[tol]=x;
        fa[tol]=-1;
        num[tol]=0;
        for(int i=0;i<type;i++)
            next[tol][i]=-1;
        return tol++;
    }

    void Init(){

        tol=0;
        last=newNode(0);
    }

    void add(int k){

        int now=last;
        int end=newNode(len[now]+1);
        while(now!=-1&&next[now][k]==-1){

            next[now][k]=end;
            now=fa[now];
        }
        if(now==-1) fa[end]=0;
        else{

            int nxt=next[now][k];
            if(len[nxt]==len[now]+1) fa[end]=nxt;
            else{

                int cnxt=newNode(len[now]+1);
                for(int i=0;i<type;i++)next[cnxt][i]=next[nxt][i];
                fa[cnxt]=fa[nxt];
                fa[nxt]=fa[end]=cnxt;
                while(now!=-1&&next[now][k]==nxt){
                    next[now][k]=cnxt;
                    now=fa[now];
                }
            }
        }
        last=end;
    }

    void Insert(char T[]){

        for(int i=0;T[i];i++){
            add(T[i]-'a');
        }
    }

    int dfs(int u,int step){
        if(step==n)return len[u];
        for(int i=0;i<type;i++){
            if(next[u][i]!=-1){
                int k=dfs(next[u][i],step+1);
                if(k)return k;
            }
        }
        return 0;
    }

    void solve(){
        printf("%d\n",dfs(0,0)-n+1);
    }

}sam;

int main(){

    int T,L;
    scanf("%d",&T);
    while(T--){

        scanf("%s",str);
        L=n=strlen(str);
        for(int i=0;i<n;i++)str[L++]=str[i];
        str[L]='\0';
        sam.Init();
        sam.Insert(str);
        sam.solve();
    }
    return 0;
}
/**

*/





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值