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. UML-1-面向对象分析和设计

    1.关键词: OOA:Object-Oriented Analysis.面向对象分析.抽取对象或概念,如:航班系统包含 飞机(Plane).航班(Flight)等概念. OOD:Object-Orie ...

  2. <?>与<T>的区别

    ?是通配符(占位符),可以表示任意一个,T只是一种替代,只能表示其中一个 假设有A,B,C三个类 <?>可以是A,B,C任意一个,每一个<?>之间没有关联 <T>如 ...

  3. Java中使用nextLine(); 没有输入就自动跳过的问题

    转自:https://www.cnblogs.com/1020182600HENG/p/6564795.html [问题分析] 必要的知识:in.nextLine();不能放在in.nextInt() ...

  4. 封装Lua for C#

    http://blog.csdn.net/rcfalcon/article/details/5583095

  5. Windows未能启动 由于关键系统驱动程序丢失或损坏 电脑无法开机

    该错误导致系统无法开机,其实也好解决 错误描述: Windows未能启动.原因可能是最近更改了硬盘或软件.解决此问题的步骤…… 1.…… 2.…… 3.…… …… 文件:\windows\system ...

  6. geth

    >geth --networkid 123 --dev --datadir "d:/blockchain/project/ethereum" --rpc --rpcaddr ...

  7. 2.storm的安装

    1.前提是linux系统已经安装了上一篇讲的Zookeeper和jdk[1.7及以上版本]还有python[centos已经自带,2.6及以上版本] 2.解压storm压缩包 sudo tar -zx ...

  8. mysql数据库将表移动到新数据库,或者说更改数据库名字

    ①创建新的数据库(你要改的名字) CREATE DATABASE new_name; ②更改数据库表的名字 RENAME TABLE  old_name.table TO new_name.table ...

  9. null 和 undefined 区别

    ---恢复内容开始--- 1.在javascipt中,将一个变量赋值为undefined 或 null ,几乎没什么区别. 2. 在if语句中undefined  和 null 都会被自动转成fals ...

  10. JSP 之 8种HTTP的请求方式 之 页面组成等

    HTTP请求的方法: HTTP/1.1协议中共定义了八种方法(有时也叫“动作”),来表明Request-URL指定的资源不同的操作方式   1.OPTIONS 返回服务器针对特定资源所支持的HTTP请 ...