POJ 1509 后缀自动机

题意:给出一个圆圈,圆圈上顺时针有一些字符,现在要从某个字符前边断开,得到一个以这个字符开头的字符串,使得得到的字符串字典序最小。如果多解,输出最小的断点。


题解:把字符串copy一次,放到原串后边,使得长度*2,然后放到SAM中,从Root开始按照字典序走贪心len步(如果不能走就提前break) 搞定。


Code:

#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 1e4+100;
char s[maxn];
int n;
int L;
struct SAM{
	int last,cnt,nxt[maxn*2][26],fa[maxn*2],len[maxn*2];
	void init(){
		last=cnt=1;
		memset(nxt[1],0,sizeof nxt[1]);
		fa[1]=len[1]=0;
	}
	int newnode(){
		++cnt;
		memset(nxt[cnt],0,sizeof nxt[cnt]);
		fa[cnt]=len[cnt]=0;
		return cnt;
	}
	void add(int c){
		int p=last;
		int np = newnode();
		last = np;
		len[np]=len[p]+1;
		while (p&&!nxt[p][c]){
			nxt[p][c]=np;
			p=fa[p];
		}
		if (!p){
			fa[np]=1;
		}else{
			int q = nxt[p][c];
			if (len[q]==len[p]+1){
				fa[np]=q;
			}else{
				int nq=newnode();
				len[nq]=len[p]+1;
				memcpy(nxt[nq],nxt[q],sizeof (nxt[q]));
				fa[nq]=fa[q];
				fa[np]=fa[q]=nq;
				while (nxt[p][c]==q){
					nxt[p][c]=nq;
					p=fa[p];
				}
			}
		}
	}
	int query(){
		int now =1;
		for (int i=0;i<L;i++){
			bool flag = false;
			for (int j=0;j<=25;j++){
				if (nxt[now][j]){
					flag = true;
					now = nxt[now][j];
					break;
				}
			}
			if (!flag){
				break;
			}
		}
		return len[now]+1-L;
	}
}sam;
int main(){
	scanf("%d",&n);
	while (n--){
		sam.init();
		scanf("%s",s);
		L = strlen(s);
		for (int i=0;i<L;i++){
			sam.add(s[i]-'a');
		}
		for (int i=0;i<L;i++){
			sam.add(s[i]-'a');
		}
		printf("%d\n",sam.query());
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值