这是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. python学习笔记8(表达式和语句)

    一.print 和 import 信息 >>> print 'age:',22 # 用逗号隔开打印多个表达式 age: 22 import somemodule # 从模块导入函数 ...

  2. iOS中的固定 高度

    iOS键盘高度   英文 216(不带联想功能) 英文 252(带联想功能)  中文 252 系统自带表情键盘的高度是 253   在使用系统键盘的时候,如果遇到输入框被键盘挡住的情况 就要用通知中心 ...

  3. C#设计模式学习资料--观察者模式

    http://www.cnblogs.com/promise-7/archive/2012/05/14/2500759.html http://www.cnblogs.com/zhenyulu/art ...

  4. java第四课:数组

    1.数组声明时,必须有中括号,但不指定数组的元素个数2.初始化时,必须指定元素个数3.数组元素内容仅能用于声明时初始化,不能用于赋值.如:char[] week; week={'1','2','3'} ...

  5. ITQ迭代量化方法解析

    一.问题来源 来源于换关键字,从LSH转换为hash检索,这要感谢李某. 二.解析 笔者认为关键思想是数据降维后使用矩阵旋转优化,其他和LSH一样的. 2.1 PCA降维 先对原始空间的数据集 X∈R ...

  6. 返回canceled 代码 的原因

    ajax 不支持跨域操作jsonp,才导致返回canceled 代码. 解决的办法就是 <script>标签.jquery 也为我们提供了$.Ajax()方法或$.getScript()方 ...

  7. ADO.net--杂七杂八(一)

    private void BtnConnectDataBase_Click(object sender, RoutedEventArgs e) { string connectionString = ...

  8. spoj 178

    输出相邻的点   比较简单吧....... #include <cstdio> #include <cstring> using namespace std; int main ...

  9. oracle-number(5,2)

    insert into emp values(70000.123); 只能存储 整数的前3位, 小数点后面的2位

  10. POJ3258River Hopscotch(二分)

    http://poj.org/problem?id=3258 题意:有一条很长很直的河距离为L,里边有n块石头,不包括起点和终点的那两块石头,奶牛们会从一个石头跳到另外一个,但因为有的石头隔得太近了, ...