最小表示法,這里用后綴自動機實現。 剛剛學了后綴自動機,懵逼中。 ps:純屬為了練習后綴自動機,字符串最小表示法的專門算法比這快很多。
#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>#include<queue>using namespace std;const int maxn=3001000;char s[maxn*2];int n;struct sam{ int a[maxn][30],mx[maxn],fa[maxn],last,cnt; sam(){ last=++cnt; } void insert(int c) { int p=last,np=last=++cnt;mx[np]=mx[p]+1; while(p&&!a[p][c]) a[p][c]=np,p=fa[p]; if(!p) fa[np]=1; else { int q=a[p][c]; if(mx[p]+1==mx[q]) fa[np]=q; else { int nq=++cnt;mx[nq]=mx[p]+1; memcpy(a[nq],a[q],sizeof(a[q])); fa[nq]=fa[q]; fa[np]=fa[q]=nq; while(a[p][c]==q) a[p][c]=nq;p=fa[p]; } } }}SAM;int main(){ freopen("MinReQQqqaabaaa*/新聞熱點
疑難解答