3-Palindromes

 
Problem code: PALIN3
 
 

All submissions for this problem are available.

Read problems statements in Mandarin Chinese and Russian as well.

Mike likes strings. He is also interested in algorithms. A few days ago he discovered for himself a very nice problem:


You are given a digit string S. You need to count the number of substrings of S, which are palindromes.

Do you know how to solve it? Good. Mike will make the problem a little bit more difficult for you.


You are given a digit string S. You need to count the number of substrings of S, which are palindromes without leading zeros and can be divided by 3 without a remainder.

A string is a palindrome if it reads the same backward as forward. A
string is a palindrome without leading zeros if it reads the same
backward as forward and doesn't start with symbol '0'. A string is a
digit string, if it doesn't contain any symbols except '0', '1', '2',
..., '9'.

Please, note that you should consider string "0" as a palindrome without leading zeros.

Input

The first line of the input contains a digit string S.

Output

Your output should contain the only integer, denoting the number of substrings of S, which are palindromes without leading zeros and can be divided by 3 without a remainder.

Constraints

1 ≤ |S| ≤ 1 000 000

Example

Input:
1045003 Output:
4

Explanation

In the example you should count S[2..2] = "0", S[5..5] = "0", S[6..6] = "0" and S[7..7] = "3".

给出一个数字串。

问有多少个子串既是回文串也能被3整除~

先用Manacher处理好串的回文串长度。

然后用一个数组 cnt[i][j] 表示sigma( 1 ~  i-1 到i 的数 ) %3 == j 串的个数。

#include <bits/stdc++.h>

using namespace std;
typedef long long LL ;
typedef pair<LL,LL> pii;
#define X first
#define Y second
const int N = ;
char Ma[N] , s[N];
int Mp[N] , len ;
void Manacher( char s[] , int len ) {
int l = ;
Ma[l++] = '$' ; Ma[l++] = '#' ;
for( int i = ; i < len ; ++i ) {
Ma[l++] = s[i];
Ma[l++] = '#' ;
}
Ma[l] = ; int mx = , id = ;
for( int i = ; i < l ; ++i ) {
Mp[i] = mx>i?min(Mp[*id-i],mx-i):;
while( Ma[i+Mp[i]] == Ma[i-Mp[i]] ) {
Mp[i]++;
}
if( i + Mp[i] > mx ) {
mx = i + Mp[i];
id = i ;
}
}
} bool is_dig( char op ) {
if( op >= '' && op <= '' ) return true ;
return false ;
}
LL cnt[N][] , sum[N] ; void Run() {
int n = strlen(s) ;
Manacher(s,n);
len = * n + ;
memset( sum , , sizeof sum );
memset( cnt , , sizeof cnt );
for( int i = ; i < len ; ++i ){
sum[i] = sum[i-];
if( is_dig(Ma[i]) ) sum[i] += ( Ma[i] - '' );
}
for( int i = ; i < len ; ++i ) {
if( !is_dig(Ma[i]) || Ma[i] == '' ) {
for( int j = ; j < ; ++j )
cnt[i][j] = cnt[i-][j];
}
else {
int x = Ma[i] - '' ;
for( int j = ; j < ; ++j ) {
int _j = (j+x)%;
cnt[i][_j] += cnt[i-][j] ;
}
cnt[i][x%]++;
}
}
LL ans = ;
for( int i = ; i < len ; ++i ) {
int x = , tmp = sum[i-] - sum[i-Mp[i]] ;
if( is_dig(Ma[i]) ) {
x = Ma[i] - '' ;
if( x % == ) ans++ ;
}
for( int j = ; j < ; ++j ) {
if( ( *j + x )% == ) {
ans += cnt[i-][j];
for( int z = ; z < ; ++z ) if( (z+tmp)% == j ){
ans -= cnt[i-Mp[i]][z];
}
}
}
}
printf("%lld\n",ans);
} int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
int _ , cas = ;
while( scanf("%s",s) != EOF )Run();
}

CodeChef 3-Palindromes(Manacher+dp)的更多相关文章

  1. cf245H Queries for Number of Palindromes (manacher+dp)

    首先马拉车一遍(或者用hash),再做个前缀和处理出f[i][j]表示以j为右端点,左端点在[i,j]的回文串个数 然后设ans[i][j]是[i,j]之间的回文串个数,那就有ans[i][j]=an ...

  2. LightOJ 1033 Generating Palindromes(dp)

    LightOJ 1033  Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  3. uva 11584 Partitioning by Palindromes 线性dp

    // uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串 ...

  4. UVA 11584 "Partitioning by Palindromes"(DP+Manacher)

    传送门 •题意 •思路一 定义 dp[i] 表示 0~i 的最少划分数: 首先,用马拉车算法求解出回文半径数组: 对于第 i 个字符 si,遍历 j (0 ≤ j < i),判断以 j 为回文中 ...

  5. UVA - 11584 Partitioning by Palindromes[序列DP]

    UVA - 11584 Partitioning by Palindromes We say a sequence of char- acters is a palindrome if it is t ...

  6. 51nod 1595 回文度 | 马拉车Manacher DP

    51nod 1595 回文度 题目描述 如果长度为n的字符串是一个回文串,同时它的长度为floor(n/2)的前缀和后缀是K-1度回文串,则这个字符串被称为K度回文串.根据定义,任何字符串(即使是空字 ...

  7. HDU 5677 ztr loves substring(Manacher+dp+二进制分解)

    题目链接:HDU 5677 ztr loves substring 题意:有n个字符串,任选k个回文子串,问其长度之和能否等于L. 题解:用manacher算法求出所有回文子串的长度,并记录各长度回文 ...

  8. BZOJ 3790 神奇项链(manacher+DP+树状数组)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3790 [题目大意] 问最少用几个回文串可以构成给出串,重叠部分可以合并 [题解] 我们 ...

  9. Hrbust 2363 Symmys (Manacher + DP)

    题目链接  Hrbust 2363 来源  “科林明伦杯”哈尔滨理工大学第七届程序设计团队赛 Problem J 题意  给出一个长度为$1e6$的字符串,求最小可重回文子串覆盖数量 首先Manach ...

随机推荐

  1. 银联银行卡查询服务-dubbo实现

    最近看到银联开放了一个银行卡查询的服务,具体内容见官网https://open.unionpay.com/tjweb/api/detail?apiSvcId=51 尝尝鲜 在文档下载目录下,下载upa ...

  2. linux强制用户下线命令

    linux强制用户下线命令   前提:必须是root权限操作:(1)使用who查看目前有哪些用户登录了服务器,见下图(2)使用pkill -kill -t pts/1命令踢出第一个用户.命令解释:pt ...

  3. kubernetes之二 使用minikube创建单节点k8s本地集群

    使用Minikube来运行kubernetes集群是最简单.快捷的途径.Minikube是一个构建单节点集群的工具,对于测试Kubernetes和本地开发应用都非常有用.官方安装minikube教程请 ...

  4. go入门收集(转)

    go mod 使用 原文地址: https://juejin.im/post/5c8e503a6fb9a070d878184a  

  5. linux运维、架构之路-Nginx提高

    一.虚拟主机搭建 1.基于域名的虚拟主机 [root@web01 html]# cat nginx.conf worker_processes ; events { worker_connection ...

  6. 2017ICPC沈阳网络赛 HDU 6205 -- card card card(最大子段和)

    card card card Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. 【HDOJ6651】Final Exam(贪心)

    题意:有n门课,价值之和为m,每门课的价值可能是0到m 一门价值为x的课需要花至少x+1时间准备才能通过 问不管价值如何分配都能通过至少k门课的最小总准备时间 m,n,k<=1e9 思路: #i ...

  8. Nginx负载均衡与反向代理—《亿级流量网站架构核心技术》

    当我们的应用单实例不能支撑用户请求时,此时就需要扩容,从一台服务器扩容到两台.几十台.几百台.然而,用户访问时是通过如http://www.XX.com的方式访问,在请求时,浏览器首先会查询DNS服务 ...

  9. poj3744 (概率DP+矩阵快速幂)

    http://poj.org/problem?id=3744 题意:在一条铺满地雷的路上,你现在的起点在1处.在N个点处布有地雷,1<=N<=10.地雷点的坐标范围:[1,10000000 ...

  10. ANTLR4加载csv数据

    实现功能: 编写一个自定义的监听器,将逗号分隔符文件(csv)中的数据加载到一种数据结构--“由Map组成的List”中. antlr4文件: grammar CSV; file : hdr row+ ...