【Codeforces Round #427 (Div. 2) D】Palindromic characteristics
【Link】:http://codeforces.com/contest/835/problem/D
【Description】
给你一个字符串;
让你在其中找到1..k阶的回文子串;
并统计它们的数量
如果一个字符串是一个回文串,则它可以是1阶子串;
k阶字符串,要求它的左边和右边都是k-1阶子串;
【Solution】
bo[i][j]表示i..j这一段是否为回文;
可以用O(n2)的复杂度处理出整个bo数组;
然后O(n2)枚举每一段区间;
算出这个区间的字符串最大可以是一个几阶字符串记为k;
ans[k]++;
如何算?
递归!
void getk(int l,int r)
如果l..r这一段不是回文,则直接返回0;
可以认为他是”0”阶子串;
如果l..r这一段是回文;
那么就只要在l..mid和mid..r这两段中选一段;
再递归求它的字符串阶数+1就好了;
不用两个都算!
则这里复杂度为O(log2n)
总的复杂度为O(n2log2n)
最后;
阶数为i的字符串也被认为是阶数为1..i的字符串;
所以
for (int i = n-1;i >= 1;i–)
ans[i]+=ans[i+1];
【NumberOf WA】
2
【Reviw】
我一开始,想的是,从低阶的字符串推出高阶的字符串
没有考虑到,这样增长得是很快的;
竟然没有想到逆向..
【Code】
#include <bits/stdc++.h>
using namespace std;
const int N = 5e3;
char s[N+10];
int n,bo[N+10][N+10],ans[N+10];
//int dp[N+10][N+10];
int getk(int l,int r){
if (!bo[l][r]) return 0;
if (l==r) return 1;
if (l+1==r) return 2;
int len = (r-l+1)/2;
int L = l,R = r;
return getk(L,L+len-1)+1;
}
int main(){
// memset(dp,255,sizeof dp);
scanf("%s",s+1);
n = strlen(s+1);
for (int i = 1;i <= n;i++){
bo[i][i] = 1;
if (i+1 <= n && s[i]==s[i+1]) bo[i][i+1] = 1;
}
for (int i = 3;i <= n;i++){
for (int j = 1;j <= n;j++){
int r = j + i - 1;
if (r > n) break;
if (bo[j+1][r-1] && s[j]==s[r]) bo[j][r] = 1;
}
}
for (int i = 1;i <= n;i++)
for (int j = i;j <= n;j++){
ans[getk(i,j)]++;
}
for (int i = n-1;i >= 1;i--)
ans[i]+=ans[i+1];
for (int i = 1;i <= n;i++)
printf("%d ",ans[i]);
return 0;
}
【Codeforces Round #427 (Div. 2) D】Palindromic characteristics的更多相关文章
- Codeforces Round #427 (Div. 2) Problem D Palindromic characteristics (Codeforces 835D) - 记忆化搜索
Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th nu ...
- 【Codeforces Round #427 (Div. 2) A】Key races
[Link]:http://codeforces.com/contest/835/problem/A [Description] [Solution] 傻逼题. [NumberOf WA] [Revi ...
- 【Codeforces Round #427 (Div. 2) B】The number on the board
[Link]:http://codeforces.com/contest/835 [Description] 原本有一个数字x,它的各个数码的和原本是>=k的; 现在这个数字x,在不改变位数的情 ...
- 【Codeforces Round #427 (Div. 2) C】Star sky
[Link]:http://codeforces.com/contest/835/problem/C [Description] 给你n个星星的坐标(xi,yi); 第i个星星在第t秒,闪烁值变为(s ...
- 【Codeforces Round #432 (Div. 1) B】Arpa and a list of numbers
[链接]h在这里写链接 [题意] 定义bad list是一个非空的.最大公约数为1的序列.给定一个序列,有两种操作:花费x将一个元素删除.花费y将一个元素加1,问你将这个序列变为good list所需 ...
- 【Codeforces Round #420 (Div. 2) C】Okabe and Boxes
[题目链接]:http://codeforces.com/contest/821/problem/C [题意] 给你2*n个操作; 包括把1..n中的某一个数压入栈顶,以及把栈顶元素弹出; 保证压入和 ...
- 【Codeforces Round #420 (Div. 2) B】Okabe and Banana Trees
[题目链接]:http://codeforces.com/contest/821/problem/B [题意] 当(x,y)这个坐标中,x和y都为整数的时候; 这个坐标上会有x+y根香蕉; 然后给你一 ...
- 【Codeforces Round #420 (Div. 2) A】Okabe and Future Gadget Laboratory
[题目链接]:http://codeforces.com/contest/821/problem/A [题意] 给你一个n*n的数组; 然后问你,是不是每个位置(x,y); 都能找到一个同一行的元素q ...
- 【Codeforces Round #423 (Div. 2) C】String Reconstruction
[Link]:http://codeforces.com/contest/828/problem/C [Description] 让你猜一个字符串原来是什么; 你知道这个字符串的n个子串; 且知道第i ...
随机推荐
- mybatis+springMVC
!!!springMVC Mybatis dbcp log4j 1.导入jar包 2.spring-servlet.xml <?xml version="1.0" enc ...
- 紫书 习题 10-6 UVa 1210(前缀和)
素数筛然后前缀和 看代码 #include<cstdio> #include<vector> #include<cstring> #include<map&g ...
- error C2440: “static_cast”: 无法从“LRESULT (__thiscall CTextProgressCtrl::* )(UINT,LPCTSTR)”转换为“LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)
转自原文 error C2440 “static_cast” 无法从“void (__thiscall C* )(void)... error C2440: “static_cast”: 无法从“LR ...
- [React] Implement a Higher Order Component with Render Props
When making a reusable component, you'll find that people often like to have the API they're most fa ...
- JavaScript语言基础3
JavaScript能够处理一些来自于现实世界的数据类型.比如:数字和文本. 同一时候JavaScript中也包括了一些具 有抽象性质的数据类型.比如对象数据类型. JavaScript它是一种弱类 ...
- atitit. java queue 队列体系and自己定义基于数据库的队列总结o7t
atitit. java queue 队列体系and自己定义基于数据库的队列总结o7t 1. 堵塞队列和非堵塞队列 1 2. java.util.Queue接口. 1 3. ConcurrentLin ...
- Pretty UI Design For Android -- 滑动背景、透明列表
本文是从国外一个网上看到的效果.感觉非常不错.就简化了一下代码.拿来用了,先看下效果图: 效果还是非常不错的,以下让我们看看是如何实现的: 看看文字来源,非常easy,是一个数组: <?xml ...
- thinkphp使后台的字体图标显示异常
thinkphp使后台的字体图标显示异常 相似问题 1.thinkPHP的这些图标都不显示了-CSDN论坛https://bbs.csdn.net/topics/391823415 解答: 发现在别的 ...
- Python(四) 分支、循环、条件与枚举
一.什么是表达式 表达式(Expression)是运算符(operator)和操作数(operand)所构成的序列 二.表达式的优先级 三.表达式优先级练习 优先级同级 从左往右计算 1 or 2 a ...
- datatable设置成中文
$('#datatable').DataTable({ language: { "sProcessing": "处理中...", "sLengthMe ...