#427 Div2 D

题意

给出一个字符串,求它的子串中为 \(k-palindrome\) 的个数。

\(1-palindrome\) 要求是一个回文串。

\(k-palindrome (k > 1)\)满足以下条件:

  • 是一个回文串
  • 它的左右两边是一个不为空的 \((k - 1)-palindromes\) 。

分析

求字符串回文子串个数是一个很经典的题目了,这题变化也不大。

\(dp[i]\) 表示以当前字符结尾的长度为 \(i\) 的字符串 \(k\) 的最大值。

注意 \(2-palindrome\) 同时也是 \(1-palindrome\) 。

code

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
char s[5005];
int dp[5005];
int ans[5005];
int main() {
scanf("%s", s + 1);
int len = strlen(s + 1);
int sum = 0;
vector<int> v1, v2;
for(int i = 1; i <= len; i++) {
ans[1]++; ans[2]--;
v2.push_back(1);
dp[1] = 1;
if(s[i] == s[i - 1]) {
v2.push_back(2);
ans[1]++; ans[2]--;
ans[2]++; ans[3]--;
dp[2] = 2;
}
for(int j = 0; j < v1.size(); j++) {
int l = v1[j];
if(s[i - l - 1] == s[i]) {
v2.push_back(l + 2);
dp[l + 2] += dp[l / 2 + 1] + 1;
ans[1]++; ans[dp[l + 2] + 1]--;
}
}
memset(dp, 0, sizeof dp);
v1 = v2;
v2.clear();
}
for(int i = 1; i <= len; i++) {
ans[i] += ans[i - 1];
}
for(int i = 1; i <= len; i++) {
printf("%d%c", ans[i], " \n"[i == len]);
}
return 0;
}

最新文章

  1. xamarin.Android 标记1
  2. Python中MySQLdb模块的安装
  3. 添加Mysql到Windows系统服务
  4. vi基本命令
  5. JAVA的Random类[转]
  6. nodejs socket
  7. work6
  8. tk资料
  9. 工作无聊,闲来无事,自己学习 android入门
  10. 如何在BIOS里设置定时关机?
  11. Thrift 入门教程
  12. 系统重启后,mr程序不生成当前时间段的MRx文件问题
  13. raise ValueError(&quot;Cannot convert {0!r} to Excel&quot;.format(value))
  14. 学习笔记——在vue中如何配置Jest(一)
  15. easyui中对数据的判断来显示,formatter控制
  16. 什么是java OOM?如何分析及解决oom问题?
  17. bzoj1503 Splay 维护名次数,支持删除
  18. 冲刺ing-6
  19. C#修改系统环境变量,调用批处理bat
  20. 存储器的保护(三)——《x86汇编语言:从实模式到保护模式》读书笔记20

热门文章

  1. 树剖模板by fcdalao
  2. 【BZOJ3038】上帝造题的七分钟2 线段树
  3. [NOIP2016]换教室 期望dp
  4. JS获取当前时间及时间戳相互转换
  5. JAVA List 一边遍历一边删除元素
  6. Leetcode 45. Jump Game II(贪心)
  7. Install the AWS Command Line Interface on Linux
  8. Java并发(10)- 简单聊聊JDK中的七大阻塞队列
  9. bzoj1575 [Usaco2009 Jan]气象牛Baric
  10. 金中欢乐赛 A题