这是CF Goodbye 2015 的D题,当时我想了一个n^3的dp算法,肯定不能过,然后听到学长后缀数组的n^2log(n)写法,仰慕

最后打完比赛看到了t神的n^2写法,简直膜拜,直接省去了后缀数组,而且用一个sum由n^3变成了n^2,唉,经验无与伦比。Orz..

下面的代码就是我照着写的:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<cstdlib>
#include<vector>
#include<queue>
using namespace std;
typedef long long LL;
const int maxn=;
const int mod=1e9+;
int dp[maxn][maxn];
int f[maxn][maxn];
char s[maxn];
bool cmp(int i,int j,int len)
{
if(f[i][j]>=len)return false;
return s[i+f[i][j]]<s[j+f[i][j]];
}
int main()
{
int n;
scanf("%d%s",&n,s+);
for(int i=n; i>; --i)
for(int j=i+; j<=n; ++j)
if(s[i]==s[j])f[i][j]=f[i+][j+]+;
int ans=;
for(int i=; i<=n; ++i)
{
int sum=;
for(int j=i; j<=n; ++j)
{
if(s[i]=='')
dp[i][j]=;
else if(i==)dp[i][j]=;
else
{
int len=j-i+;
bool flag=;
if(i-len>=&&cmp(i-len,i,len))
{
flag=;
sum=(sum+dp[i-len][i-])%mod;
}
dp[i][j]=sum;
if(!flag&&i-len>=) sum=(sum+dp[i-len][i-])%mod;
}
if(j==n)ans=(ans+dp[i][j])%mod;
}
}
printf("%d\n",ans);
return ;
}

Codeforces 611D New Year and Ancient Prophecy dp+字符串比较的更多相关文章

  1. 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) &g ...

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

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

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

  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 219D. Choosing Capital for Treeland (树dp)

    题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...

  6. [CodeForces - 1272D] Remove One Element 【线性dp】

    [CodeForces - 1272D] Remove One Element [线性dp] 标签:题解 codeforces题解 dp 线性dp 题目描述 Time limit 2000 ms Me ...

  7. Codeforces 935E Fafa and Ancient Mathematics dp

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

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

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

  9. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

随机推荐

  1. Android中9patch图片格式(xx.9.png)介绍与制作详解

    一:9patch图片介绍: android的.9.png是android系统中一种特殊的图片格式,专门用来用来处理图片大小变化后(如拉伸)的失真,不正常,如我们看到的qq聊天中的文字气泡,不管你输入的 ...

  2. python 开发webService

    最近在学习用python 开发webservice,费了半天时间把环境搭好,记录下具体过程,以备后用. 首先系统上要有python.其次要用python进行webservice开发,还需要一些库: 1 ...

  3. [转载]ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase玩转URL

    引言-- 在初级篇中,我们介绍了如何利用基于ASP.NET MVC的Web程序中的Global文件来简单的重写路由.也介绍了它本身的局限性-依赖于路由信息中的键值对: 如果键值对中没有的值,我们无法将 ...

  4. Deep Learning and the Triumph of Empiricism

    Deep Learning and the Triumph of Empiricism By Zachary Chase Lipton, July 2015 Deep learning is now ...

  5. 团体程序设计天梯赛-练习集L1-019. 谁先倒

    L1-019. 谁先倒 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 划拳是古老中国酒文化的一个有趣的组成部分.酒桌上两人划拳 ...

  6. The 9th Zhejiang Provincial Collegiate Programming Contest->Problem D:D - Draw Something Cheat

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3603 题意:在给出的字符串中找出每行都出现的字母按字典序排序. #incl ...

  7. win8系统中PL/SQL Developer连接Oracle出现的问题

    注意:所有软件最后不要安装在program files (x86)下  PL/SQL Developer显示Not logged on 以管理员的身份打开PL/SQL Developer 2.   t ...

  8. 李洪强漫谈iOS开发[C语言-014]-变量

    01 变量的概念 02 - 变量的语法 03 变量的使用

  9. Nginx、LVS及HAProxy负载均衡软件的优缺点详解

    http://www.csdn.net/article/2014-07-24/2820837

  10. HeadFirst设计模式之组合模式

    一. 1.The Composite Pattern allows us to build structures of objects in the form of trees that contai ...