又是一个广义 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
56
57
58
59
60
61
using namespace std;
const int N = 521634;
int n,m;
char s[N + 5];
int ans,cnt;
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];
if(pam[i].sz[0] && pam[i].sz[1])
if(pam[i].len > ans)
ans = pam[i].len,cnt = 1;
else if(pam[i].len == ans)
++cnt;
}
}
}
int main()
{
scanf("%d%d%s",&n,&m,s + 1);
for(register int i = 1;i <= n;++i)
PAM::insert(s,i,0);
PAM::las = 1;
scanf("%s",s + 1);
for(register int i = 1;i <= m;++i)
PAM::insert(s,i,1);
PAM::build();
printf("%d %d\n",ans,cnt);
}