Codeforces Good Bye 2015 D. New Year and Ancient Prophecy DP

题意:把给出的序列分成严格递增的子序列。
思路: dp(ij)=j1k=1dp(ij,k)+dp(ij,j) ,其中 dp(i,j) 代表第 i 个字符结尾,最后一个块的大小为j dp(i,j) 只能由 dp(ij,k) 得到。其中 j1k=1dp(ij,k) 是不用判断的,而 dp(ij,j) 是需要判断,因为题目要求的是严格递增的,所以要判断 dp(i,j) 能否由 dp(ij,j) 得到。这个判断过程我在打CF的时候并不知道,看了别人的代码才学到的, cnt(i,j) 代表以 ai aj 开头的字符串有多少个字符时相同的。 if(ai==aj)cnt(i,j)=cnt(i1,j1)+1 。然后判断 dp(i,j) 能否由 dp(ij,j) 得到的时候就可以直接判断不相同的那一位而不用判断整个字符串。
复杂度 O(N2)

http://codeforces.com/contest/611/problem/D

/*********************************************
    Problem : Codeforces Good Bye 2015
    Author  : NMfloat
    InkTime (c) NM . All Rights Reserved .
********************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

#define rep(i,a,b)  for(int i = (a) ; i <= (b) ; i ++)
#define rrep(i,a,b) for(int i = (b) ; i >= (a) ; i --)
#define repE(p,u) for(Edge * p = G[u].first ; p ; p = p -> next)
#define cls(a,x)   memset(a,x,sizeof(a))
#define eps 1e-8

using namespace std;

const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5+5;
const int MAXE = 2e5+5;

typedef long long LL;
typedef unsigned long long ULL;

int T,n,m,k;

int fx[] = {0,1,-1,0,0};
int fy[] = {0,0,0,-1,1};

char a[5005];
int cnt[5005][5005];
int dp[5005][5005];
int sum[5005][5005];

void input() {

}

void solve() {
    cls(cnt,0);
    rrep(i,1,n) {
        rrep(j,i+1,n) {
            if(a[i] == a[j]) {
                cnt[i][j] = cnt[i+1][j+1] + 1; //代表以i和j开头的两个字符串后面有cnt[i][j]个字符是相等的
            }
        }
    }
    //dp[i][j]代表第i为结尾有j个连续字符的有多少个
    cls(dp,0); cls(sum,0);
    dp[0][0] = 1;
    rep(i,0,n) sum[0][i] = 1;
    rep(i,1,n) {
        rep(j,1,i) {//长度
            if(a[i-j+1] == '0') continue;
            dp[i][j] = (dp[i][j] + sum[i-j][j-1]) % MOD;
            int l = i - 2 * j + 1;
            int r = i - j + 1;
            if(l < 1) continue;
            if(cnt[l][r] < j && a[l+cnt[l][r]] < a[r+cnt[l][r]]) {
                dp[i][j] = (dp[i][j] + dp[i-j][j])%MOD;
            }
        }
        rep(j,1,n) {
            sum[i][j] = (dp[i][j] + sum[i][j-1]) % MOD;
         //   printf("%d ",dp[i][j]);
        }
        //puts("");
    }
    int ans = 0;
    rep(i,1,n) {
        ans = (ans + dp[n][i]) % MOD;
    }
    printf("%d\n",ans);
}

int main(void) {
    //freopen("a.in","r",stdin);
    while(~scanf("%d %s",&n,&a[1])) {
        input();
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值