poj1509(后缀自动机)

其实这个题也可以用最小表示法

但是还是练了练后缀自动机

明白了构造函数就是构造原串(整个串)的以最后一个字符为止的所有后缀都加上一个字符产生新串的后缀

这个题目就是找这个字符串循环中的最小表示

直接两个原串一拼接 所有的循环都是拼接后长串的后缀

找到长度为len的最小后缀即可



#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
const int maxn = 20000+100;
int tot = 1;
int las = 1;
struct NODE{
    int ch[26];
    int len,fa;
    NODE(){
        memset(ch,0,sizeof(ch));
        len = fa = 0;
    }
}dian[maxn];
int pos[maxn*2];
void add(int c,int _pos){
    int p = las;
    int np = las = ++tot;
    pos[np] = _pos;
    dian[np].len = dian[p].len+1;
    for(;p&&!dian[p].ch[c];p = dian[p].fa)dian[p].ch[c] = np;
    if(!p)dian[np].fa = 1;
    else{
        int q = dian[p].ch[c];
        if(dian[q].len == dian[p].len+1){
            dian[np].fa = q;
        }
        else{
            int nq = ++tot;
            pos[nq] = _pos;
            dian[nq] = dian[q];
            dian[nq].len = dian[p].len+1;
            dian[q].fa = dian[np].fa = nq;
            for(;p&&dian[p].ch[c]==q;p = dian[p].fa)dian[p].ch[c] = nq;
        }
    }
}
char s[maxn*2];
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        memset(dian,0,sizeof(dian));
        tot = las = 1;
        scanf("%s",s);
        int len = strlen(s);
        for(int i = len;i<len+len;++i){
            s[i] = s[i-len];
        }
        for(int i = 0;i<len+len;++i){
            add(s[i]-'a',i);
        }
        int st = 1;

        for(int i = 0;i<len;++i){
            for(int j = 0;j<26;++j){
                if(dian[st].ch[j]){
                    st = dian[st].ch[j];
                    //cout<<pos[st]<<endl;
                    break;
                }
            }
        }
        int ind = pos[st];
       // cout<<ind<<endl;
        printf("%d\n",ind-len+2);


    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值