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 ...
随机推荐
- C++ GUI Qt4编程(02)-1.2quit
1. 根据C++ GUI Qt4编程(第二版)整理2. 系统:centos7: Qt版本:5.5.13. 程序:quit.cpp #include <QApplication> #inc ...
- cmd笔记
批量获取一个文件夹内所有文件的文件名: dir *.png /b >list.txt tree /f >list.txt for /f "delims=" %%a in ...
- oracle 基础知识(十)----exp/imp--->>>>>expdp/impdp
一,简介 存活下来的远古级别的导入导出软件exp/imp ,软件多数使用于oracle 9i 之前 到了10g以后基本全面被数据库泵(Data Pump)取代,即expdp/impdp.本文会分别介 ...
- kali 安装命令类
apt-get常用命令:update – 取回更新的软件包列表信息upgrade – 进行一次升级install – 安装新的软件包(注:软件包名称是 libc6 而非 libc6.deb)remov ...
- unity摄像机移动滑动
之前写了一个pc版本的 // 当按住鼠标左键的时候 //if (Input.GetMouseButton(0)) //{ // // 获取鼠标的x和y的值,乘以速度和Time.deltaTime是因为 ...
- What is Network Address Translation?---reference
http://whatismyipaddress.com/nat What is Network Address Translation? Network Address Translation (N ...
- python 对列表去重,并保持列表原来顺序
mailto = ['cc', 'bbbb', 'afa', 'sss', 'bbbb', 'cc', 'shafa'] addr_to = list(set(mailto)) addr_to.sor ...
- jquery中Ajax提交配合PHP使用的注意事项-编码
问题:Ajax提交的数据的编码为utf-8,并且返回的数据也要求是utf-8的,如果说你的系统不是utf-8编码的话,那会让你痛不欲生! 解决方法:(比较笨拙的方法,但是很好用) 对于接收的数据,使用 ...
- eclipse-java-style.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?><profil ...
- Path类 操作文件类
// Path类 IO命名空间 静态类 不能创建对象类名. string str =@"E:\C#程序设计基础入门教程\(第十一天)\122\22\nee.txt"; ////in ...