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. IOS深入学习(12)之Archiving

    1 前言 本文介绍的是一个归档解档方法,也是编码和解码时候所做的事情,和如何进行,编码和归档其实就是将对象关系转化为字节流并且归档为特殊的文件,解码和解档是逆过程. 英文原文:http://blog. ...

  2. 关于bootstrap--表格(table的各种样式)

    1.table-striped:斑马线表格 2.table-bordered:带边框的表格 3.table-hover:鼠标悬停高亮的表格 4.table-condensed:紧凑型表格(单元格的内距 ...

  3. hadoop2对比hadoop1

    hadoop2对比hadoop1 1.体系结构 HDFS+MapReduce,共同点都是分布式的,主从关系结构. HDFS=一个NameNode+多个DataNode, NameNode含有我们用户存 ...

  4. 【转载】cocos2d-x2.2.3和android的平台环境

    这两天试图按照教程来学习写游戏移植到的横版过关Android在.在网上找了很多教程,但版本号变化.所使用的工具有细微的差别.所以,现在我们还没有准备好,阅读后,下面的文章.最后能够顺利您的手机上跑起来 ...

  5. lseek() 定位一个已经打开的文件

    Lseek lseek()的作用是,设置文件内容的读写位置. 每个打开的文件都有一个"当前文件偏移量",是一个非负整数,用以度量从文件开始处计算的字节数.通常,读写操作都是从当前文 ...

  6. openssl 非对称加密算法DSA命令详解

    1.DSA算法概述 DSA算法是美国的国家标准数字签名算法,它只能用户数字签名,而不能用户数据加密和密钥交换. DSA与RSA的生成方式不同,RSA是使用openssl提供的指令一次性的生成密钥(包括 ...

  7. C# 创建验证码图片

    using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; us ...

  8. java基础2

    //第一个程序 用super访问父类中被隐藏的成员变量和被重写的方法 package foxe; class superClass{ int x; superClass(){ x=4; System. ...

  9. MS-DOS命令列表

    以下是微软的DOS操作系统(MS-DOS)的DOS命令列表.其它DOS的命令和用法可能类似. 后期版本的 DOS 可以通过help命令来得到命令与参数列表,通过help <命令>或者< ...

  10. mysql配置文件转载

    #BEGIN CONFIG INFO#DESCR: 4GB RAM, 只使用InnoDB, ACID, 少量的连接, 队列负载大#TYPE: SYSTEM#END CONFIG INFO ## 此my ...