Codeforces Round #427 (Div. 2) D-Palindromic characteristics (回文串,暴力)
来源:互联网 发布:中国顾问在叙利亚知乎 编辑:程序博客网 时间:2024/05/22 03:53
D. Palindromic characteristics
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
inputabbaoutput6 1 0 0 inputabacabaoutput12 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.
题目大意
找回文串,一个k阶回文串的定义是他的左边一半是一个k-1阶回文串。输出1-|s|阶回文串的个数。
注意题目要求,一个k阶回文串也会在k-1阶里面计数,但这问题不大。
首先用通用技巧,在每两个字符之间增加一个字符,来避免讨论奇偶。
然后为了方便的判断阶数,我们可以暴力的开一个数组,来记录以L为起点以R为终点的字符串的阶数,非回文串为0,于是我们只需要枚举中间数,向两边拓展,然后阶数就等于一边的阶数加一即可。注意不要把前面增加的字符算进来。具体可以参见代码。
代码:
#include<stdio.h>#include<iostream>#include<algorithm>#include<cstring>#define ll long longusing namespace std;ll cnt[12345],i,j,k,l,p,x,y,tmp,n;string s;char A[12345];short Q[10005][10005];int main(){ cin>>s;n=s.length();s=" "+s; for(i=1;i<=n;i++) { A[2*i-1]=s[i]; A[2*i]='#'; } cnt[1]+=n; n=2*n-1; for(i=1;i<=n;i++) if(A[i]!='#')Q[i][i]=1; for(k=1;k<=n;k++) { for(l=1;k-l>=1&&k+l<=n;l++) { if(A[k-l]=='#')continue; i=k-l;j=k+l; if(A[i]!=A[j])break; if(A[k-1]=='#')x=k-2; else x=k-1; tmp=Q[i][x]+1; cnt[tmp]++; Q[i][j]=tmp; } } for(i=s.length()-1;i>0;i--)cnt[i]+=cnt[i+1]; for(i=1;i<s.length();i++)printf("%I64d ",cnt[i]);}
阅读全文
0 0
- Codeforces Round #427 (Div. 2) D-Palindromic characteristics (回文串,暴力)
- Codeforces Round #427 (Div. 2)D. Palindromic characteristics(DP+回文串)
- 【Codeforces】Codeforces Round #427 (Div. 2) D. Palindromic characteristics DP回文串
- Codeforces Round #427 (Div. 2)C. Star sky 暴力D. Palindromic characteristics
- Codeforces Round #427 (Div. 2) D. Palindromic characteristics dp
- Codeforces Round #427 (Div. 2) D.Palindromic characteristics
- Codeforces 835D Round #427 D-Palindromic characteristics—— DP+回文串
- Codeforces Round #427 (Div. 2) D.Palindromic characteristics【DP、后缀和】
- 【回文串】835D Palindromic characteristics
- Codeforces Round #286 (Div. 2) A.(回文串暴力)
- Codeforces Round #279 (Div. 2) D 暴力
- Codeforces 835D-Palindromic characteristics
- Codeforces Round #322 (Div. 2) D. Three Logos(暴力)
- Codeforces 427Div2 D、835D Palindromic characteristics
- Codeforces 835 D Palindromic characteristics(区间DP)
- Codeforces #835D: Palindromic characteristics 题解
- Codeforces 835 D Palindromic characteristics 区间dp
- Codeforces 835D Palindromic characteristics【区间Dp】
- 【01】web前端知识体系
- Java基础之面向对象基础
- Dubbo之Zookeeper作用解析
- 1052 最大M子段和
- 安卓6.0以上ScrollView嵌套RecyclerView条目无法全部显示
- Codeforces Round #427 (Div. 2) D-Palindromic characteristics (回文串,暴力)
- JZOJ5235. 【NOIP2017模拟8.7A组】好的排列
- Unity实现UI渐变效果(UGUI)
- EasyUI查询数据为空时的提示
- CF152C: Pocket Book(思维)
- 智能指针auto_ptr、scoped_ptr、shared_ptr和weak_ptr
- ACM题集以及各种总结大全
- HDU 4764 Stone 巴什博弈
- opencv1.0使用cvLoadImage读取一个文件夹下连续编号的图片