Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem G. k-palindrome dp
Problem G. k-palindrome
题目连接:
http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c7022&all_runs=1&action=140
Description
We will say that a string T is a k-palindrome for some positive integer k if and only if k is not grater than
the length of T and its prefix of the length k is equal to the reversed suffix of that length. For example,
abacaba is a k-palindrome for any k from 1 to 7 while abacabada is only 1-palindrome.
You are given a string S. Try to find the number of k-palindrome substrings of S for all k from 1 to the
length of S.
Input
The only line of input contains the string S (1 ≤ |S| ≤ 5000). The string contains only lowercase letters
of the Latin alphabet.
Output
Output |S| integers, the kth of them should be equal to the number of k-palindrome substrings of S.
Sample Input
abacaba
Sample Output
14 5 5 2 2 1 1
Hint
题意
k回文串就是表示这个串的前k个字符和后k个字符是回文的。
然后给你一个人串,问你他的k回文子串有多少个,k从1到n
题解:
首先k回文串的话,那么他一定是k-1回文串,所以只要知道最长的就好了。
枚举起点枚举终点,hash二分,这个复杂度n^2logn
但是可以n^2,就用dp去预处理起点和终点的最长前后缀,这个傻逼dp就好了。
代码
#include <bits/stdc++.h>
#define rep(a,b,c) for(int (a)=(b);(a)<=(c);++(a))
#define drep(a,b,c) for(int (a)=(b);(a)>=(c);--(a))
#define pb push_back
#define mp make_pair
#define sf scanf
#define pf printf
#define two(x) (1<<(x))
#define clr(x,y) memset((x),(y),sizeof((x)))
#define dbg(x) cout << #x << "=" << x << endl;
const int mod = 772002;
int mul(int x,int y){return 1LL*x*y%mod;}
int qpow(int x , int y){int res=1;while(y){if(y&1) res=mul(res,x) ; y>>=1 ; x=mul(x,x);} return res;}
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
using namespace std;
const int maxn = 5000 + 50;
int f[maxn][maxn],len,ans[maxn];
char str[maxn];
int DFS(int l , int r){
if(~f[l][r]) return f[l][r];
if(l>len||r<=0) return f[l][r]=0;
if(str[l]==str[r]) f[l][r] = DFS(l+1,r-1)+1;
else f[l][r] = 0;
return f[l][r];
}
int main( int argc , char * argv[] ){
//freopen("in.txt","r",stdin);
sf("%s",str+1);
len=strlen(str+1);
memset(f,-1,sizeof(f));
for(int i = 1 ; i <= len ; ++ i) for(int j = i ; j <= len ; ++ j) ans[min(j-i+1,DFS(i,j))]++;
for(int i = len ; i >= 1 ; -- i) ans[i - 1] += ans[i];
for(int i = 1 ; i <= len ; ++ i){
if( i > 1 ) pf(" ");
pf("%d",ans[i]);
}
pf("\n");
return 0;
}
Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem G. k-palindrome dp的更多相关文章
- Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem K. UTF-8 Decoder 模拟题
Problem K. UTF-8 Decoder 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c702 ...
- Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem I. Alien Rectangles 数学
Problem I. Alien Rectangles 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c ...
- Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem H. Parallel Worlds 计算几何
Problem H. Parallel Worlds 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c7 ...
- Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem F. Turning Grille 暴力
Problem F. Turning Grille 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c70 ...
- Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem C. Cargo Transportation 暴力
Problem C. Cargo Transportation 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed ...
- Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem A. A + B
Problem A. A + B 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c7022&al ...
- 2009-2010 ACM-ICPC, NEERC, Western Subregional Contest
2009-2010 ACM-ICPC, NEERC, Western Subregional Contest 排名 A B C D E F G H I J K L X 1 0 1 1 1 0 1 X ...
- 2010 NEERC Western subregional
2010 NEERC Western subregional Problem A. Area and Circumference 题目描述:给定平面上的\(n\)个矩形,求出面积与周长比的最大值. s ...
- 【GYM101409】2010-2011 ACM-ICPC, NEERC, Western Subregional Contest
A-Area and Circumference 题目大意:在平面上给出$N$个三角形,问周长和面积比的最大值. #include <iostream> #include <algo ...
随机推荐
- bzoj千题计划174:bzoj1800: [Ahoi2009]fly 飞行棋
http://www.lydsy.com/JudgeOnline/problem.php?id=1800 圆上两条直径构成矩形的对角线 #include<cstdio> using nam ...
- opencv附加依赖性选择,提示找不到opencv_world400d.dll
连接器>>输入>>附加依赖项,添加opencv_world400d.lib库文件名,在....\opencv\build\x64\vc14\lib有2个lib文件, 带d的是d ...
- [转]GCC系列: __attribute__((visibility("")))
在 objc-api.h 里面有很多关于__attribute__ 的定义. 例如 #if !defined(OBJC_VISIBLE) # if TARGET_OS_WIN32 # if defin ...
- linux的内存文件系统tmpfs
在centos系统上自带的内存文件系统.这个tmpfs是temporary file system的意思. 一. 使用命令 df -h 查看tmpfs是否正在运行. Filesystem Size U ...
- Android Studio 新建drawable-hdpi、drawable-mdpi等
在不同的模式“Project” / “Android”的文件夹中查看文件夹.如果文件夹丢失,您可以轻松添加它们. 1.在“res”文件夹上右键“New”->”Android Resource D ...
- Java基本数据类型装箱的127临界点
package wrapper.demo; public class WrapperDemo { /** * @param args */ public static void main(String ...
- 云计算--hdfs dfs 命令
在hadoop安装目录下:/hadoop2/hadoop-2.7.3 1.创建目录 bin/hdfs dfs -mkdir /user bin/hdfs dfs -mkdir /user/<us ...
- PowerDesigner显示Common注释列并自动赋值
PowerDesigner中默认不显示Common注释列,可根据以下步骤显示并紫东填充Name列内容. 1.显示Common注释列 2.运行VB Script脚本自动赋值 使用Shift+Ctrl+X ...
- C语言字节对齐 __align(),__attribute((aligned (n))),#pragma pack(n)【转】
转自:https://www.cnblogs.com/ransn/p/5081198.html 转载地址 : http://blog.csdn.net/21aspnet/article/details ...
- 数组用console.log输出
输出的时候,如果前面有字符串,那么输出的就是整个字符串