生活随笔
收集整理的這篇文章主要介紹了
BZOJ 4516: [Sdoi2016]生成魔咒 [后缀自动机]
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
4516: [Sdoi2016]生成魔咒
題意:詢問一個(gè)字符串每個(gè)前綴有多少不同的子串
做了一下SDOI2016R1D2,題好水啊隨便AK
強(qiáng)行開map上SAM
每個(gè)狀態(tài)的貢獻(xiàn)就是\(Max(s)-Min(s)+1\)
插入的時(shí)候維護(hù)一下就行了
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
using namespace std;
typedef long long ll;
#define fir first
#define sec second
const int N=3e5+5, P=1e9+7;
inline int read() {char c=getchar(); int x=0, f=1;while(c<'0' || c>'9') {if(c=='-')f=-1; c=getchar();}while(c>='0' && c<='9') {x=x*10+c-'0'; c=getchar();}return x*f;
}int n, s[N]; ll ans;
struct meow { map<int, int> ch; int par, val;}t[N];
int sz=1, root=1, last=1;
void extend(int c) {int p=last, np=++sz;t[np].val = t[p].val+1;for(; p && !t[p].ch[c]; p=t[p].par) t[p].ch[c]=np;if(!p) t[np].par = root;else {int q=t[p].ch[c];if(t[q].val == t[p].val+1) t[np].par=q;else {ans -= t[q].val - t[p].val;int nq=++sz; t[nq]=t[q]; t[nq].val = t[p].val+1;t[q].par = t[np].par = nq;ans += t[q].val - t[nq].val; ans ++;for(; p && t[p].ch[c]==q; p=t[p].par) t[p].ch[c] = nq;}}ans+=t[np].val - t[t[np].par].val;last=np;
}
int main() {//freopen("in","r",stdin);freopen("incantation.in","r",stdin);freopen("incantation.out","w",stdout);n=read();for(int i=1; i<=n; i++) {s[i]=read();extend(s[i]);printf("%lld\n",ans);}
}
轉(zhuǎn)載于:https://www.cnblogs.com/candy99/p/6652757.html
總結(jié)
以上是生活随笔為你收集整理的BZOJ 4516: [Sdoi2016]生成魔咒 [后缀自动机]的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。