codeforces 611D New Year and Ancient Prophecy
f[i = 以i结尾][j = 长度为j] = 方案数。
f[i][j] = sum{ f[i-j][k] , k < j || (k == j && s(i-j+1,j) > s(i-2*j+1,j) ) }
转移为O(N^3)需要优化,
对于k < j,递推g[i][j] = sum(f[i][k], k <= j)。
对于k == j,有O(N^2)个后缀,可以用二维数组lcp[i][j]递推i和j开头的最长公共前缀。
(后缀数组倍增大概也可以做的,用memcpy都可以过的,常数还比较小,极限数据1481ms。
#include<bits/stdc++.h>
using namespace std; typedef long long ll; const int N = 5e3+, mod = 1e9+;
int f[N][N], g[N][N];
char s[N]; int lcp[N][N]; bool bigger(int i, int j, int len)
{
if(lcp[i][j] >= len) return false;
else {
int c = lcp[i][j];
return s[i+c] > s[j+c];
}
} //#define LOCAL
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif
int n, i, j;
scanf("%d%s", &n, s+);
if(s[] == ''){ puts(""); return ; }
for(i = n; i > ; i--){
for(j = n; j > ; j--){
if(s[i] == s[j]) lcp[i][j] = lcp[i+][j+]+;
}
} for(i = ; i <= n; i++){
for(j = ; j < i; j++) {
g[i][j] = g[i][j-];
if(s[i-j+] != ''){
f[i][j] = g[i-j][min(j-,i-j)];
if(f[i-j][j] && bigger(i-j+,i-j-j+,j)){
f[i][j] += f[i-j][j];
if(f[i][j] >= mod) f[i][j] -= mod;
}
g[i][j] += f[i][j];
if(g[i][j] >= mod) g[i][j] -= mod;
}
}
f[i][i] = ;
g[i][i] = g[i][i-] + ;
if(g[i][i] == mod) g[i][i] = ;
}
printf("%d\n",g[n][n]);
return ;
}
codeforces 611D New Year and Ancient Prophecy的更多相关文章
- Codeforces 611D New Year and Ancient Prophecy dp+字符串比较
这是CF Goodbye 2015 的D题,当时我想了一个n^3的dp算法,肯定不能过,然后听到学长后缀数组的n^2log(n)写法,仰慕 最后打完比赛看到了t神的n^2写法,简直膜拜,直接省去了后缀 ...
- 【27.34%】【codeforces 611D】New Year and Ancient Prophecy
time limit per test2.5 seconds memory limit per test512 megabytes inputstandard input outputstandard ...
- Codeforces Good Bye 2015 D. New Year and Ancient Prophecy 后缀数组 树状数组 dp
D. New Year and Ancient Prophecy 题目连接: http://www.codeforces.com/contest/611/problem/C Description L ...
- Good Bye 2015 D. New Year and Ancient Prophecy
D. New Year and Ancient Prophecy time limit per test 2.5 seconds memory limit per test 512 megabytes ...
- Codeforces Beta Round #1 C. Ancient Berland Circus 计算几何
C. Ancient Berland Circus 题目连接: http://www.codeforces.com/contest/1/problem/C Description Nowadays a ...
- Codeforces 611d [DP][字符串]
/* 题意:给一个长度不超过5000的字符串,每个字符都是0到9的数字. 要求将整个字符串划分成严格递增的几个数字,并且不允许前导零. 思路: 1.很开心得发现,当我在前i个区间以后再加一个区间的时候 ...
- [codeforces 260]B. Ancient Prophesy
[codeforces 260]B. Ancient Prophesy 试题描述 A recently found Ancient Prophesy is believed to contain th ...
- CodeForces 164 B. Ancient Berland Hieroglyphs 单调队列
B. Ancient Berland Hieroglyphs 题目连接: http://codeforces.com/problemset/problem/164/B Descriptionww.co ...
- Codeforces 935E Fafa and Ancient Mathematics dp
Fafa and Ancient Mathematics 转换成树上问题dp一下. #include<bits/stdc++.h> #define LL long long #define ...
随机推荐
- cmd笔记
批量获取一个文件夹内所有文件的文件名: dir *.png /b >list.txt tree /f >list.txt for /f "delims=" %%a in ...
- ansible 实战项目之文件操作(二)
一,前言 如果没有安装好的话看我以前的贴子哦!! 上次安装已经确定通了,所以下面步骤应该是完全ok的 特点: (1).轻量级,无需在客户端安装agent,更新时,只需在操作机上进行一次更新即可: (2 ...
- Hadoop Ecosystem related ports
本文总结了Hadoop生态系统中各个组件使用的端口,包括了HDFS,Map Reduce,HBase,Hive,Spark,WebHCat,Impala,Alluxio,Sqoop等,后续会持续更新. ...
- 【计算机网络】详解HttpURLConnection
请求响应流程 设置连接参数的方法 setAllowUserInteraction setDoInput setDoOutput setIfModifiedSince setUseCaches setD ...
- HDU 5288——OO’s Sequence——————【技巧题】
OO’s Sequence Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- mongo 多条件or
or语句 or b=2 > db.XXX.find({"$or":[{"a":1}, {"b":2}]}); 等于java mong ...
- FontAwesome 图标 class="fa fa-home"
本文转自:http://www.yeahzan.com/fa/facss.html 引用方式: class="fa fa-home" 注意:每个图标的引用都必须要添加 fa fa- ...
- 简单springboot及springboot cloud环境搭建
springboot使用特定的方式,简化了spring的各种xml配置文件,并通过maven或者gradle,完成所需依赖,使用springboot maven插件,可直接输出可运行的jar包,省去了 ...
- 3..net可以做什么
.net可以做什么呢? (1)桌面应用程序 Winform(.net开发的桌面应用程序叫winform应用程序) (2)internet应用程序 ASP.net(.net开发的internet应用程 ...
- Git读档
$ git config --global user.name "meng kai" $ git config --global user.email 363255751@qq.c ...