[Codeforces Global Round 7] D2. Prefix-Suffix Palindrome (Hard version) (回文串,字符串hash)

题意

You are given a string s, consisting of lowercase English letters. Find the longest string, t, which satisfies the following conditions:

  1. The length of t does not exceed the length of s.
  2. t is a palindrome.
  3. There exists two strings a and b (possibly empty), such that t=a+b ( “+” represents concatenation), and a is prefix of s while b is suffix of s.
    记找s的一个不重叠的前缀a和后缀b,使得a+b是回文串且尽量长

样例

input:
5
a
abcdfdcecba
abbaxyzyx
codeforces
acbba

output:
a
abcdfdcba
xyzyx
c
abba

首先前后一一匹配,比方说abccbba,找出前缀ab后缀ba,剩下的ccb找一个最长回文前缀(cc),后缀(b)选最长的

最长回文前缀问题可以用字符串哈希解决,求一遍正向哈希,再求一遍倒着的哈希,如果是回文hash值相同,这里用的是BDKRHash,它可以O(1)求子串hash值,另外hash是非完美算法,可以在O(n)检验一下是不是回文串

#include <iostream>
#include <cstdio>
using namespace std;

typedef unsigned long long LL;
#define base 33
LL p[1000100];
LL h1[1000100],h2[1000100];

inline LL gethash1(int l,int r) {
   
    LL tp = l?h1[l-1]:0;
    return h1[r] - tp*p[r-l+1];
}

inline LL gethash2(int l,int r) {
   
    LL tp = l?h2[l-1]:0;
    return h2[r
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值