Problem Description
Alice get a string S. She thinks palindrome string is interesting. Now she wanna know how many three tuple (i,j,k) satisfy 1≤i≤j<k≤length(S) , S[i..j] and S[j+1..k] are all palindrome strings. It's easy for her. She wants to know the sum of i*k of all required three tuples. She can't solve it. So can you help her? The answer may be very large, please output the answer mod 1000000007.

A palindrome string is a string that is same when the string
is read from left to right as when the string is read from right to left.

 
Input
The input contains multiple test cases.

Each
test case contains one string. The length of string is between 1 and 1000000.
String only contains lowercase letter.

 
Output
For each test case output the answer mod
1000000007.
 
Sample Input
aaa
abc
 
Sample Output
14
8

题意: 找三元组(i,j,k) 使得[i,j]和[j+1,k]都是回文串。计算所有i*k的和模上1000000007.

解析:先跑一边manacher.然后处理4个数组

C[0][i]: 代表对于下标i的点,他所在的回文串(回文串在他右边)中心下标*2的和

C[1][i]: 代表对于下标i的点,他所在的回文串(回文串在他左边)中心下标*2的和

C[2][i]: 代表对于下标i的点,他所在的回文串个数(回文串在他右边)

C[3][i]: :代表对于下标i的点,他所在的回文串个数(回文串在他左边)

对于以x为左端点的回文串,那么它右边的所有k的和为C[0][x]-C[2][x]*x(想一想为甚么,得到的恰好是所有包含x为左端点的回文的长度),以

x为右端点同理。还要注意奇偶性,我参照了别人的写法。

代码:

#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
typedef long long LL;
const int mod=;
const int maxn=;
char org[maxn],S[maxn];
int p[maxn];
inline int manacher() //manacher实现部分
{
S[]='$'; S[]='#';
int len=;
int L=strlen(org);
for(int i=;i<L;i++)
{
S[len++]=org[i];
S[len++]='#';
}
S[len]='@';
S[len+]=;
int ri=,id;
for(int i=;i<len;i++)
{
if(ri>i) p[i]=min(p[*id-i],ri-i);
else p[i]=;
while(S[i+p[i]]==S[i-p[i]]) p[i]++;
if(i+p[i]>ri) { ri=i+p[i]; id=i; }
}
return len-;
}
int C[][maxn];
void Modify(int k,int l,int r,int v) //修改
{
if(l>r) return;
C[k][l]=(C[k][l]+v)%mod; //l位置要加上v
C[k][r+]=(C[k][r+]-v+mod)%mod; //r+1要减去v因为从l到r这一段是累加的,但是到r+1开始就要减掉了
}
int main()
{
while(scanf("%s",org)!=EOF)
{
int len=manacher();
memset(C,,sizeof(C));
for(int i=len;i>=;i--)
{
Modify(,i-p[i]+,i,i);
Modify(,i-p[i]+,i,);
}
for(int i=;i<=len;i++)
{
Modify(,i,i+p[i]-,i);
Modify(,i,i+p[i]-,);
}
for(int k=;k<;k++)
for(int i=;i<=len;i++) C[k][i]=(C[k][i]+C[k][i-])%mod;
int ans=;
for(int i=;i<len-;i+=)
{
int a=i,b=i+;
int lsum=(C[][b]-(LL)C[][b]*(b/)%mod+mod)%mod;
int rsum=(C[][a]-(LL)C[][a]*(a/)%mod+mod)%mod;
ans=(ans+(LL)lsum*rsum%mod)%mod;
}
printf("%d\n",ans);
}
return ;
}

Hdu5785-Interesting(回文串处理)的更多相关文章

  1. [LeetCode] Longest Palindrome 最长回文串

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  2. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  3. [LeetCode] Palindrome Partitioning II 拆分回文串之二

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  4. [LeetCode] Palindrome Partitioning 拆分回文串

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  5. [LeetCode] Longest Palindromic Substring 最长回文串

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  6. bzoj 3676 回文串 manachar+hash

    考虑每个回文串,它一定是它中心字母的最长回文串两侧去掉同样数量的字符后的一个子串. 所以我们可以用manachar求出每一位的回文半径,放到哈希表里并标记出它的下一个子串. 最后拓扑排序递推就行了.. ...

  7. BZOJ 3676: [Apio2014]回文串

    3676: [Apio2014]回文串 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 2013  Solved: 863[Submit][Status ...

  8. SPOJ - PLSQUARE Palin Squar(hash+回文串)

    题意:给你一个n*n (n<=200)的字符串矩阵,问你每行每列都是回文串的最大的m*m的矩阵是多少 题解:首先答案不满足单调性,即m成立而m-1与m+1都却不一定成立,所以必须枚举答案确定现在 ...

  9. 删除部分字符使其变成回文串问题——最长公共子序列(LCS)问题

    先要搞明白:最长公共子串和最长公共子序列的区别.    最长公共子串(Longest Common Substirng):连续 最长公共子序列(Longest Common Subsequence,L ...

随机推荐

  1. [ES6] Generators

    Example 1: function *topicList(){ yield "ES2015"; yield "Semi-colons: good or bad?&qu ...

  2. MDM 证书申请流程(vendor及customer)

    整个流程分为两部分:vendor,customer. 一.Vendor 1.成为一个 MDM Vendor 1) 首先你须要拥有一个 Apple Enterprise account($299/年). ...

  3. 基于akka实现简单的主从框架

    ========================Master============================== package com.scala.akka.rpc.demo2 import ...

  4. CentOS 6.8编译安装httpd2.2.31+MySQL5.6.31+PHP5.3.27

    CentOS 6.8编译安装httpd2.2.31+MySQL5.6.31+PHP5.3.27   说明:   操作系统:CentOS 6.8 32位 准备篇: 一.系统约定    软件源代码包存放位 ...

  5. CSS transform(变形)和transform-origin(变形原点)

    transform(变形)和transform-origin(变形原点)的说明: 目前这两个属性得到了除去ie以外各个主流浏览器webkit,firefox,opera的支持,属性名分别为 -webk ...

  6. Sass函数--颜色函数--HSL函数

    HSL函数简介HSL颜色函数包括哪些具体的函数,所起的作用是什么: hsl($hue,$saturation,$lightness):通过色相(hue).饱和度(saturation)和亮度(ligh ...

  7. Code First 数据注释--InverseProperty 和 ForeignKey

    ForeignKey 按照约定在Post类中看到BlogId属性,会认为是Blog类的外键,但是在Blog类中并没有BlogId属性,解决方法是,在 Post 中创建一个导航属性,并使用 Foreig ...

  8. FineUI表单验证

    自动编码文本 默认情况下,Label的EncodeText属性为true,会对文本中的HTML进行编码.当然我们也可以设置EncodeText=false,从而将HTML片段赋值给Text属性,请看这 ...

  9. Android Studio tips and tricks 翻译学习

    Android Studio tips and tricks 翻译 这里是原文的链接. 正文: 如果你对Android Studio和IntelliJ不熟悉,本页提供了一些建议,让你可以从最常见的任务 ...

  10. Winform 无边框随意拖动【转载】

    本篇技术内容转载自:http://www.cnblogs.com/ap0606122/archive/2012/10/23/2734964.html using System; using Syste ...