BZOJ4480:快乐的jyy(回文自动机)

题面
题意:给出两个串,问所有回文串在两个串出现次数之积之和。

应该是回文自动机果题
插完一个串后重置last,再插入另一个串
每个状态对于两个串分别统计Right集的大小
就可以统计答案了

#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;
#define mmst(a, b) memset(a, b, sizeof(a))
#define mmcp(a, b) memcpy(a, b, sizeof(b))

typedef long long LL;

const int N=200200;

int n;
int son[N][26],pre[N],len[N],r[N][2],last,cnt;
char s[N];
LL ans;

void Insert(int pos,int ops)
{
    int x=last,ch=s[pos]-'A';
    while(s[pos-len[x]-1]!=s[pos])
    x=pre[x];
    if (!son[x][ch])
    {
        len[++cnt]=len[x]+2;
        int y=pre[x];
        while(s[pos-len[y]-1]!=s[pos])
        y=pre[y];
        pre[cnt]=son[y][ch];
        son[x][ch]=cnt;
    }
    x=son[x][ch];
    r[x][ops]++;
    last=x;
}

int main()
{
    pre[1]=pre[0]=cnt=1;
    len[1]=-1;
    scanf("%s",s+1);
    n=strlen(s+1);

    for(int i=1;i<=n;i++)
    Insert(i,0);

    last=0;
    scanf("%s",s+1);
    n=strlen(s+1);
    for(int i=1;i<=n;i++)
    Insert(i,1);

    for(int i=cnt;i>=2;i--)
    if(pre[i]>1)
    r[pre[i]][0]+=r[i][0],r[pre[i]][1]+=r[i][1];

    for(int i=2;i<=cnt;i++)
    ans+=(LL)r[i][0]*r[i][1];

    cout<<ans<<endl;

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值