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 ...
随机推荐
- restTemplate工具类
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.sprin ...
- Redis 复制原理及特性
摘要 早期的RDBMS被设计为运行在单个CPU之上,读写操作都由经单个数据库实例完成,复制技术使得数据库的读写操作可以分散在运行于不同CPU之上的独立服务器上,Redis作为一个开源的.优秀的key- ...
- ps:矢量格式图像
假设我们写了一首新的乐曲,要把它交给唱片公司,可以通过两种方式: 把这首乐曲弹奏出来并录制在磁带上. 把这首乐曲的乐谱写下来. 这两种方式的最大区别在于记录的形式. 前者是记述性的.包含乐曲的音频信息 ...
- canvas toBlob ,ie兼容
/* canvas-toBlob.js * A canvas.toBlob() implementation. * 2016-05-26 * * By Eli Grey, http://eligrey ...
- 前端每日实战:35# 视频演示如何把 CSS 径向渐变用得出神入化,只用一个 DOM 元素就能画出国宝熊猫
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/odKrpy 可交互视频教程 此视频 ...
- orm 查询数据库随机返回一条数据的解决办法用models.User.objests.all().order_by('?').first()
- OC + RAC(七) RACSubject和RACSignal的区别
-(void)_test8{ /// RACSubject继承自RACSignal 但是RACSubject和RACSignal的区别? //1能接收1,2 //但是2只能接收2 RACSubject ...
- java扫描仪上传文件
问题: 项目中有一个功能,原来是用ckfinder做的,可以选择本地图片上传至服务器,然后将服务器的图片显示在浏览器中,并可以将图片地址保存到数据库:现在客户觉得麻烦,提出连接扫描仪扫描后直接上传至服 ...
- Oracle dmp文件(表)导入与导出
dmp文件是作为oracle导入和导出表使用的文件格式dmp文件导出dmp文件导出用的比较多的一般是三种,他们分别是:1.导出整个数据库实例下的所有数据2.导出指定用户的所有表3.导出指定表. 打开命 ...
- execute、executeQuery和executeUpdate之间的区别 转
转:http://blog.csdn.net/colin_fantasy/article/details/3898070 execute.executeQuery和executeUpdate之间的区别 ...