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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 2010 NEERC Western subregional

    2010 NEERC Western subregional Problem A. Area and Circumference 题目描述:给定平面上的\(n\)个矩形,求出面积与周长比的最大值. s ...

  9. 【GYM101409】2010-2011 ACM-ICPC, NEERC, Western Subregional Contest

    A-Area and Circumference 题目大意:在平面上给出$N$个三角形,问周长和面积比的最大值. #include <iostream> #include <algo ...

随机推荐

  1. SQL语句(六)成批导入导出数据

    (六) 成批导入导出数据 假设已经存在teaching数据库, 存在一张Student表,如图: 右键teaching->任务->导入数据 下一步->数据源(Microsoft Ex ...

  2. 操作系统环境变量LANG和NLS_LANG的关系

    =Native Language Support本地语言支持 NLS ORACLE11g-ORA-12705: Cannot access NLS data files or invalid envi ...

  3. Socket心跳包机制【转】

    转自:https://blog.csdn.net/xuyuefei1988/article/details/8279812 心跳包的发送,通常有两种技术 方法1:应用层自己实现的心跳包 由应用程序自己 ...

  4. 使用/dev/uinput的简要介绍(含demo程序)【转】

    转自:https://blog.csdn.net/zhongkunjia/article/details/75142699 uinput机制有2个很大的优点: 1) 不用自己写驱动(比如弄个红外遥控器 ...

  5. Ansible 插件 之 【CMDB】【转】

    Github地址: https://github.com/fboender/ansible-cmdb 从facts收集信息,生成主机概述 安装 wget https://github.com/fboe ...

  6. IE WebBrowser内核设置

    public class IEVersion { /// <summary> /// IE WebBrowser内核设置 /// </summary> public stati ...

  7. linux(CentOS) 下mysql自动备份

    1.创建并编辑文件 /usr/sbin/bakmysql.sh,命令: vi /usr/sbin/bakmysql.sh 内容如下: db_user="root" db_passw ...

  8. c++中的前置声明

    引用google c++编码规范: When you include a header file you introduce a dependency that will cause your cod ...

  9. MVC母板页传递数据

    问题:判断Session 失败 结果: if (Session[SessionKey.AccountInfo] != null) { ViewData["AdminUser"] = ...

  10. *使用配置类定义Codeigniter全局变量

    之前提到的 CodeIgniter 引入自定义公共函数 这篇文章提到了公共函数实现,全局的变量也可以借助 helper 函数来实现.不过,更为合适的方式可能要属用配置类定义了. CodeIgniter ...