最长公共回文子串(Longest_Common_Palindrome_Substring)

38 篇文章 0 订阅

一、基本概念

最长公共回文子串(Longest_Common_Palindrome_Substring):两个字符串中最长的公共子串并且是回文字符串的子串

二、算法

 

三、代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int,int> pii;
 
const int N=2e5+5;
int ch[N][26],fa[N],len[N],ct,last;
int tag[N];
void ini();
int newnode();
void extend(int,int);
char s[N];
void work();
 
int main(){
//    freopen("in.txt","r",stdin);
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
//    int T;cin>>T;
//    while(T--)
    work();
}
void work(){
    while(cin>>s+1){
    ini();
    for(int i=1;s[i];i++) extend(s[i]-'a',i),tag[last]=1;
    cin>>s+1;
    int ans=0;
    last=0;
    for(int i=1;s[i];i++){
        extend(s[i]-'a',i);
        if(tag[last]) ans=max(ans,len[last]);
    }
    cout<<ans<<'\n';
    }
}
void extend(int c,int i){
    int p=last;
    while(s[i-len[p]-1]!=s[i]) p=fa[p];
    if(!ch[p][c]){
        int np=newnode(),q=fa[p];
        while(s[i-len[q]-1]!=s[i]) q=fa[q];
        fa[np]=ch[q][c],ch[p][c]=np;
        len[np]=len[p]+2;
    }
    last=ch[p][c];
}
void ini(){
    fa[0]=fa[1]=1;
    len[1]=ct=-1;
    last=0;
    newnode(),newnode();
}
int newnode(){
    memset(ch[++ct],0,sizeof(ch[0]));
    tag[ct]=0;
    return ct;
}

四、例题

https://ac.nowcoder.com/acm/contest/908/H

五、参考文章

https://blog.csdn.net/qq_36551189/article/details/79245675?tdsourcetag=s_pcqq_aiomsg

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Starzkg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值