1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
#include<ctime> #include<cstdio> #include<cstring> #include<cstdlib> #include<map> #include<set> #include<queue> #include<deque> #include<stack> #include<bitset> #include<vector> #include<algorithm> #include<iostream> #include<deque> using namespace std; namespace mine { typedef long long ll; #define double long double const int INF=0x3f3f3f3f; const ll LLINF=0x3f3f3f3f3f3f3f3fll; ll () { ll ans=0;char c=getchar();int f=1; while(c<'0' or c>'9') {if(c=='-') f=-1;c=getchar();} while('0'<=c and c<='9') ans=ans*10+c-'0',c=getchar(); return ans*f; } void write(ll num) { if(num<0) {num=-num;putchar('-');} if(num>9) write(num/10); putchar('0'+num%10); } void writeln(int num){write(num);puts("");} #define FR first #define SE second #define MP make_pair #define pr pair<int,int> #define PB push_back inline void chmax(ll &x,const ll y) {x=x>y?x:y;} inline void chmin(ll &x,const ll y) {x=x<y?x:y;}
const int MOD=1e9+7; void add(int &x,int y) {x+=y;if(x>=MOD) x-=MOD;if(x<0) x+=MOD;} const int MAX_N=2e5+10; struct LCT { struct Nod { int son[2],fa; int siz,right,add,sum; int sum2; bool fz; }p[MAX_N]; int id;LCT(){id=0;memset(p,0,sizeof p);} #define lc p[x].son[0] #define rc p[x].son[1] bool son(int x) {return p[p[x].fa].son[1]==x;}
void rr(int x) { if(p[x].fz) { if(lc) p[lc].fz^=1,swap(p[lc].son[0],p[lc].son[1]); if(rc) p[rc].fz^=1,swap(p[rc].son[0],p[rc].son[1]); p[x].fz=0; } } void pushup(int x) { p[x].sum2=p[x].siz; if(lc>0) add(p[x].sum2,p[lc].sum2); if(rc>0) add(p[x].sum2,p[rc].sum2);
p[x].sum=(ll)p[x].siz*p[x].right%MOD; if(lc>0) add(p[x].sum,p[lc].sum); if(rc>0) add(p[x].sum,p[rc].sum); add(p[x].sum,(ll)(p[x].sum2-p[x].siz)*p[x].add%MOD); } void pushdown(int x) { if(p[x].add==0) return; if(lc) p[lc].right+=p[x].add,p[lc].add+=p[x].add,pushup(lc); if(rc) p[rc].right+=p[x].add,p[rc].add+=p[x].add,pushup(rc); p[x].add=0;pushup(x); } void rotate(int x) { int f=p[x].fa,ff=p[f].fa;if(p[ff].son[son(f)]==f) p[ff].son[son(f)]=x; int w=son(x),tt=p[x].son[1-w];p[x].son[1-w]=f;p[f].son[w]=tt; p[tt].fa=f;p[f].fa=x;p[x].fa=ff;pushup(f);pushup(x); } bool isrt(int x) {return p[x].fa==0 or p[p[x].fa].son[son(x)]!=x;} int tmp[MAX_N]; void splay(int x) { int tot=0,now=x;while(!isrt(now)) tmp[++tot]=now,now=p[now].fa; tmp[++tot]=now;for(int i=tot;i>=1;i--) rr(tmp[i]),pushdown(tmp[i]); for(int f=p[x].fa;!isrt(x);rotate(x),f=p[x].fa) if(!isrt(f)) son(x)^son(f)?rotate(x):rotate(f); }
void access(int x) { int lst=0; while(x) { splay(x); p[x].son[1]=lst;p[lst].fa=x;pushup(x); lst=x;x=p[x].fa; } } void link(int x,int y) { access(y);p[y].fa=x; } int root; void makeroot(int x) { root=x; access(x);splay(x); p[x].fz^=1; swap(p[x].son[0],p[x].son[1]); } void cut(int x,int y) { makeroot(x);access(y);splay(y); if(p[y].son[0]!=x) puts("error"); p[y].son[0]=p[x].fa=0;pushup(y); } }lct; int t[MAX_N]; int ans=0,kkk=0; struct SAM { struct Nod{int fail,mx,son[26];}p[MAX_N]; int id,lst; void insert(int c) { int now=++id;p[now].mx=p[lst].mx+1; int a=lst;while(a and !p[a].son[c]) p[a].son[c]=now,a=p[a].fail; if(a==0) p[now].fail=1; else { int b=p[a].son[c];
if(p[b].mx==p[a].mx+1) { p[now].fail=b; lct.makeroot(1);lct.access(b);lct.splay(b);add(kkk,lct.p[b].sum); } else { int tmp=++id;p[tmp]=p[b];p[tmp].mx=p[a].mx+1;
lct.cut(b,p[b].fail);lct.splay(b);lct.p[b].siz=p[b].mx-p[tmp].mx;lct.pushup(b); lct.p[tmp].siz=p[tmp].mx-p[p[tmp].fail].mx;lct.p[tmp].right=lct.p[b].right;lct.pushup(tmp); lct.link(p[tmp].fail,tmp);lct.link(tmp,b); lct.makeroot(1);lct.access(tmp);lct.splay(tmp);add(kkk,lct.p[tmp].sum);
p[b].fail=p[now].fail=tmp; while(a and p[a].son[c]==b) p[a].son[c]=tmp,a=p[a].fail; } }
int fa=p[now].fail; lct.makeroot(1);lct.access(fa);lct.splay(fa);lct.p[fa].add++;lct.p[fa].right++;add(lct.p[fa].sum,lct.p[fa].siz);lct.pushup(fa); lct.p[now].right=1;lct.p[now].siz=p[now].mx-p[fa].mx;lct.pushup(now);lct.link(fa,now);
lst=now;add(ans,kkk); }
SAM(){id=lst=1;memset(p,0,sizeof p);} }sam;
char str[MAX_N]; void main() { int n;scanf("%d%s",&n,str+1); for(int i=1;i<=n;i++) { sam.insert(str[i]-'a'); printf("%dn",ans); } } }; int main() { srand(time(0)); mine::main(); }
|
近期评论