CC 3-Palindromes(manacher)

传送门:3-Palindromes

题意:求为回文串且能整除3且不前导0的子串个数。

分析:由 manacher算法O(N)可算出以i为坐标的最长为p[i]回文子串,且Si-k,Si-k+1......Si+k-1,Si+k(0<k<p[i])全为回文串。

又知,能整除3的整数数位和也能整除3,那么只要Si-k,Si-k+1......Si+k-1,Si+k和整除3即可。

由回文串对称性知Si-k==Si-k,那么只要Si-k..Si-1这段中模3余数与Si模3余数相同,Si-k...Si+k和必定整除3(设左右各位余数x+本身x=3x).

因此只要预处理出Si...Sj整段中模3余0,1,2的个数,就可O(N)得出全部符合条件的子串。

#pragma comment(linker,"/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <limits.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 1000000007
#define inf 0x3f3f3f3f
#define eps 1e-6
#define N 1000010
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PII pair<int,int>
using namespace std;
inline LL read()
{
    char ch=getchar();LL x=0,f=1;
    while(ch>‘9‘||ch<‘0‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch<=‘9‘&&ch>=‘0‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
int p[N<<1],len,num,mx,id;
char s[N],str[N<<1];
void build()
{
    len=strlen(s);num=0;
    str[num++]=‘@‘;str[num++]=‘#‘;
    for(int i=0;i<len;i++)
    {
        str[num++]=s[i];
        str[num++]=‘#‘;
    }
    str[num]=0;
}
void manacher()
{
    mx=0;
    memset(p,0,sizeof(p));
    for(int i=1;i<num;i++)
    {
        if(mx>i)p[i]=min(p[2*id-i],mx-i);
        else p[i]=1;
        while(str[i-p[i]]==str[i+p[i]])p[i]++;
        if(p[i]+i>mx)mx=p[i]+i,id=i;
    }
}
int a[N<<1],sum[N<<1][3];
void solve()
{
    for(int i=2;i<num;i++)
    {
        a[i]=a[i-1];//前缀和
        if(str[i]!=‘#‘)a[i]=(a[i]+str[i]-‘0‘)%3;
        for(int j=0;j<3;j++)sum[i][j]=sum[i-1][j];
        if(str[i]!=‘#‘&&str[i]!=‘0‘)
        sum[i][a[i]]++;
    }
    LL ans=0;
    for(int i=2;i<num;i++)
    {
        int t=(str[i]-‘0‘)%3;
        if(str[i]==‘#‘)t=0;
        if(str[i]!=‘#‘&&t==0)ans++;
        int k=(t+a[i])%3;//由于sum[i+p[i]-1][k]~sum[i][k]都多了a[i],因此补回来防止误差
        ans+=sum[i+p[i]-1][k]-sum[i][k];
    }
    printf("%lld\n",ans);
}
int main()
{
    while(scanf("%s",s)>0)
    {
        build();
        manacher();
        solve();
    }
}

时间: 2024-08-25 00:30:35

CC 3-Palindromes(manacher)的相关文章

HDU 5340 Three Palindromes(Manacher)

Problem Description: Can we divided a given string S into three nonempty palindromes? Input: First line contains a single integer T≤20 which denotes the number of test cases. For each test case , there is an single line contains a string S which only

【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)

[SPOJ]NUMOFPAL - Number of Palindromes(Manacher,回文树) 题面 洛谷 求一个串中包含几个回文串 题解 Manacher傻逼题 只是用回文树写写而已.. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<

hdu3294Girls&#39; research(manacher)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3294 题目不难,感觉输出比较麻烦. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int maxn=2000010; 6 char s[maxn<<1]; 7 int r[maxn<<1]; 8 char ans[

HDU 4513 吉哥系列故事——完美队形II(Manacher)

Problem Description 吉哥又想出了一个新的完美队形游戏! 假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出一些人,让这些人形成一个新的队形,新的队形若满足以下三点要求,则就是新的完美队形: 1.挑出的人保持原队形的相对顺序不变,且必须都是在原队形中连续的: 2.左右对称,假设有m个人形成新的队形,则第1个人和第m个人身高相同,第2个人和第m-1个人身高相同,依此类推,当然如果m是奇数,中间那个人可以任意: 3.从左到中间那

O(n)回文子串(Manacher)算法

O(n)回文子串(Manacher)算法 资料来源网络 参见:http://www.felix021.com/blog/read.php?2040 问题描述: 输入一个字符串,求出其中最大的回文子串.子串的含义是:在原串中连续出现的字符串片段.回文的含义是:正着看和倒着看相同,如abba和yyxyy. 解析: 这里介绍O(n)回文子串(Manacher)算法 算法基本要点:首 先用一个非常巧妙的方式,将所有可能的奇数/偶数长度的回文子串都转换成了奇数长度:在每个字符的两边都插入一个特殊的符号.比

算法导论:回文子串(Manacher)算法 ,O(n)时间效率实现

问题描述: 输入一个字符串,求出其中最大的回文子串.子串的含义是:在原串中连续出现的字符串片段.回文的含义是:正着看和倒着看相同,如abba和yyxyy. 解析: 这里介绍O(n)回文子串(Manacher)算法 算法基本要点:首先用一个非常巧妙的方式,将所有可能的奇数/偶数长度的回文子串都转换成了奇数长度: 在每个字符的两边都插入一个特殊的符号.比如 abba 变成 #a#b#b#a#, aba变成 #a#b#a#. 为了进一步减少编码的复杂度,可以在字符串的开始加入另一个特殊字符,这样就不用

HDU----(3294)Girls&#39; research(manacher)

Girls' research Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 537    Accepted Submission(s): 199 Problem Description One day, sailormoon girls are so delighted that they intend to research abo

hdu5340—Three Palindromes—(Manacher算法)——回文子串

Three Palindromes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1948    Accepted Submission(s): 687 Problem Description Can we divided a given string S into three nonempty palindromes? Input F

codeforce No to Palindromes!(枚举)

1 /* 2 题意:给定一个字符串中没有任何长度>1的回文子串!求按照字典序的该串的下一个字符串 3 也不包含长度>1的任何回文子串! 4 5 思路:从最低位进行枚举,保证第i位 不与 第 i-1位和第 i-2位相同就好了!那么因为前边i-1 6 位没有长度>1的回文子串,那么前i位也不会出现!最后将最后边的字符按照相同的原则补齐就好了! 7 */ 8 #include<iostream> 9 #include<cstdio> 10 #include<cst