洛谷 5685 「JSOI2013」快乐的 JYY

广义 PAM 板题吧……

算每个状态在两个字符串分别的出现次数就完事了(

代码:

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
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 5e4;
int n;
char s[N + 5];
long long ans;
namespace PAM
{
struct node
{
int ch[26];
int fa,len,sz[2];
} pam[N + 5];
int las = 1,tot = 1;
inline int init()
{
pam[1].len = -1,pam[0].fa = 1;
return 0;
}
int Init = init();
void insert(char *s,int i,int pos)
{
int cur = las,x = s[i] - 'A';
for(;s[i - pam[cur].len - 1] ^ s[i];cur = pam[cur].fa);
if(!pam[cur].ch[x])
{
int p = ++tot,q = pam[cur].fa;
pam[p].len = pam[cur].len + 2;
for(;s[i - pam[q].len - 1] ^ s[i];q = pam[q].fa);
pam[p].fa = pam[q].ch[x],pam[cur].ch[x] = p;
}
++pam[las = pam[cur].ch[x]].sz[pos];
}
inline void build()
{
for(register int i = tot;i > 1;--i)
pam[pam[i].fa].sz[0] += pam[i].sz[0],
pam[pam[i].fa].sz[1] += pam[i].sz[1],
ans += (long long)pam[i].sz[0] * pam[i].sz[1];
}
}
int main()
{
scanf("%s",s + 1),n = strlen(s + 1);
for(register int i = 1;i <= n;++i)
PAM::insert(s,i,0);
PAM::las = 1;
scanf("%s",s + 1),n = strlen(s + 1);
for(register int i = 1;i <= n;++i)
PAM::insert(s,i,1);
PAM::build();
printf("%lld\n",ans);
}