forked from beet-aizu/library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindromictree.cpp
140 lines (130 loc) · 2.76 KB
/
palindromictree.cpp
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
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
//BEGIN CUT HERE
struct PalindromicTree{
struct node{
map<char,int> nxt;
int len,suf,hei,app,cnt;
};
string s;
vector<node> v;
vector<int> ord;
int n,suff;
PalindromicTree(){}
PalindromicTree(string s_):s(s_),v(s_.length()+10){
n=2;
suff=1;
v[0].app=v[1].app=-1;
v[0].len=-1;v[1].len=0;
v[0].suf=v[1].suf=0;
v[0].hei=v[1].hei=0;
build();
}
bool addchar(int pos){
char ch=s[pos];
int cur=suff;
while(1){
if(pos-1-v[cur].len>=0&&s[pos-1-v[cur].len]==ch) break;
cur=v[cur].suf;
}
if(v[cur].nxt.count(ch)){
suff=v[cur].nxt[ch];
v[suff].cnt++;
return false;
}
suff=n++;
v[suff].len=v[cur].len+2;
v[suff].app=pos-v[suff].len+1;
v[suff].cnt=1;
v[cur].nxt[ch]=suff;
if(v[suff].len==1){
v[suff].suf=1;
v[suff].hei=1;
return true;
}
while(1){
cur=v[cur].suf;
if(pos-1-v[cur].len>=0&&s[pos-1-v[cur].len]==ch){
v[suff].suf=v[cur].nxt[ch];
break;
}
}
v[suff].hei=v[v[suff].suf].hei+1;
return true;
}
void calcOrder(){
ord.clear();
ord.push_back(0);
ord.push_back(1);
for(int i=0;i<(int)ord.size();i++){
for(auto &p:v[ord[i]].nxt) ord.push_back(p.second);
}
}
void calcCount(){
assert(ord.size());
for(int i=(int)ord.size()-1;i>=0;i--){
v[v[ord[i]].suf].cnt+=v[ord[i]].cnt;
}
v[0].cnt=v[1].cnt=0;
}
void build(){
for(int i=0;i<(int)s.size();i++) addchar(i);
calcOrder();
calcCount();
}
};
//END CUT HERE
struct RollingHash{
typedef unsigned long long ull;
string S;
ull B;
vector<ull> hash,p;
int len;
RollingHash(){}
RollingHash(string S,ull B=1000000007LL):S(S),B(B){init();};
void init(){
len=S.length();
hash.resize(len+1);
p.resize(len+1);
hash[0]=0;p[0]=1;
for(int i=0;i<len;i++){
hash[i+1]=hash[i]*B+S[i];
p[i+1]=p[i]*B;
}
}
//S[l,r)
ull find(int l,int r){
return hash[r]-hash[l]*p[r-l];
}
};
signed main(){
string s,t;
cin>>s>>t;
PalindromicTree p1(s),p2(t);
RollingHash r1(s),r2(t);
using ull = RollingHash::ull;
map<ull,Int> m1[55555],m2[55555];
for(int i=0;i<(int)p1.n;i++){
PalindromicTree::node& u=p1.v[i];
if(u.app<0) continue;
m1[u.len][r1.find(u.app,u.app+u.len)]=u.cnt;
}
for(int i=0;i<(int)p2.n;i++){
PalindromicTree::node& u=p2.v[i];
if(u.app<0) continue;
m2[u.len][r2.find(u.app,u.app+u.len)]=u.cnt;
}
Int ans=0;
for(int i=1;i<=(int)s.length();i++){
for(auto p:m1[i]){
ans+=m2[i][p.first]*p.second;
}
}
cout<<ans<<endl;
return 0;
}
/*
verified on 2017/10/29
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2292
*/