数字串 枚举 优化 暴力 思维
数字串
https://ac.nowcoder.com/acm/contest/11231/B
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 5e5 + 7; inline bool cmp(char a[], char b[]) { for (int i = 1; a[i] && b[i]; ++i) if (a[i] != b[i]) return a[i] > b[i]; return 1; // equal } char L[50], R[50], s[N]; int main() { scanf("%s%s%s", L + 1, R + 1, s + 1); int lenL = strlen(L + 1), lenR = strlen(R + 1), lenS = strlen(s + 1); ll ans = 0, preZero = 0; int left = 0, right = 0, now; for (int i = 1, sz = lenS - lenL + 1; i <= sz; ++i) { if (s[i] == '0') ++preZero; else { // 枚举以s[i]开头的合法区间,其实只会在目标位置附近浮动一位,那么检测一下即可 left = i + lenL - 1, right = i + lenR - 1; if (!cmp(s + i - 1, L)) ++left; if (right > lenS) right = lenS; else if (!cmp(R, s + i - 1)) --right; now = right - left + 1; ans += now; ans += preZero * now; preZero = 0; } } printf("%lld", ans); return 0; }
算法竞赛之路 文章被收录于专栏
整理、记录算法竞赛的好题