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 ...
随机推荐
- python数据类型,格式话输出
一.程序交互 name = input(“你的名字是:”) #用户输入,输入的任何东西都存储成str(字符串类型)的形式 二.注释的重要性 以后动辄几千行代码的时候,回过头再去看的时候,发现自己都看不 ...
- JDK安装以及maven部署
JDK安装 检查原有JDK rpm -qa|grep jdk 假如原环境安装有JDK,卸载,命令举例: yum -y remove java--openjdk-headless-.b17.el7.x8 ...
- PIE SDK地图图层渲染方案管理
1. 功能简介 在数据种类较多.渲染规则复杂的情况下,逐个设置其渲染方式是一件繁琐的工作.PIE SDK提供了一种省力省心的办法, PIE SDK提供栅格和矢量数据渲染方案的打开与保存.能够将配色方案 ...
- vue 修饰符(转载)
大佬写的很详细,直接转载过来,随时可以参考, 原博:https://www.w3cplus.com/vue/vue-methods-and-event-handling.html 事件处理 如果需要在 ...
- TCP协议三次握手、四次挥手过程
本文通过图来梳理TCP-IP协议相关知识.TCP通信过程包括三个步骤:建立TCP连接通道,传输数据,断开TCP连接通道.如图1所示,给出了TCP通信过程的示意图. 上图主要包括三部分:建立连接.传输数 ...
- es6 vs commonjs
'use strict' export function showMe() { alert("es6"); }; class logging { constructor() { a ...
- (Frontend Newbie)JavaScript基础之常见数据类型
JavaScript中的数据类型分为两种,一种是简单数据类型,包括Undefined.Null.Boolean.Number和String,另一种是复杂数据类型,即Object,也可称作为引用类型. ...
- 牛客网Java刷题知识点之equals和hashcode()
不多说,直接上干货! 福利 => 每天都推送 欢迎大家,关注微信扫码并加入我的4个微信公众号: 大数据躺过的坑 Java从入门到架构师 人工智能躺过的坑 ...
- 023-将表单序列化为json对象
使用jQuery将表单序列化为json对象,其中serializeJson方法的名字任意,serializeArray()这个jQuery提供的方法.this指的就是谁调用了这个方法. $.fn.se ...
- java中的输入输出方法
输入 import java.util.Scanner; public class EnterTest { public static void main(String[] args) { //主方法 ...