Palindromic characteristics CodeForces - 835D (区间DP,预处理回文串问题)
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.
Input
The first line contains the string s (1 ≤ |s| ≤ 5000) consisting of lowercase English letters.
Output
Print |s| integers — palindromic characteristics of string s.
Examples
abba
6 1 0 0
abacaba
12 4 1 0 0 0 0
Note
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.
题意:
给定一个字符串,定义字符串的等级如下:
如果一个字符串是回文串,那么等级为1
如果他的左半边的字符串和右半边的字符串也都是回文串,那么等级为2.
如果他左半边的左半边和右半边是回文串,右边同,那么等级为3.
以此类推。。。。。
让求这个字符串的所有连续子串的等级情况,
你只需要输出等级为1~n的子串的个数就ok了。
思路:
用区间DP,n*n处理字符串,即dp[i][j] =1 则代表字符串s 中,s[i~j] 是一个回文串。
而数组f[i][j] 代表 s[i~j]的等级。
细节见代码,有详细的注释。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1ll;while(b){if(b&)ans=ans*a%MOD;a=a*a%MOD;b>>=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
char s[maxn];
int cnt[maxn];
const int N = 5e3+;
int dp[N][N];
int f[N][N];
int main()
{
// freopen("C:\\Users\\DH_M\\Desktop\\code_io\\in.txt.txt","r",stdin);
// freopen("C:\\Users\\DH_M\\Desktop\\code_io\\out.txt.txt","w",stdout);
scanf("%s",s+);
int len=strlen(s+);
repd(i,,len)
{
dp[i][i]=dp[i][i-]=;// dp[i][i] 长度为1的字符串肯定是回文串,而dp[i][i-1]=1 是因为在区间DP转移的时候要用到。
f[i][i]=;// 长度为1的串肯定只能是1等级的字符串
}
cnt[]+=len;// len个长度为1 的字符串加到等级为1的中
repd(k,,len)// k 这里是枚举子串的长度
{
// len = 5
// 1 2 3 4 5
// 1 5
// 1 2
repd(i,,len+-k)
{
int j=i+k-;// 区间的左区间
dp[i][j]=dp[i+][j-]&(s[i]==s[j]);// 转移过程用到的”与“运算
// 因为s[i]~s[j] 想要是回文串,那么要在s[i+1]~s[j-1]是回文串的基础上,s[i]==s[j]
// 这里长度为2的时候就要用到dp[i][i-1]=1
f[i][j]=dp[i][j] ? f[i][(i+j-)/]+:;// 等级转移,
// 首先判断是不是回文串,如果不是,等级只能为0
// 如果是回文串,那么这个字符串的等级为他的左半边字符串的等级+1
cnt[f[i][j]]++;// 在答案数组中加一下
}
}
for(int i=len;i>=;i--)
{
cnt[i]+=cnt[i+];// 处理下答案数组,因为i+1等级的字符串一定也满足i等级
}
repd(i,,len)
{
printf("%d ",cnt[i] );// 输出答案
}
// Input
// abba
// Output
// 6 1 0 0
return ;
} inline void getInt(int* p) {char ch;do {ch = getchar();}
while (ch == ' ' || ch == '\n');if (ch == '-') {*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {*p = *p * - ch + '';}}
else {*p = ch - '';while ((ch = getchar()) >= '' && ch <= '')
{*p = *p * + ch - '';}}}
Palindromic characteristics CodeForces - 835D (区间DP,预处理回文串问题)的更多相关文章
- HDU 4632 Palindrome subsequence(区间dp,回文串,字符处理)
题目 参考自博客:http://blog.csdn.net/u011498819/article/details/38356675 题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数. ...
- 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 ...
- HDU 4632 CF 245H 区间DP(回文)
先说HDU 4632这道题,因为比较简单,题意就是给你一个字符串,然后给你一个区间,叫你输出区间内所有的回文子序列,注意是回文子序列,不是回文字串. 用dp[i][j]表示区间[i,j]内的回文子序列 ...
- HDU 4632 Palindrome subsequence(区间DP求回文子序列数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4632 题目大意:给你若干个字符串,回答每个字符串有多少个回文子序列(可以不连续的子串).解题思路: 设 ...
- 牛客练习赛64 如果我让你查回文你还爱我吗 线段树 树状数组 manacher 计数 区间本质不同回文串个数
LINK:如果我让你查回文你还爱我吗 了解到了这个模板题. 果然我不会写2333... 考试的时候想到了一个非常辣鸡的 线段树合并+莫队的做法 过不了不再赘述. 当然也想到了manacher不过不太会 ...
- CF452F等差子序列 & 线段树+hash查询区间是否为回文串
记录一下一个新学的线段树基础trick(真就小学生trick呗) 给你一个1到n的排列,你需要判断该排列内部是否存在一个3个元素的子序列(可以不连续),使得这个子序列是等差序列.\(n\) <= ...
- leetcode 5 Longest Palindromic Substring(Manacher算法求最长回文串)
应用一下manacher算法就可以O(n)求出结果了.可以参考hdu3068 substr(start,length)函数是这样用的: substr 方法 返回一个从指定位置开始,并具有指定长度的子字 ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- UVA 11584 Partitioning by Palindromes 划分回文串 (Manacher算法)
d[i]表示前面i个字符划分成的最小回文串个数, 转移:当第i字符加进来和前面区间j构成回文串,那么d[i] = d[j]+1. 要判断前面的字符j+1到i是不是回文串,可以用Manacher算法预处 ...
随机推荐
- stored information about method csdn
Eclipse编译时保留方法的形参 Window -> Preferences -> Java -> Compiler. 选中Store information about meth ...
- Linux 小知识翻译 - 「NTP」
这周聊聊「NTP」. 上次,聊了「时区」,也就是时间相关的话题. NTP是「Network Time Protocol」的简称,是为了将网络中计算机的时钟同步到正确时间的协议. PC内部的时钟是相当不 ...
- ubuntu中利用qtcreator引用opencv249及采起采集卡的共享库
opencv Link:https://github.com/opencv/opencv CqUsbCam link:https://github.com/niuxiaobaoer/CqUsbCam_ ...
- JavaScript对象原型写法详解
体现对象原型分步式写法 //原型分步式写法 //构造函数 function Person(){} //对象原型 Person.prototype.name = 'Avensatr'; Pers ...
- Django-rest-framework 接口实现 ModelSerializer 使用
ModelSerializer 使用 不需要单独写字段的 序列化工具类 相当于 ModelForm 写法:以及 注意事项: 继承 serializers.ModelSerializer 在类中填写 c ...
- centos7下kubernetes(7.kubernetesScale Up/Down)
伸缩(Scale up/down)是指在线增加或减少pod副本数量 通过yml文件创建两个nginx的pod 先查看一下nginx的yml文件: 通过kubectl apply -f创建 通过kube ...
- BFC原理剖析
本文讲了BFC的概念是什么: BFC的约束规则:咋样才能触发生成新的BFC:BFC在布局中的应用:防止margin重叠(塌陷,以最大的为准): 清除内部浮动:自适应两(多)栏布局. 1. BFC是什么 ...
- Springboot 实现api校验和登录验证
https://blog.csdn.net/qq_36085004/article/details/83348144 文章目录 API校验 场景 实现思路 代码 拦截器: 拦截器注册: 登录token ...
- oracle等待事件-direct path read/write
转://http://blog.chinaunix.net/uid-23177306-id-2531235.html 一.direct path read1.与直接读取相关联的等待事件.当ORACLE ...
- hive 压缩 差不多出来数据 各种压缩 数据格式
待学习 DeprecatedLzoTextInputFormat