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 ...
随机推荐
- linux grep、find 命令详解
grep1.作用grep命令可以指定文件中搜索特定的内容,并将含有这些内容的行标准输出.grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权 ...
- this.Invoke和this.BeginInvoke的区别
private void button1_Click(object sender, EventArgs e) { "; this.Invoke(new EventHandler(delega ...
- 2336: [HNOI2011]任务调度 - BZOJ
一道随机算法的题目 随便用什么随机算法 首先我们可以想到枚举类型3的最终类型,然后再做 先贪心出一个较优的序列,首先我们知道肯定是在A机器上先做完类型1的事件再做类型2的事件,机器B也类似,因为这些没 ...
- Java Swing 快捷键
Java Swing 快捷键 给Java Swing 编程中按钮或者其他组件事件添加快捷键的方法: Component.setAccelerator(KeyStroke.getKeyStroke(‘ ...
- 安卓 DevOps:从一次推送命令到生产
DevOps 是一种广为人知的活动,其主要目的是使软件交付自动化.的确,DevOps 的目标是持续测试.代码质量.功能开发和更轻松地进行维护更新.因此,DevOps 的终极目标之一是让开发者可以执行快 ...
- URAL 1260 Nudnik Photographer(递推)
题目链接 题意 : 给你1到n这n个数,排成一排,然后1放在左边最开始,剩下的数进行排列,要求排列出来的数列必须满足任何两个相邻的数之间的差不能超过2,问你有多少种排列 思路 : 对于dp[n], n ...
- 选择排序的MPI实现
#include "stdafx.h" #include "mpi.h" #include <stdio.h> #include <math. ...
- 解决 IntelliJ 乱码问题
原文:解决 IntelliJ 乱码问题 汉字符在IntelliJ的控制台输出乱码.编译器在编译的时候,把汉字符编译成非UTF-8而引起乱码.我是在做Jsoup解析的时候出现的错误,其实归根结底确实编译 ...
- MAC下编译FFMPEG
1.下载ffmpeg,我使用xcode自带的git下载. 或者直接下载压缩包: 2.下载gas-preprocessor脚本 https://raw.github.com/mansr/gas-prep ...
- Android Framework------之Input子系统
下面这是基于Android4.2代码的关于Input子系统的笔记.在这篇笔记中,只涉及Android相关的东西,关于Linux内核中对各种输入设备的统一,在本文中不作说明.此外,由于才疏学浅,文中难免 ...