【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 ...
随机推荐
- mpstat---用于多CPU环境下,显示各个可用CPU的状态
mpstat命令指令主要用于多CPU环境下,它显示各个可用CPU的状态系你想.这些信息存放在/proc/stat文件中.在多CPUs系统里,其不但能查看所有CPU的平均状况信息,而且能够查看特定CPU ...
- rhcs clustat
http://www.cnblogs.com/jyzhao/p/4775942.html https://access.redhat.com/documentation/zh-CN/Red_Hat_E ...
- 关于post请求“CAUTION: Provisional headers are shown”【转】
在POST请求中偶尔会出现"CAUTION: Provisional headers are shown" 这个警告的意思是说:请求的资源可能会被(扩展/或其它什么机制)屏蔽掉. ...
- ListView阻尼效果
效果图省略.. . activity_main.xml(仅仅有一个自己定义ListView) <RelativeLayout xmlns:android="http://schemas ...
- 安卓自己定义对话框及The specified child already has a child问题
问题:在android开发过程中,有时会在不同情况下遇到同种问题:The specified child already has a parent.You must call removeView() ...
- java设计模式学习 ----- 单例模式(Singleton)
单例模式(Singleton) 单例对象(Singleton)是一种经常使用的设计模式. 在Java应用中,单例对象能保证在一个JVM中,该对象仅仅有一个实例存在.单例模式也分三种:懒汉式单例.饿汉式 ...
- python in操作引发 TypeError
在看 networkx 源代码的时候认为疑惑.为什么外层 for 要注意 TypeError.里面就不用.相同是 in, 一直纠结 node 是不是有问题,比方 node 不能够被迭代什么的,那么里面 ...
- jfinal文件上传与下载
import com.alibaba.fastjson.JSONObject; import com.jfinal.core.Controller; import com.jfinal.ext.kit ...
- sass01
Chrome --流行的浏览器,及前端开发调试工具 WebStorm --强大的跨平台前端集成开发环境 Sublime Text --神器级别的代码编辑器,如vim般强大,而上手难度极低. ----- ...
- vim 脚本之快速打印log
" zsl_log.vim " Version: 1.0 if exists("g:zsl_loaded_log") || &cp || v:versi ...