CodeChef 3-Palindromes(Manacher+dp)
3-PalindromesProblem 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)的更多相关文章
- cf245H Queries for Number of Palindromes (manacher+dp)
首先马拉车一遍(或者用hash),再做个前缀和处理出f[i][j]表示以j为右端点,左端点在[i,j]的回文串个数 然后设ans[i][j]是[i,j]之间的回文串个数,那就有ans[i][j]=an ...
- LightOJ 1033 Generating Palindromes(dp)
LightOJ 1033 Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- uva 11584 Partitioning by Palindromes 线性dp
// uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串 ...
- UVA 11584 "Partitioning by Palindromes"(DP+Manacher)
传送门 •题意 •思路一 定义 dp[i] 表示 0~i 的最少划分数: 首先,用马拉车算法求解出回文半径数组: 对于第 i 个字符 si,遍历 j (0 ≤ j < i),判断以 j 为回文中 ...
- 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 ...
- 51nod 1595 回文度 | 马拉车Manacher DP
51nod 1595 回文度 题目描述 如果长度为n的字符串是一个回文串,同时它的长度为floor(n/2)的前缀和后缀是K-1度回文串,则这个字符串被称为K度回文串.根据定义,任何字符串(即使是空字 ...
- HDU 5677 ztr loves substring(Manacher+dp+二进制分解)
题目链接:HDU 5677 ztr loves substring 题意:有n个字符串,任选k个回文子串,问其长度之和能否等于L. 题解:用manacher算法求出所有回文子串的长度,并记录各长度回文 ...
- BZOJ 3790 神奇项链(manacher+DP+树状数组)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3790 [题目大意] 问最少用几个回文串可以构成给出串,重叠部分可以合并 [题解] 我们 ...
- Hrbust 2363 Symmys (Manacher + DP)
题目链接 Hrbust 2363 来源 “科林明伦杯”哈尔滨理工大学第七届程序设计团队赛 Problem J 题意 给出一个长度为$1e6$的字符串,求最小可重回文子串覆盖数量 首先Manach ...
随机推荐
- dotnet ef执行报错, VS 2019发布时配置项中的Entity Framework迁移项显示不出来
VS 2019发布时配置项中的Entity Framework迁移项显示不出来 dotnet ef dbcontext list --json “无法执行,因为找不到指定的命令或文件.可能的原因包括: ...
- lvm分区创建和扩容
shell> fdisk /dev/xvdb #### 选择磁盘 Command (m for help): m #### 帮助 Command action a toggle a bootab ...
- Ubuntu环境下SSH服务安装、SSH远程登录以及SSH数据传输
https://www.cnblogs.com/asyang1/p/9467646.html SSH 为 Secure Shell 的缩写,为建立在应用层基础上的安全通信协议. 一.检查SSH服务是否 ...
- Linux忘记root密码解决方案
忘记Linux root密码时,只需重启Linux系统,然后引导进入Linux的单用户模式(init 1),由于单用户模式不需要输入登陆密码,因此,可直接登陆系统,修改root密码即可解决问题.需要说 ...
- wannafly25 E 01串
链接 wannafly25 E 01串 给出一个\(01\)串,有两种操作,操作一是将某一个位置的数字修改,操作二是询问某一个区间,将这个区间看做\(1\)个二进制数,可以随意加减\(2\)的幂次,问 ...
- Nginx+lua_Nginx+GraphicsMagick来实现实时缩略图
1.安装GraphicsMagick cd /usr/local/src wget http://sourceforge.net/projects/graphicsmagick/files/graph ...
- 关于pug的笔记
一.简介 Pug 是一款健壮.灵活.功能丰富的模板引擎,专门为 Node.js 平台开发.Pug 是由 Jade 改名而来,他可以帮助我们写html的时候更加的简单明了.安装.使用pug的过程打开cm ...
- vue+java后台通信报403,cors解决跨域问题(该贴说的不是很清楚,不过大概如此,可再去网上查相关内容)
前端是vue2.0,网络请求用的是axios,后端是springboot2.0 用axios向后端发送post请求,结果得到一个403无权限的错误,莫名其妙啊,我明明发送的是post请,但在chrom ...
- HAProxy+Heartbeat双节点出现VIP情况
本文使用heartbeat做高可用,主节点192.168.0.204,备节点192.168.0.205,vip192.168.0.206,防火墙启动状态 先启动主节点,再启动备节点后,发现以下问题: ...
- SSM项目web.xml等配置文件中如何查找类的全路径名?
如题, web.xml,applicationContext.xml 等配置文件中,有时不会出现自动提示类的名字,这时如何查找类的全路径名,如下图所示: 1.鼠标右键单击菜单栏Navigate选项,选 ...