• CodeForces1051E EXKMP + 线段树dp


    http://codeforces.com/problemset/problem/1051/E

    题意:给你一个很大的数字,然后你可以把这个数字拆分成为任意多个部分,要求每一个部分的数字大小要在一个区间内,问有多少种拆分方式。

    很容易看出这是一个dp,用dp[i]表示到i之前位置总共的数量,再用l[i]和r[i]表示i位置到l和r区间内的字符串全都满足上下限的条件,将dp[i - 1]加到l到r上更新即可,是一个很显然的n²dp,当然n²是不可能的,随随便便搞个数据结构进行一下区间修改就行了,这里用的是线段树。

    问题就给到了预处理l[i]和r[i]这个问题上,暴力预处理又是一个n²的操作,考虑到大数比较事实上是去掉最大公共前缀之后比较下一位即可,可以容易的想到用EXKMP去处理下就好了。

    (线段树一开始就开了0 ~ N,RE了一个小时)

    #include <map>
    #include <set>
    #include <ctime>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <string>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <sstream>
    #include <iostream>
    #include <algorithm>
    #include <functional>
    using namespace std;
    inline int read(){int now=0;register char c=getchar();for(;!isdigit(c);c=getchar());
    for(;isdigit(c);now=now*10+c-'0',c=getchar());return now;}
    #define For(i, x, y) for(int i=x;i<=y;i++)  
    #define _For(i, x, y) for(int i=x;i>=y;i--)
    #define Mem(f, x) memset(f,x,sizeof(f))  
    #define Sca(x) scanf("%d", &x)
    #define Sca2(x,y) scanf("%d%d",&x,&y)
    #define Sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
    #define Scl(x) scanf("%lld",&x);  
    #define Pri(x) printf("%d
    ", x)
    #define Prl(x) printf("%lld
    ",x);  
    #define CLR(u) for(int i=0;i<=N;i++)u[i].clear();
    #define LL long long
    #define ULL unsigned long long  
    #define mp make_pair
    #define PII pair<int,int>
    #define PIL pair<int,long long>
    #define PLL pair<long long,long long>
    #define pb push_back
    #define fi first
    #define se second 
    typedef vector<int> VI;
    const double eps = 1e-9;
    const int maxn = 1e6 + 10;
    const int INF = 0x3f3f3f3f;
    const LL mod = 998244353 ; 
    char a[maxn],down[maxn],up[maxn];
    int l[maxn],r[maxn];
    int nxt[maxn],edown[maxn],eup[maxn];
    void pre_EKMP(char x[],int m,int next[]){
        next[0] = m;
        int j = 0;
        while(j + 1 < m && x[j] == x[j + 1]) j++;
        next[1] = j;
        int k = 1;
        for(int i = 2; i < m ; i ++){
            int p = next[k] + k - 1;
            int L = next[i - k];
            if(i + L < p + 1) next[i] = L;
            else{
                j = max(0,p - i + 1);
                while(i + j < m && x[i + j] == x[j]) j ++;
                next[i] = j;
                k = i;
            }
        }
    }
    void EKMP(char x[],int m,char y[],int n,int next[],int extend[]){ 
        pre_EKMP(x,m,next);
        int j = 0;
        while(j < n && j < m && x[j] == y[j]) j ++;
        extend[0] = j;
        int k = 0;
        for(int i = 1; i < n ; i ++){
            int p = extend[k] + k - 1;
            int L = next[i - k];
            if(i + L < p + 1) extend[i] = L;
            else{
                j = max(0,p - i + 1);
                while(i + j < n && j < m && y[i + j] == x[j]) j ++;
                extend[i] = j;
                k = i;
            }
        }
    }
    struct Tree{
        int l,r;
        LL lazy;
    }tree[maxn << 3];
    void Build(int t,int l,int r){
        if(l > r) exit(0);
        tree[t].l = l; tree[t].r = r;
        tree[t].lazy = 0;
        if(l == r) return;
        int m = (l + r) >> 1;
        Build(t << 1,l,m); Build(t << 1 | 1,m + 1,r);
    }
    void Pushdown(int t){
        if(tree[t].lazy){
            tree[t << 1].lazy = (tree[t << 1].lazy + tree[t].lazy) % mod;
            tree[t << 1 | 1].lazy = (tree[t << 1 | 1].lazy + tree[t].lazy) % mod;
            tree[t].lazy = 0;
        }
    }
    void update(int t,int l,int r,LL v){
        if(l <= tree[t].l && tree[t].r <= r){
            tree[t].lazy = (v + tree[t].lazy) % mod;
            return;
        }
        Pushdown(t);
        int m = (tree[t].l + tree[t].r) >> 1;
        if(r <= m) update(t << 1,l,r,v);
        else if(l > m) update(t << 1 | 1,l,r,v);
        else{
            update(t << 1,l,m,v); 
            update(t << 1 | 1,m + 1,r,v);
        } 
    }
    LL query(int t,int p){
        if(tree[t].l >= tree[t].r) return tree[t].lazy % mod;
        Pushdown(t);
        int m = (tree[t].l + tree[t].r) >> 1;
        if(p <= m) return query(t << 1,p);
        else return query(t << 1 | 1,p);
    }
    int main()
    {
        scanf("%s%s%s",a,down,up);
        int N = strlen(a);
        int l1 = strlen(down),l2 = strlen(up);
        EKMP(down,l1,a,N,nxt,edown);
        EKMP(up,l2,a,N,nxt,eup);
        for(int i = 1; i <= N ; i ++){
            l[i] = i + l1 - 1,r[i] = i + l2 - 1;
            if(a[i - 1] == '0'){
                if(down[0] == '0'){
                    l[i] = r[i] = i;
                }else{
                    l[i] = 1,r[i] = 0;
                }
                continue;
            }
            int len = edown[i - 1];
            if((len < l1) &&  down[len] > a[i + len - 1]) l[i]++;
            len = eup[i - 1];
            if((len < l2) && up[len] < a[i + len - 1]) r[i]--;
        }
        Build(1,0,N * 2); update(1,0,0,1);
        for(int i = 1; i <= N ; i ++){
            if(l[i] > r[i]) continue;
            LL x = query(1,i - 1);
            update(1,l[i],r[i],x);
        }
        Prl(query(1,N));
        return 0;
    }
  • 相关阅读:
    题解:CF1494 F. Delete The Edges
    「CEOI2011」选做
    题解:「CEOI2017」Chase
    学习笔记:《具体数学》问题整理
    题解:「COCI2019」 Transport
    【做题记录】位运算
    线性DP
    虚树 virtual-tree
    三元环计数
    长链剖分
  • 原文地址:https://www.cnblogs.com/Hugh-Locke/p/9974221.html
Copyright © 2020-2023  润新知