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

  1. Codeforces 611D New Year and Ancient Prophecy dp+字符串比较

    这是CF Goodbye 2015 的D题,当时我想了一个n^3的dp算法,肯定不能过,然后听到学长后缀数组的n^2log(n)写法,仰慕 最后打完比赛看到了t神的n^2写法,简直膜拜,直接省去了后缀 ...

  2. 【27.34%】【codeforces 611D】New Year and Ancient Prophecy

    time limit per test2.5 seconds memory limit per test512 megabytes inputstandard input outputstandard ...

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

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

  5. Codeforces Beta Round #1 C. Ancient Berland Circus 计算几何

    C. Ancient Berland Circus 题目连接: http://www.codeforces.com/contest/1/problem/C Description Nowadays a ...

  6. Codeforces 611d [DP][字符串]

    /* 题意:给一个长度不超过5000的字符串,每个字符都是0到9的数字. 要求将整个字符串划分成严格递增的几个数字,并且不允许前导零. 思路: 1.很开心得发现,当我在前i个区间以后再加一个区间的时候 ...

  7. [codeforces 260]B. Ancient Prophesy

    [codeforces 260]B. Ancient Prophesy 试题描述 A recently found Ancient Prophesy is believed to contain th ...

  8. CodeForces 164 B. Ancient Berland Hieroglyphs 单调队列

    B. Ancient Berland Hieroglyphs 题目连接: http://codeforces.com/problemset/problem/164/B Descriptionww.co ...

  9. Codeforces 935E Fafa and Ancient Mathematics dp

    Fafa and Ancient Mathematics 转换成树上问题dp一下. #include<bits/stdc++.h> #define LL long long #define ...

随机推荐

  1. python 可迭代对象,迭代器和生成器,lambda表达式

    分页查找 #5.随意写一个20行以上的文件(divmod) # 运行程序,先将内容读到内存中,用列表存储. # l = [] # 提示:一共有多少页 # 接收用户输入页码,每页5条,仅输出当页的内容 ...

  2. PIE SDK灾前灾后对比

    灾前灾后对比功能是GIS软件中常用的功能之一,指利用多时相获取的覆盖同一地表区域的遥感影像及其它辅助数据来确定和分析地表变化.它利用计算机图像处理系统,对不同时段目标或现象状态的变化进行识别.分析:它 ...

  3. 你的UI设计够不够趣味性

    这周要做一个设计,在研究怎么修改的时候,想到了要加入一些符合产品调性的趣味设计,但是要怎么加入才能增加用户对产品的印象,进而增加好感与认可呢,我们今天就来研究一下很多有意思的APP里的一些趣味设计. ...

  4. (转)远程SSH连接服务与基本排错

    远程SSH连接服务与基本排错 原文:https://www.cnblogs.com/chensiqiqi/p/6224474.html#top 1.1 为什么要远程连接Linux系统 在实际的工作场景 ...

  5. vuex中filter的使用 && 快速判断一个数是否在一个数组中

    vue中filter的使用 computed: mapState({ items: state => state.items.filter(function (value, index, arr ...

  6. net.sf.json.JSONException: There is a cycle in the hierarchy! 转json死循环问题解决

    解决上述问题遵照两个原则就可以: 1.页面不需要展示关联数据时 解决:将关联对象属性排除掉 2.页面需要展示关联数据时 解决:将关联对象改为立即加载,并且将关联对象中的属性排除

  7. 简单的CSS3鼠标滑过图片标题和遮罩层动画特效

    此文转自:http://www.cnblogs.com/w2bc/p/5735300.html,仅供本人学习参考,版权归原作者所有!   这是一款使用CSS3制作的简单的鼠标滑过图片标题和遮罩层动画特 ...

  8. PHP中break及continue两个流程控制指令解析

    <?php $arr = array( 'a' => '0a0', 'b' => '0b0', 'c' => '0c0', 'd' => '0d0', 'e' => ...

  9. Red–black tree ---reference wiki

    source address:http://en.wikipedia.org/wiki/Red%E2%80%93black_tree A red–black tree is a type of sel ...

  10. How to Configure Tomcat/JBoss and Apache HTTPD for Load Balancing and Failover

    http://java.dzone.com/articles/how-configure-tomcatjboss-and In this post we will see how to setup a ...