题意:
给你一个由数字构成的字符串,问你有多少种划分方式,使得每段不含前导0,并且每段的数字大小在[l, r]之间。
题解:
很明显的DP。
dp[i]为下标为i时的划分数,设ll为以i+1开始,最小的大于l的位置, rr为以i+1开始,最大的小于r的位置,
那么dp[ll], dp[ll + 1]......dp[rr]都要+d[i],用前缀和或者线段树都行。
关键在与怎么求ll, rr。
其实是大数的大小比较问题。
只要求个lcp,然后判断lcp后面一位的大小即可。
用扩展kmp来O(n)求出当前串每个后缀和另一个串的公共前缀即可。
总时间复杂度O(n)
代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <bitset>
#include <map>
#include <vector>
#include <stack>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <ctime>
#ifdef LOCAL
#define debug(x) cout<<#x<<" = "<<(x)<<endl;
#else
#define debug(x) 1;
#endif
#define chmax(x,y) x=max(x,y)
#define chmin(x,y) x=min(x,y)
#define lson id<<1,l,mid
#define rson id<<1|1,mid+1,r
#define lowbit(x) x&-x
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, int> pii;
const int MOD = 998244353;
const double PI = acos (-1.);
const double eps = 1e-10;
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 2e6 + 5;
int nxt[MAXN], extend1[MAXN], extend2[MAXN];
void GetNext (char *T) {
int a = 0;
int Tlen = strlen (T);
nxt[0] = Tlen;
while (a < Tlen - 1 && T[a] == T[a + 1]) a++;
nxt[1] = a;
a = 1;
for (int k = 2; k < Tlen; k++) {
int p = a + nxt[a] - 1, L = nxt[k - a];
if ( (k - 1) + L >= p) {
int j = (p - k + 1) > 0 ? p - k + 1 : 0;
while (k + j < Tlen && T[k + j] == T[j]) j++;
nxt[k] = j;
a = k;
} else nxt[k] = L;
}
}
void GetExtend (char *S, char *T, int * extend) {
int a = 0;
int Slen = strlen (S);
int Tlen = strlen (T);
GetNext (T);
int MinLen = Slen < Tlen ? Slen : Tlen;
while (a < MinLen && S[a] == T[a]) a++;
extend[0] = a;
a = 0;
for (int k = 1; k < Slen; k++) {
int p = a + extend[a] - 1, L = nxt[k - a];
if ( (k - 1) + L >= p) {
int j = (p - k + 1) > 0 ? p - k + 1 : 0;
while (k + j < Slen && j < Tlen && S[k + j] == T[j]) j++;
extend[k] = j;
a = k;
} else extend[k] = L;
}
}
char a[MAXN], l[MAXN], r[MAXN];
ll d[MAXN];
int main() {
#ifdef LOCAL
freopen ("input.txt", "r", stdin);
#endif
scanf("%s %s %s", a, l, r);
GetExtend(a, l, extend1);
GetExtend(a, r, extend2);
int n = strlen(a), len1 = strlen(l), len2 = strlen(r);
int f = (len1 == 1 && l[0] == '0');
ll now = 1;
d[0] = MOD - 1;
for (int i = 0; i < n; i++) {
int ll, rr;
if (a[i] == '0') {
if (f) ll = rr = i;
else {
now = (now + d[i]) % MOD;
continue;
}
} else {
if (extend1[i] == len1) ll = i + len1 - 1;
else if (a[i + extend1[i]] < l[extend1[i]]) ll = i + len1;
else ll = i + len1 - 1;
if (extend2[i] == len2) rr = i + len2 - 1;
else if (a[i + extend2[i]] > r[extend2[i]]) rr = i + len2 - 2;
else rr = i + len2 - 1;
}
d[ll] = (d[ll] + now) % MOD;
d[rr + 1] = (d[rr + 1] - now + MOD) % MOD;
now = (now + d[i]) % MOD;
}
printf("%lld\n", now);
return 0;
}