【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 ...
随机推荐
- vue-cli生成的模板各个文件详解(转)
vue-cli脚手架中webpack配置基础文件详解 一.前言 原文:https://segmentfault.com/a/1190000014804826 vue-cli是构建vue单页应用的脚手架 ...
- shell判断变量是字符还是数字
ok,以后最好是每天一个shell小脚本吧,这样以后工作时还可以直接套用,嗯,比较不错,顺便还可以带给刚入门shell的朋友一些帮助,好了,废话不多说,下面是我两种判断的实现方式: 1.通过grep去 ...
- 安装虚拟机(VM)(一)
原创作品,允许转载,转载时请务必声明作者信息和本声明. https://www.cnblogs.com/zhu520/p/10728248.html 本人小白,有错指出.谢谢! 一:安装虚拟机前奏 ...
- bzoj 2120 数颜色 题解
转载请注明:http://blog.csdn.net/jiangshibiao/article/details/23990489 [原题] 2120: 数颜色 Time Limit: 6 Sec M ...
- Install Qt 5 on Ubuntu(使用qt-opensource-linux-x64-5.7.0.run进行安装,而且是官方的wiki)
Introduction This is a tutorial for installation of Qt 5.7.0 to Ubuntu 12.10. It may be used also fo ...
- linux 下的select函数
函数原型 /* According to POSIX.1-2001 */ #include <sys/select.h> //头文件 /* According to earlier st ...
- vue --- 脚手架初始化项目中配置文件webpack.base.conf.js代码含义
'use strict' //引入node path 中间件 可以获取到 path 路径的一些信息 const path = require('path') //引入utils工具模块 utils主要 ...
- 113.dynamic_cast 虚函数 通过子类初始化的父类转化为子类类型
#include <iostream> using namespace std; //子类同名函数覆盖父类 //父类指针存储子类地址,在有虚函数情况会调用子类方法,否则会调用父类方法 cl ...
- mybatis集成到spring理解
- js创建dom操作select
document.getElementById("column-left").getElementsByTagName("header")[0].onclick ...