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. 算法导论(第三版)Exercises4.2(第四章二节)

    4.2-1(计算结果) 18  14 62  66 4.2-2(Strassen算法计算矩阵乘法) void multiplyMatrix(int a[], int b[], int n, int r ...

  2. day55

    担心了好久的编译原理也总是考完了 大学里的最后一次考试也是结束罗 这次的考试起伏跌宕啊 我们本来是9点钟开始考试 但是我们班的几个同学基本上7点钟就去了 为了什么?? 选个好的位置撒哈哈,到了九点,老 ...

  3. c指针点滴5-指针变量计算

    //接口dll _declspec(dllexport) void go() { char *p1; int *p2; p1 = (char*)0x30fa83;//每次运行exe的时候输出地址值不同 ...

  4. AndroidStudio常见提示

    Required:请求的是String字符串 .     Found:   et.getText()返回的是text.Editable

  5. Python安装MySQLdb并连接MySQL数据库

    当然了,前提是你已经安装了Python和MySQL.我的Python是2.6版本的. Python2.6的“Set”有点兼容性问题,自己照着改一下: http://sourceforge.net/fo ...

  6. JMeter脚本参数化和断言设置( CSV Data Set Config )

    用Badboy录制了Jmeter的脚本,用Jmeter打开后形成了原始的脚本.但是在实际应用中,为了增强脚本的多样性,就要使脚本参数化.这里我以登录为例,参数化用户账号与用户密码.  图1 :原始脚本 ...

  7. 选择Android还是IOS开发?

    选择Android还是IOS? 随着移动互联网的如日中天,涌现了越来越多的开发者.IOS优秀的用户体验,Android极高的用户群,这对于开发者来说陷入了选择困难的境地,尤其是新入门的开发者,精力有限 ...

  8. hdu1047(模拟大量的循环添加)

    标题信息:总结多个大整数,(使用add循环相加的功能) http://acm.hdu.edu.cn/showproblem.php? pid=1047 AC代码: /**  *大数的循环加法,转化为字 ...

  9. javascript构造函数+原形继承+作用域扩充

    目前为止我认为这是最佳的声明对象的模式,将今天学到的东西写下 <script type="text/javascript"> function Constructor( ...

  10. UVA10562 数据结构题目

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...