Codeforces 611D New Year and Ancient Prophecy dp+字符串比较
这是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+字符串比较的更多相关文章
- 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 ...
- 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 ...
- 【27.34%】【codeforces 611D】New Year and Ancient Prophecy
time limit per test2.5 seconds memory limit per test512 megabytes inputstandard input outputstandard ...
- 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 219D. Choosing Capital for Treeland (树dp)
题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...
- [CodeForces - 1272D] Remove One Element 【线性dp】
[CodeForces - 1272D] Remove One Element [线性dp] 标签:题解 codeforces题解 dp 线性dp 题目描述 Time limit 2000 ms Me ...
- Codeforces 935E Fafa and Ancient Mathematics dp
Fafa and Ancient Mathematics 转换成树上问题dp一下. #include<bits/stdc++.h> #define LL long long #define ...
- Codeforces 611d [DP][字符串]
/* 题意:给一个长度不超过5000的字符串,每个字符都是0到9的数字. 要求将整个字符串划分成严格递增的几个数字,并且不允许前导零. 思路: 1.很开心得发现,当我在前i个区间以后再加一个区间的时候 ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
随机推荐
- python学习笔记29(python中堆的使用)
堆(heap):优先队列的一种,使用优先队列能够以任意顺序增加对象,并且能在任意时间(可能在增加对象的同时)找到(也可能是移除)最小元素,比用于列表中min的方法要高效. Python中并没有独立的堆 ...
- C#学习笔记---基础入门(二)
枚举 枚举是被命名的整型常数的集合:枚举类型的变量只有赋值后才能使用:不同枚举中的枚举值可以重名:可以自定义枚举值. enum Playstates { 跑, 跳,下滑,左转,右 ...
- apache与nginx日志文件的区别(转载)
apache与nginx日志文件的区别 转载:http://www.xfcodes.com/apache/log/3270.htm 导读:apache与nginx日志文件的区别,在apache与ngi ...
- 5.0:Spring-bean的加载
内容来自<Spring深度解析>,之后的不一一复述! 在Spring中,最基本的IOC容器接口是BeanFactory - 这个接口为具体的IOC容器的实现作了最基本的功能规定 - 不管怎 ...
- Linux rm命令
rm可以用来删除文件和文件夹. rm --help Usage: rm [OPTION]... FILE... Remove (unlink) the FILE(s). -f, --force ...
- tomcat 设置默认编码格式
在tomcat目录下 conf文件夹下的server.xml中: <Connector port="80" protocol="HTTP/1.1" ...
- Qt串口通信接收数据不完整的解决方法(传输图片)
在使用串口接收数据时,当数据量大的时候会出现数据接收不完整的情况.因为串口数据获取函数readAll()由readyRead()信号触发,但readyRead()信号在串口读到起始标志时立即发送,并不 ...
- 提供几个可注册的edu邮箱链接
旧版的邮箱大全有edu邮箱的专题页面,放出来2个国内edu.cn邮箱的注册地址:@live.shop.edu.cn和@abc.shop.edu.cn,现在已经停止开放注册了. 其实旧版中还做了个隐藏的 ...
- C++中的指针与const
刚开始接触C++时,指针和const之间的关系有点混乱,现在总结如下: 一.指向const变量的指针 #include<iostream.h> void main() { const in ...
- Sqlmap基础(二)
sqlmap.py -r req1.txt --dbms Oracle --risk