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
|
const int N=8e5+10;
char str[N];
int l1,ans;
int n,s[N],sa[N],rk[N],tp[N],pos[N],c[N],c1[N];
#define del(x) sa[c[s[x]]--]=x
#define add(x) sa[c[s[x]]++]=x
void SA_sort(int *S,int n,int m,int *s,int *tp,int tn)
{
clr(sa,n+2),clr(c1,m+2);
R(i,1,n) c1[s[i]]++;
R(i,2,m) c1[i]+=c1[i-1];
cpy(c+1,c1+1,m+2);
L(i,1,tn) del(S[i]);
R(i,1,m+1) c[i]=c1[i-1]+1;
R(i,1,n) if(sa[i]>1&&tp[sa[i]-1]) add(sa[i]-1);
cpy(c+1,c1+1,m+2);
L(i,1,n) if(sa[i]>1&&!tp[sa[i]-1]) del(sa[i]-1);
}
void SA_IS(int n,int m,int *s,int *tp,int *pos)
{
int tot=0,cnt=0;int *S=s+n;
tp[n]=0;
L(i,1,n-1) tp[i]=(s[i]!=s[i+1])?s[i]>s[i+1]:tp[i+1];
rk[1]=0;
R(i,2,n) if(tp[i-1]&&!tp[i]) pos[++tot]=i,rk[i]=tot;else rk[i]=0;
SA_sort(pos,n,m,s,tp,tot);
int u=0,p=0;
R(i,1,n) if(rk[sa[i]])
{
u=rk[sa[i]];
if(cnt<=1||pos[u+1]-pos[u]!=pos[p+1]-pos[p]) ++cnt;
else
{
R(j,0,pos[u+1]-pos[u])
if(s[pos[u]+j]!=s[pos[p]+j]||tp[pos[u]+j]!=tp[pos[p]+j]) {++cnt;break;}
}
S[u]=cnt;
p=u;
}
if(tot!=cnt) SA_IS(tot,cnt,S,tp+n,pos+n);
else R(i,1,tot) sa[S[i]]=i;
R(i,1,tot) S[i]=pos[sa[i]];
SA_sort(S,n,m,s,tp,tot);
}
int ht[N];
void get_ht(int n)
{
R(i,1,n) rk[sa[i]=sa[i+1]]=i;
int k=0;
R(i,1,n)
{
k=(!k)?0:k-1;
while(s[i+k]==s[sa[rk[i]-1]+k]) ++k;
ht[rk[i]]=k;
}
}
pii stk[N];
int top,g[N];
signed main()
{
scanf("%s",str+1);l1=strlen(str+1);str[l1+1]='z'+1;
scanf("%s",str+l1+2);
n=strlen(str+1);
R(i,1,n) s[i]=str[i]-'a'+2;s[++n]=1;
SA_IS(n--,28,s,tp,pos);
get_ht(n);
//R(i,1,n) printf("i:%lld ht:%lld sa:%lld rk:%lld\n",i,ht[i],sa[i],rk[i]);
stk[0]=mkp(1,0);
R(i,1,n) g[i]=g[i-1]+(sa[i]<=l1);
R(i,1,n)
{
while(top&&ht[stk[top].fi]>ht[i]) top--;
++top;stk[top]=mkp(i,(g[i-1]-g[stk[top-1].fi-1])*ht[i]+stk[top-1].se);
if(sa[i]>l1+1) ans+=stk[top].se;
}
top=0;
R(i,1,n) g[i]=g[i-1]+(sa[i]>l1+1);
R(i,1,n)
{
while(top&&ht[stk[top].fi]>ht[i]) top--;
++top;stk[top]=mkp(i,(g[i-1]-g[stk[top-1].fi-1])*ht[i]+stk[top-1].se);
if(sa[i]<=l1) ans+=stk[top].se;
}
writeln(ans);
}
|