题解:CodeForces 835 D Palindromic characteristics[区间dp/马拉车Manacher]
CodeForces 835D
D. Palindromic characteristics
time limit per test: 3 seconds
memory limit per test: 256 megabytes
*inputstandard input
outputstandard output
Palindromic characteristics of string
- A string is
-palindrome if and only if it reads the same backward as forward. - A string is
-palindrome if and only if:
Its left half equals to its right half.
Its left and right halfs are non-empty
The left half of string
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
Input
The first line contains the string
Output
Print
Examples
input
abba
output
6 1 0 0
input
abacaba
output
12 4 1 0 0 0 0
Note
In the first example
我的题解
一题比较简单的字符串dp
假设字符串长度为l
用一个二维数组记录两个字符之间的字符串是否为回文字符串(或者说组成了多少级别的回文字符串)
再用一个数组计数,该等级的回文字符串有多少个
先把自己组成回文字符串的挑出来(字符串长度为1)
再把和相邻的字符组成回文字符串的挑出来(字符串长度为2)
然后从3到n遍历字符串长度
检测是否存在该长度字符串为回文字符串
(只要看除了左右端点的也就是内部的字符串是否为回文字符串 和 左右两个字符是否相同 就可以了)
如果不是就continue
如果是就找这是多少级别的
只要查询最左边字符与中间字符之间的字符串构成多少级别的字符串再加一就可以了
找到最中间字符要对字符串长度分奇偶判断
That's all!
7.14更新: 预处理可以用马拉车Manacher 但是我还不会
我的代码:
#include <bits/stdc++.h> #define int long long const int N = 1e4/2 + 2; int dp[N][N]; int k[N]; int n; signed main() { std::string s; std::cin >> s; n = s.size(); for(int i = 0 ; i < n ; i ++) { dp[i][i] += 1; k[1] ++; if(i < n-1 && s[i] == s[i+1]) { dp[i][i+1] += 2; k[1] ++; k[2] ++; } } for(int i = 3 ; i <= n ; i ++) { for(int j = 0 ; j+i-1 < n ; j ++) { int l = j , r = l+i-1; if(!dp[l+1][r-1] || s[l] != s[r]) continue; int mid = l + r >> 1; if(i&1) dp[l][r] = dp[j][mid-1] + 1; else dp[l][r] = dp[j][mid] + 1; for(int p = dp[l][r] ; p > 0 ; p --) { k[p]++; } } } for(int i = 1 ; i <= n ; i ++) std::cout << k[i] << " "; return 0; }
PS:
思路来自大佬题解指路
有什么出现纰漏的地方还请大家在评论区指出!谢谢!
posted on 2024-07-14 14:07 Jiejiejiang 阅读(7) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】