7 const int maxn
=250000+10;
13 SAM(): pre(NULL
), len(0) { memset(next
,0,sizeof(next
)); }
14 } sam
[maxn
*4],*root
,*last
;
18 namespace suffix_automation
20 void insert(int ch
) //ch==ASCII code - 'a'
22 SAM
*p
=last
,*np
=&sam
[cnt
++];
24 for (; p
&& !p
->next
[ch
]; p
=p
->pre
) p
->next
[ch
]=np
;
26 if (!p
) np
->pre
=root
; else
27 if (p
->next
[ch
]->len
==p
->len
+1) np
->pre
=p
->next
[ch
]; else
29 SAM
*q
=p
->next
[ch
],*nq
=&sam
[cnt
++];
33 for (; p
&& p
->next
[ch
]==q
; p
=p
->pre
) p
->next
[ch
]=nq
;
39 int n
=strlen(s
),res
=0,tmp
=0;
41 for (int i
=0; i
<n
; ++i
)
44 if (now
->next
[ch
]) ++tmp
,now
=now
->next
[ch
]; else
46 while (now
&& !now
->next
[ch
]) now
=now
->pre
;
47 if (!now
) now
=root
,tmp
=0; else tmp
=now
->len
+1,now
=now
->next
[ch
];
59 last
=root
=&sam
[cnt
++];
62 for (int i
=0; i
<n
; ++i
) suffix_automation::insert(a
[i
]-'a');
63 printf("%d\n",suffix_automation::solve(b
));