思路:

\(50pts\)

\(f[l,r]\)表示区间\([l,r]\)能够变成多少个串,转移枚举\(l\),利用\(hash\)判字符串相等。

复杂度\(O(Tn^3)\)

\(70pts\)

考虑优化,发现\(f[1,n]\)的贡献来源于每个\(f[i,n - i + 1]\),所以dp过程降低复杂度为\(O(Tn^2)\)。

\(100pts\)

枚举\(border\)每次贪心的砍\(border\),被卡单\(hash\)一脸不爽\(.jpg\)

不过判相等如果泥工\(kmp\),恭喜你,贪心和没贪一样,因为复杂度还是\(O(n^2)\)

复杂度\(O(Tn)\)


#include <bits/stdc++.h>
using namespace std;
const int bse1 = 29;
const int bse2 = 33;
const int mod1 = 1e9+7;
const int mod2 = 1e9+9;
#define ull unsigned long long
const int maxn = 10000010;
char s[maxn];
ull hsh[maxn][2];
ull pw1[maxn];
ull pw2[maxn];
int T;
inline void pre () {
pw1[0] = pw2[0] = 1;
for(int i = 1;i < maxn; ++i) {
pw1[i] = pw1[i - 1] * bse1 % mod1;
pw2[i] = pw2[i - 1] * bse2 % mod2;
}
}
inline ull cal1(int l,int r) {
return (hsh[r + 1][0] - hsh[l][0] * pw1[r - l + 1] % mod1 + mod1) % mod1;
}
inline ull cal2(int l,int r) {
return (hsh[r + 1][1] - hsh[l][1] * pw2[r - l + 1] % mod2 + mod2) % mod2;
}
inline bool check(int x,int y,int l,int r) {
return cal1(x,y) == cal1(l,r) && cal2(x,y) == cal2(l,r);
}
inline int solve(int l,int r) {
if(l > r) return 0;
for(int i = l;i < (r - (i - l)); ++i) {
if(check(l,i,(r - (i - l)),r)) {
return solve(i + 1,r - (i - l) - 1) + 2;
}
}
return 1;
}
int main () {
pre();
scanf("%d",&T);
while(T--) {
scanf("%s",s);
int len = strlen(s);
for(int i = 0;i < len; ++i) {
hsh[i + 1][0] = (hsh[i][0] * bse1 + s[i] - 'a') % mod1;
hsh[i + 1][1] = (hsh[i][1] * bse2 + s[i] - 'a') % mod2;
}
printf("%d\n",solve(0,len - 1));
}
return 0;
}

[NOI.AC] palindrome的更多相关文章

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

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

  2. # NOI.AC省选赛 第五场T1 子集,与&最大值

    NOI.AC省选赛 第五场T1 A. Mas的童年 题目链接 http://noi.ac/problem/309 思路 0x00 \(n^2\)的暴力挺简单的. ans=max(ans,xor[j-1 ...

  3. NOI.ac #31 MST DP、哈希

    题目传送门:http://noi.ac/problem/31 一道思路好题考虑模拟$Kruskal$的加边方式,然后能够发现非最小生成树边只能在一个已经由边权更小的边连成的连通块中,而树边一定会让两个 ...

  4. NOI.AC NOIP模拟赛 第五场 游记

    NOI.AC NOIP模拟赛 第五场 游记 count 题目大意: 长度为\(n+1(n\le10^5)\)的序列\(A\),其中的每个数都是不大于\(n\)的正整数,且\(n\)以内每个正整数至少出 ...

  5. NOI.AC NOIP模拟赛 第六场 游记

    NOI.AC NOIP模拟赛 第六场 游记 queen 题目大意: 在一个\(n\times n(n\le10^5)\)的棋盘上,放有\(m(m\le10^5)\)个皇后,其中每一个皇后都可以向上.下 ...

  6. NOI.AC NOIP模拟赛 第一场 补记

    NOI.AC NOIP模拟赛 第一场 补记 candy 题目大意: 有两个超市,每个超市有\(n(n\le10^5)\)个糖,每个糖\(W\)元.每颗糖有一个愉悦度,其中,第一家商店中的第\(i\)颗 ...

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

    NOI.AC NOIP模拟赛 第四场 补记 子图 题目大意: 一张\(n(n\le5\times10^5)\)个点,\(m(m\le5\times10^5)\)条边的无向图.删去第\(i\)条边需要\ ...

  8. NOI.AC NOIP模拟赛 第三场 补记

    NOI.AC NOIP模拟赛 第三场 补记 列队 题目大意: 给定一个\(n\times m(n,m\le1000)\)的矩阵,每个格子上有一个数\(w_{i,j}\).保证\(w_{i,j}\)互不 ...

  9. NOI.AC WC模拟赛

    4C(容斥) http://noi.ac/contest/56/problem/25 同时交换一行或一列对答案显然没有影响,于是将行列均从大到小排序,每次处理限制相同的一段行列(呈一个L形). 问题变 ...

随机推荐

  1. leetcode-164周赛-1268-搜索推荐系统

    题目描述: 自己的提交: class Solution: def suggestedProducts(self, products: List[str], searchWord: str) -> ...

  2. Linux常用命令入门文件、网络、系统及其他操作命令

    Linux常用命令入门文件.网络.系统及其他操作命令.压缩 归档 文件系统 系统管理 用户管理  网络管理 finger 相关命令 netstat ping rsh telnet wget 进程管理等 ...

  3. hdu1848 Fibonacci again and again [组合游戏]

    http://acm.hdu.edu.cn/showproblem.php?pid=1848 Problem Description 任何一个大学生对菲波那契数列(Fibonacci numbers) ...

  4. 基于aop的日志记录方式实现

    说明 最近有个项目需要增加日志记录功能,因为这个项目原来是基于spring开发的,在查阅了相关资料以后,我采用了spring aop的方式实现该需求,然后就有了本篇文章. 思路 我这边需求是这样的:要 ...

  5. vue简单的input校验手机号

    <input class="phone input-style " :class="{'err-input' : phone.err}" v-model= ...

  6. (转)Vmware vSphere 5.0系列教程 vSphere网络原理及vSwitch简介 及一个host两个网卡说明

    转:http://andygao.blog.51cto.com/323260/817518/ 在一个物理网络拓扑中,通常都是路由器-交换机-PC机的连接,不同的服务器和PC机,通过交换机的连接而相互连 ...

  7. cgo 和 Go 语言是两码事

    cgo不是Go 借用 JWZ的一句话 有些人,当他们面临一个问题时,认为“我知道,我会使用 cgo ”.那么现在,他们有了两个问题. 最近有人在 Gopher 的 Slack Channel 上使用 ...

  8. 无法将 Ethernet0 连接到虚拟网络”VMnet0″ 详细信息可以在 vmware.log 文件中找到未能连接虚拟机Ethernet0

    在 vmware“编辑->虚拟网络设置”里面,点“恢复默认”可解决.  

  9. LaTex 插入图像,以及应用表格

    插入图像 参考:http://www.ctex.org/documents/latex/graphics/ 1: \includegraphics[width=20mm]{head.png} 应用表格 ...

  10. EM相关两个算法 k-mean算法和混合高斯模型

    转自http://www.cnblogs.com/jerrylead/archive/2011/04/06/2006924.html http://www.cnblogs.com/jerrylead/ ...