[CEOI2017]Palindromic Partitions

题目大意:

给出一个长度为\(n(n\le10^6)\)的只包含小写字母字符串,要求你将它划分成尽可能多的小块,使得这些小块构成回文串。

思路:

哈希以后从两侧往里贪心,尽量取短的。

时间复杂度\(\mathcal O(n)\)。

源代码:

#include<cstdio>
#include<cctype>
#include<cstring>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
typedef unsigned long long uint64;
const int N=1e6+2;
const uint64 base=33;
char s[N];
uint64 pwr[N],hash[N];
inline uint64 calc(const int &l,const int &r) {
return hash[r]-hash[l-1]*pwr[r-l+1];
}
int main() {
for(register int T=getint();T;T--) {
scanf("%s",&s[1]);
const int n=strlen(&s[1]);
for(register int i=pwr[0]=1;i<=n;i++) {
pwr[i]=pwr[i-1]*base;
hash[i]=hash[i-1]*base+s[i]-'a';
}
int last=0,ans=0;
for(register int i=1;i<=n/2;i++) {
if(calc(last+1,i)==calc(n-i+1,n-last)) {
last=i;
ans+=2;
}
}
if(last*2<n) ans++;
printf("%d\n",ans);
}
return 0;
}

[CEOI2017]Palindromic Partitions的更多相关文章

  1. 洛谷 P4656: LOJ 2484: [CEOI2017]Palindromic Partitions

    菜菜只能靠写简单字符串哈希维持生活. 题目传送门:LOJ #2484. 题意简述: 题面讲得很清楚了. 题解: 很显然从两边往中间推,能选的就选上这个贪心策略是对的. 如何判断能不能选上,直接字符串哈 ...

  2. [洛谷P4656][CEOI2017]Palindromic Partitions

    题目大意:一个长度为$n$的字符串,要求把它分成尽可能多的小块,使得这些块构成回文串 题解:贪心,从两边从找尽可能小的块使得左右的块相等,判断相等可以用$hash$ 卡点:无 C++ Code: #i ...

  3. LOJ2484 CEOI2017 Palindromic Partitions DP、回文树

    传送门 当我打开Luogu题解发现这道题可以Hash+贪心的时候我的内心是崩溃的-- 但是看到这道题不都应该认为这是一道PAM的练手好题么-- 首先把原字符串重排为\(s_1s_ks_2s_{k-1} ...

  4. poj 3790 Recursively Palindromic Partitions

    /*摘抄自博客:Recursively Palindromic Partitions Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...

  5. poj 3790 Recursively Palindromic Partitions (递推)

    题目 题意:求输入的数字的递归回文. 思路:答案等于这个数字一半之前的所有的 之和. #include <iostream> #include <cstdio> #includ ...

  6. LibreOJ 题解汇总

    目录 #1. A + B Problem #2. Hello, World! #3. Copycat #4. Quine #7. Input Test #100. 矩阵乘法 #101. 最大流 #10 ...

  7. NOI.AC NOIP模拟赛 第二场 补记

    NOI.AC NOIP模拟赛 第二场 补记 palindrome 题目大意: 同[CEOI2017]Palindromic Partitions string 同[TC11326]Impossible ...

  8. OJ题解记录计划

    容错声明: ①题目选自https://acm.ecnu.edu.cn/,不再检查题目删改情况 ②所有代码仅代表个人AC提交,不保证解法无误 E0001  A+B Problem First AC: 2 ...

  9. 最长回文子串-LeetCode 5 Longest Palindromic Substring

    题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

随机推荐

  1. css radial-gradient()函数用法

    radial:半径的:放射状的:射线:光线:径向 gradient:梯度,坡度:渐变 radial-gradient:径向渐变 radial-gradient()函数:用径向渐变创建函数.径向渐变由中 ...

  2. C根据排序字符串

    #include<stdio.h> #include<string.h> #include <stdlib.h> #define STR_LEN_MAX 100 c ...

  3. vue双向绑定原理分析

    当我们学习angular或者vue的时候,其双向绑定为我们开发带来了诸多便捷,今天我们就来分析一下vue双向绑定的原理. 简易vue源码地址:https://github.com/jiangzhenf ...

  4. WEBAPI 帖子收藏

    [翻译]ASP.NET Web API入门 [翻译]ASP.NET WEB API异常处理 ASP.NET WebAPI 路由规则与POST数据 ASP.NET Web API路由规则(二) ASP. ...

  5. (转载)mysql:“Access denied for user 'root'@'localhost'”

    原文地址:http://www.linuxidc.com/Linux/2007-05/4338.htm # /etc/init.d/mysql stop# mysqld_safe --user=mys ...

  6. mysql索引 B+tree

    一.B+tree示意图 二.为什么要用索引 1.索引能极大减少存储引擎需要扫描的数据量:因为索引有序所以可以快速查找并且不用全表查找: 2.索引可以把随机IO变为顺序IO:因为B+tree在数据中保存 ...

  7. jdk8 lambda表达式总结

    Java8 lambda表达式10个示例   1. 实现Runnable线程案例 使用() -> {} 替代匿名类: //Before Java 8: new Thread(new Runnab ...

  8. 和为k的最长子数组及其延伸

    问题1: /** * 问题描述: * 给定一个无序数组arr,其中元素可正.可负.可0, * 求arr所有的子数组中正数与负数个数相等的最长子数组长度 * * 解题思路:对数组进行处理,正数为1,负数 ...

  9. django orm按天统计发布单数量

    夜深了,先上代码和数据,明天再实现可视化图表. from datetime import datetime, timedelta from django.http import JsonRespons ...

  10. chunk writer 中需要对抛错的交易进行回滚,同时又要在其他表中记录是哪一笔交易记录失败

    首先根据我有限的知识判断,回滚之后进行写表,该写表动作只能使用listener来进行. 考虑使用的listener有:ItemWriteListener     StepExecutionListen ...