题解 CF585F 【Digits of Number Pi】
考虑用数位 \(DP\) 来统计数字串个数,用 \(SAM\) 来实现子串的匹配。
设状态 \(f(pos,cur,lenth,lim,flag)\),表示数位的位数,在 \(SAM\) 上的节点,匹配的长度,是否有最高位限制,是否已经满足要求。
在 \(dfs\) 转移时,若当前节点能接着匹配枚举到的字符,就直接转移,若不能,则在 \(Parent\) 树上向上跳,直到能接着匹配,转移过程中判断是否满足条件即可。
\(code:\)
#include<bits/stdc++.h>
#define maxn 2010
#define maxd 55
#define mod 1000000007
using namespace std;
template<typename T> inline void read(T &x)
{
x=0;char c=getchar();bool flag=false;
while(!isdigit(c)){if(c=='-')flag=true;c=getchar();}
while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
if(flag)x=-x;
}
int n,d,tot=1,las=1,root=1,cnt;
int fa[maxn],len[maxn],ch[maxn][12],num[maxn],f[maxd][maxn][maxd][2][2];
char s[maxn],a[maxn],b[maxn];
void insert(int c)
{
int p=las,np=las=++tot;
len[np]=len[p]+1;
while(p&&!ch[p][c]) ch[p][c]=np,p=fa[p];
if(!p) fa[np]=root;
else
{
int q=ch[p][c];
if(len[q]==len[p]+1) fa[np]=q;
else
{
int nq=++tot;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
len[nq]=len[p]+1,fa[nq]=fa[q],fa[np]=fa[q]=nq;
while(ch[p][c]==q) ch[p][c]=nq,p=fa[p];
}
}
}
int dp(int pos,int cur,int lenth,bool lim,bool flag)
{
if(pos==cnt+1) return flag;
if(f[pos][cur][lenth][lim][flag]!=-1) return f[pos][cur][lenth][lim][flag];
int v=0,ma=9;
if(lim) ma=num[pos];
for(int c=0;c<=ma;++c)
{
if(flag) v=(v+dp(pos+1,cur,lenth,lim&&c==ma,1))%mod;
else
{
int p=cur;
if(ch[p][c]) v=(v+dp(pos+1,ch[p][c],lenth+1,lim&&c==ma,lenth+1>=d/2))%mod;
else
{
while(p&&!ch[p][c]) p=fa[p];
if(p) v=(v+dp(pos+1,ch[p][c],len[p]+1,lim&&c==ma,len[p]+1>=d/2))%mod;
else v=(v+dp(pos+1,root,0,lim&&c==ma,0))%mod;
}
}
}
return f[pos][cur][lenth][lim][flag]=v;
}
int solve(char *s)
{
cnt=strlen(s+1),memset(f,-1,sizeof(f));
for(int i=1;i<=cnt;++i) num[i]=s[i]-'0';
return dp(1,root,0,1,0);
}
int main()
{
scanf("%s%s%s",s+1,a+1,b+1),n=strlen(s+1),d=cnt=strlen(a+1),a[cnt--]--;
for(int i=1;i<=n;++i) insert(s[i]-'0');
while(a[cnt]-'0'==-1) a[cnt]--,a[cnt--]=9+'0';
printf("%d",(solve(b)-solve(a)+mod)%mod);
return 0;
}
题解 CF585F 【Digits of Number Pi】的更多相关文章
- CF585F Digits of Number Pi
题目 把\(s\)串所有长度为\(\lfloor \frac{d}{2}\rfloor\)的子串插入一个ACAM中,之后数位dp就好了,状态是\(dp_{i,j,0/1}\)第\(i\)位,在ACAM ...
- [CodeForces-585F]Digits of Number Pi
题目大意: 给你一个数字串s,一个序列范围l和r,(l和r的数字位数为d)求l到r中有多少个数,满足它的长度为d/2的子串,能够在s中被匹配. 思路: 首先将s中每一个长度为d/2的子串插入后缀自动机 ...
- 题解-Ehab's REAL Number Theory Problem
Ehab's REAL Number Theory Problem 前置知识 质数 分解质因数 无向无权图最小环<讲> Ehab's REAL Number Theory Problem/ ...
- LeetCode题解之Valid Triangle Number
1.题目描述 2.问题分析 暴力计算 3.代码 int triangleNumber(vector<int>& nums) { ; ) return res; ; i < n ...
- PAT甲题题解-1019. General Palindromic Number (20)-又是水题一枚
n转化为b进制的格式,问你该格式是否为回文数字(即正着写和倒着写一样)输出Yes或者No并且输出该格式又是水题... #include <iostream> #include <cs ...
- PAT甲题题解-1104. Sum of Number Segments (20)-(水题)
#include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...
- 【leetcode】1295. Find Numbers with Even Number of Digits
题目如下: Given an array nums of integers, return how many of them contain an even number of digits. Exa ...
- ICPC — International Collegiate Programming Contest Asia Regional Contest, Yokohama, 2018–12–09 题解
目录 注意!!此题解存在大量假算法,请各位巨佬明辨! Problem A Digits Are Not Just Characters 题面 题意 思路 代码 Problem B Arithmetic ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
随机推荐
- [源码解析] 从TimeoutException看Flink的心跳机制
[源码解析] 从TimeoutException看Flink的心跳机制 目录 [源码解析] 从TimeoutException看Flink的心跳机制 0x00 摘要 0x01 缘由 0x02 背景概念 ...
- 使用 Masstransit中的 Request/Response 与 Courier 功能实现最终一致性
简介 目前的.net 生态中,最终一致性组件的选择一直是一个问题.本地事务表(cap)需要在每个服务的数据库中插入消息表,而且做不了此类事务 比如:创建订单需要 余额满足+库存满足,库存和余额处于两个 ...
- webstorm 调试 typescript
{ "compilerOptions": { "target": "es5", "outFile": "bin ...
- Redis系列(八):数据结构List双向链表中阻塞版本之BLPOP、BRPOP和LINDEX、LINSERT、LRANGE命令详解
1.BRPOP.BLPOP BLPOP: BLPOP 是阻塞式列表的弹出原语. 它是命令 LPOP 的阻塞版本,这是因为当给定列表内没有任何元素可供弹出的时候, 连接将被 BLPOP 命令阻塞. 当给 ...
- C++ MFC 文件操作(新建,删除,剪切,复制,读数据,写数据,重命名)
源文件:http://pan.baidu.com/s/1ve0hV 这是运行mfc缺失的dll动态链接库:http://pan.baidu.com/s/17pGlT 哈哈,我也是初接触C++,基础的什 ...
- Mac上使用Docker Desktop启动Kubernetes,踩坑后终于搞掂
1 前言 Kubernetes又简称k8s,是Google开源的容器集群管理系统,最近也是火热.闲来无事(为了发文),捣鼓了一下,在Mac上搭建Kubernetes,遇到一些坑,也记录一下. 另外,D ...
- docker容器化python服务部署(supervisor-gunicorn-flask)
docker容器化python服务部署(supervisor-gunicorn-flask) 本文系作者原创,转载请注明出处: https://www.cnblogs.com/further-furt ...
- 「MoreThanJava」Day 3:构建程序逻辑的方法
「MoreThanJava」 宣扬的是 「学习,不止 CODE」,本系列 Java 基础教程是自己在结合各方面的知识之后,对 Java 基础的一个总回顾,旨在 「帮助新朋友快速高质量的学习」. 当然 ...
- 揭秘JAVA JVM内幕
在之前的文章 一步步解析java执行内幕 中,比较详细分析了java代码是如何一步一步在jvm中执行的,然而设计的的jvm核心技术点,并未做深入分析,本篇文章将重点分析jvm,涉及到的内容包括jvm内 ...
- The Shortest Statement,题解
题目链接 分析: 还是很明白的题意,直接分析问题,首先,这一题真的是给spfa用武之地,m比n大不超过20,但是这并不能使暴力不t,我们考虑一下如何改进一下,我们这样想,这个图只比它的生成树多最多21 ...