考虑用数位 \(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】的更多相关文章

  1. CF585F Digits of Number Pi

    题目 把\(s\)串所有长度为\(\lfloor \frac{d}{2}\rfloor\)的子串插入一个ACAM中,之后数位dp就好了,状态是\(dp_{i,j,0/1}\)第\(i\)位,在ACAM ...

  2. [CodeForces-585F]Digits of Number Pi

    题目大意: 给你一个数字串s,一个序列范围l和r,(l和r的数字位数为d)求l到r中有多少个数,满足它的长度为d/2的子串,能够在s中被匹配. 思路: 首先将s中每一个长度为d/2的子串插入后缀自动机 ...

  3. 题解-Ehab's REAL Number Theory Problem

    Ehab's REAL Number Theory Problem 前置知识 质数 分解质因数 无向无权图最小环<讲> Ehab's REAL Number Theory Problem/ ...

  4. LeetCode题解之Valid Triangle Number

    1.题目描述 2.问题分析 暴力计算 3.代码 int triangleNumber(vector<int>& nums) { ; ) return res; ; i < n ...

  5. PAT甲题题解-1019. General Palindromic Number (20)-又是水题一枚

    n转化为b进制的格式,问你该格式是否为回文数字(即正着写和倒着写一样)输出Yes或者No并且输出该格式又是水题... #include <iostream> #include <cs ...

  6. PAT甲题题解-1104. Sum of Number Segments (20)-(水题)

    #include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...

  7. 【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 ...

  8. ICPC — International Collegiate Programming Contest Asia Regional Contest, Yokohama, 2018–12–09 题解

    目录 注意!!此题解存在大量假算法,请各位巨佬明辨! Problem A Digits Are Not Just Characters 题面 题意 思路 代码 Problem B Arithmetic ...

  9. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

随机推荐

  1. loadRunnner中90%的响应时间

    参考博客https://blog.csdn.net/lengyue_112/article/details/1095320?utm_source=blogxgwz4 LR在场景执行完了会出个报告,其中 ...

  2. android java层通过jni加载使用第三方的so库

    1.例如我们自己编译一个so库,我们的其他模块要加载如何操作了 首先在c盘新建立一个文件夹sb,在sb下面新建立一个文件夹jni,如果你要使用ndk编译so库,必须需要有jni目录 2.在jni目录下 ...

  3. Maximum Subsequence Sum(java)

    7-1 Maximum Subsequence Sum(25 分) Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A con ...

  4. Freemarker在replace替换是对NULL值的处理

    freemarker的对象调用内建函数时,比如userInfo对象的birthDay函数,页面${userInfo.birthDay}调用,当我想将birthDay值中的“-”替换为“/”时,${us ...

  5. shell基本正则表达式

    基本正则表达式 星号* 匹配它前面的字符串或正则表达式任意次(包括0次).比如,“1122*” 将匹配11+1个或多个2,其可能匹配的字符串将是112.1122.112222.11223343等 句点 ...

  6. Python实用笔记 (23)面向对象高级编程——使用__slots__

    正常情况下,当我们定义了一个class,创建了一个class的实例后,我们可以给该实例绑定任何属性和方法,这就是动态语言的灵活性.先定义class: class Student(object): pa ...

  7. ShellExecute指定IE浏览器打开网页

    ShellExecute(NULL,L"open", L"iexplore.exe", L"www.baidu.com", NULL, SW ...

  8. MFC 添加C++类,别的类不通过C++类的定义的对象就可以直接调用C++类里面的成员函数;

    MFC 添加C++类,不用定义C++类的对象,别的类不通过C++类的定义的对象就可以直接调用C++类里面的成员函数: 1先在mfc程序中添加普通类CProdata,然后删除头文件Prodata.h里面 ...

  9. 使用Fiddler模拟Post请求

    做了一个动态的GIF来做演示,应该更加直观些. (完)

  10. (私人收藏)Vue.js手册及教程

    (私人收藏)Vue.js手册及教程 https://pan.baidu.com/s/1XG1XdbbdBQm7cyhQKUIrRQ5lrt Vue.js手册及教程 Vue.js 教程 Vue.js 安 ...