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. Display number of replies in disscussion board

    how to display number of replies in disscussion board I have a require about display the replies' nu ...

  2. vi高级技巧

    本文一般情况下用<c-字母>(里边的字母一般大小写无所谓,除非特别注明)表示按住ctrl同时按下相关字母,命令前加一个i 表示在插入模式下用这个命令 1. 选定文字/ 拷贝粘贴 v 为可视 ...

  3. 浏览器内核Trident/Gecko/WebKit/Presto

    “浏览器内核”主要指渲染引擎(Rendering Engine),负责解析网页语法(如HTML.JavaScript)并渲染.展示网页.因此,所谓的浏览器内核通常也就是指浏览器所采用的渲染引擎, 渲染 ...

  4. 并行计算基础&amp;编程模型与工具

    在当前计算机应用中,对快速并行计算的需求是广泛的,归纳起来,主要有三种类型的应用需求: 计算密集(Computer-Intensive)型应用,如大型科学project计算与数值模拟: 数据密集(Da ...

  5. 安装centos6.3

    废话少说,今天安装镜像文件.版本为centos6.3 1.首先,我们已经创建了一个空的虚拟机,此时,打开虚拟机,选择的镜像文件,点击ok自己下载 2.点击绿色的三角箭头,你会看到下面页面.(如果报错T ...

  6. [CSAPP笔记][第六章存储器层次结构]

    第六章 存储器层次结构 在简单模型中,存储器系统是一个线性的字节数组,CPU能够在一个常数访问每个存储器位置. 虽然是一个行之有效的模型,但没有反应现代系统实际工作方式. 实际上,存储器系统(memo ...

  7. python-文件压缩和解压

    import tarfile #压缩 tar = tarfile.open('your.tar','w') tar.add('ooo.xml',arcname='ooo.xml') tar.close ...

  8. HTML与CSS入门——第九章 使用颜色

    知识点: 1.为网站选择颜色的方法 2.颜色在Web上的工作方式 3.使用十六进制颜色值的方法 4.使用CSS设置背景.文本和边框颜色的方法 9.1 选择颜色的最佳方法: 直白地说:根据用户群体找到最 ...

  9. Server.HTMLEncode用法

    Server.HTMLEncode用法!! Server.HTMLEncode HTMLEncode 一.HTMLEncode 方法对指定的字符串应用 HTML 编码. 语法 Server.HTMLE ...

  10. 《第一行代码》学习笔记16-碎片Fragment(1)

    1.碎片( Fragment):一种可以嵌入在活动当中的UI片段,能让程序更加合理和充分地利用大屏幕的空间,在平板上的应用广泛. 2.碎片同样包括布局,有自己的生命周期,甚至可理解成一个迷你型的活动. ...