D. Palindromic characteristics
3 seconds
256 megabytes
standard input
standard output
Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes.
A string is 1-palindrome if and only if it reads the same backward as forward.
A string is k-palindrome (k > 1) if and only if:
- Its left half equals to its right half.
- Its left and right halfs are non-empty (k - 1)-palindromes.
The left half of string t is its prefix of length ⌊|t| / 2⌋, and right half — the suffix of the same length. ⌊|t| / 2⌋denotes the length of string t divided by 2, rounded down.
Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times.
The first line contains the string s (1 ≤ |s| ≤ 5000) consisting of lowercase English letters.
Print |s| integers — palindromic characteristics of string s.
abba
6 1 0 0
abacaba
12 4 1 0 0 0 0
In the first example 1-palindromes are substring «a», «b», «b», «a», «bb», «abba», the substring «bb» is 2-palindrome. There are no 3- and 4-palindromes here.
回文串
递归解法
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
typedef long long LL;
#define MAXN 5050
#define N 100
/*
1阶回文串就是左右两边读相等
k 回文串就是从中间分开 左右两边都是k-1阶回文串
预处理a[i][j]确定i 到j 是不是回文串
然后递归 找每个固定阶的子串 k阶串肯定是k+1阶串!
*/
int g[MAXN][MAXN], ans[MAXN];
char str[MAXN];
int solve(int l, int r)
{
if (!g[l][r]) return ;//不回文
if (l == r) return ;
if (l + == r) return ;
if ((l - r + ) % == )
return solve(l, (l + r) / ) + ;
else
return solve(l, (l + r) / - ) + ;
}
int main()
{
scanf("%s", str + );
int len = strlen(str+); for (int i = ; i <= len; i++)
{
g[i][i] = ;
if (i + <= len&&str[i] == str[i + ])
g[i][i + ] = ;
} for (int k = ; k <= len; k++)
{
for (int i = ; i <= len; i++)
{
int r = i + k - ;
if (r > len) break;
if (g[i + ][r - ] && str[r] == str[i])
g[i][r] = ;
}
} for (int i = ; i <= len; i++)
{
for (int j = ; j <= len; j++)
ans[solve(i, j)]++;//solve(i,j)b表示从i到j的连续子串是一个几阶回文串
} for (int i = len - ; i >= ; i--)
ans[i] += ans[i + ]; for (int i = ; i <= len; i++)
printf("%d ", ans[i]);
return ;
}
最新文章
- 如何查看经过编码的cookie?
- 模板插件aTpl.js新增功能
- imx6 MFG TOOL 分析
- php执行的困惑
- 高性能MySql进化论(十一):常见查询语句的优化
- RSA算法原理(二)
- POJ3690 Constellations 【KMP】
- hdu Eddy&#39;s picture (最小生成树)
- Android -- 从源码的角度一步步打造自己的TextView
- ReactNative Android之原生UI组件动态addView不显示问题解决
- JQuery之事件处理
- 【最短路+最大流】上学路线@安徽OI2006
- css边框的一些属性
- MySql Undo Redo
- python解决json序列化时间格式
- 初识Quartz (一)
- es6(15)--generator
- Code Chef December Challenge 2018题解
- myeclipse设置jvm参数的三种方式
- bzoj1294 [SCOI2009]围豆豆
热门文章
- CSS实现居中的方式
- [IOI1998]Polygon
- python获取主机名和用户名
- 388 Longest Absolute File Path 最长的绝对文件路径
- jQuery实现文字横向滚动效果
- Android中出现Error:In (declare-styleable) FontFamilyFont, unable to find attribute android:font
- Android 滚动RecyclerView加载图片时的流畅度优化
- git Eclipse项目不显示当前分支
- Mantis 配置与使用学习
- SQL 语句在存储过程执行和在SSMS中执行的差异