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. hdu3308--LCIS 最大连续递增序列长度

    这个是动态的,所以要用线段树维护.代码里有注释因为ls敲成lsum,rs敲成rsum查错查了好久.. #include <set> #include <map> #includ ...

  2. puppet cert maintain

  3. [转载]date命令时间转换

    Linux时间戳和标准时间的互转 在LINUX系统中,有许多场合都使用时间戳的方式表示时间,即从1970年1月1日起至当前的天数或秒数.如/etc/shadow里的密码更改日期和失效日期,还有代理服务 ...

  4. html5 手机APP计算高度问题

    安卓手机型号比较多,会出现bottom:0 找不到底部的问题,所以需要计算手机可视区域高度,这样便于使用百分比适配 (function(window,undefined){ /** * js_heig ...

  5. [Cycle.js] Making our toy DOM Driver more flexible

    Our previous toy DOM Driver is still primitive. We are only able to sends strings as the textContent ...

  6. [AngularJS + RxJS] Search with RxJS

    When doing search function, you always need to consider about the concurrent requests. AEvent ----(6 ...

  7. 住javaWeb分页实现(模拟百度首页)

    本文来源于 http://blog.csdn.net/tjpu_lin/article/details/41050475 近期在开发一个项目,项目中有非常多数据展示的模块.所以要用到分页,网上搜了非常 ...

  8. gnuplot常用技巧

      一.         基础篇: 在linux命令提示符下运行gnuplot命令启动,输入quit或q或exit退出. 1.plot命令 gnuplot> plot sin(x) with l ...

  9. Cogs 12 运输问题2 (有上下界网络流)

    #include <cstdlib> #include <algorithm> #include <cstring> #include <iostream&g ...

  10. poj 1190 DFS 不等式放缩进行剪枝

    F - (例题)不等式放缩 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submi ...