CodeChef FAVNUM FavouriteNumbers(AC自动机+数位dp+二分答案)
All submissions for this problem are available.
Chef likes numbers and number theory, we all know that. There are N digit strings that he particularly likes. He likes them so much that he defines some numbers to be beautiful numbers based on these digit strings.
Beautiful numbers are those numbers whose decimal representation contains at least one of chef's favorite digit strings as a substring. Your task is to calculate the Kth smallest number amongst the beautiful numbers in the range from L to R (both inclusive). If the number of beautiful numbers between L and R is less than K, then output "no such number".
Input
In the first line of input there will be integers L, R, K and N. Then N lines follow. Each line will contain a single string of decimal digits.
Output
Output one integer - the solution to the problem described above or a string "no such number" if there is no such number.
Constraints
- 1<=L<=R<=10^18
- 1<=K<=R-L+1
- 1<=N<=62
- 1<=The length of any Chef's favourite digit string<=18. Each string begins with a nonzero digit.
Example
Input:
1 1000000000 4 2
62
63 Output:
163
Input:
1 1 1 1
2 Output:
no such number
Input:
1 1000 15 2
6
22 Output:
67
Author:6★xcwgf666
Tester:6★laycurse
Editorial:http://discuss.codechef.com/problems/FAVNUM
Tags:aho-corasick, dynamic-programming, july12, medium, xcwgf666
Date Added:20-12-2011
Time Limit:0.1 - 0.146779 secs
Source Limit:50000 Bytes
Languages:C, CPP14, JAVA, PYTH, PYTH 3.6, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, PYP3, CLOJ, FS
题解:给你一些幸运数字,然后问你在一个区间内的第k大的,含有幸运数字的数是哪一个数字。
第K大,二分答案,然后考虑数位DP。如果是单个数字的话这样就可以了。多个数字,我们把它们放在AC自动机里面,然后dp[len][pos][flag]表示当前长度为len,走到AC自动机上pos点的时候取幸运数字状态为flag(是或否取到)的方案数。那么我们显然可以有转移方程:dp[len][pos][flag]=Σ dp[len-1][ch][flag || AC.T[ch].cnt],其中ch为pos的某一个后继节点。意思是,如果到下一个节点能够组成一个幸运数字,那么flag的状态就要相应改变。之后,按照普通数位dp的套路那样,记忆化搜索转移即可。
参考代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define maxn 2400
int n,tot;
ll L,R,K;
char s[];
int ch[maxn][],fail[maxn],val[maxn],last[maxn]; void Init()
{
tot=;
memset(ch[],,sizeof(ch[]));
memset(val,,sizeof(val));
}
int idx(char c){ return c - '';}
void Insert(char*s)
{
int u=,len=strlen(s);
for(int i=;i<len;++i)
{
int c=idx(s[i]);
if(!ch[u][c])
{
memset(ch[tot],,sizeof(ch[tot]));
val[tot]=;
ch[u][c]=tot++;
}
u=ch[u][c];
}
val[u]=;
}
void GetFail()
{
queue<int> q;
fail[]=;
for(int c=;c<;++c)
{
int u=ch[][c];
if(u){ fail[u]=;q.push(u);last[u]=; }
}
while(!q.empty())
{
int r=q.front(); q.pop();
val[r]|=val[fail[r]];
for(int c=;c<;++c)
{
int u=ch[r][c];
if(!u){ch[r][c]=ch[fail[r]][c];continue;}
q.push(u);
int v=fail[r];
fail[u]=ch[v][c];
last[u] = val[fail[u]]?fail[u]:last[fail[u]];
}
}
}
int dig[];
ll dp[][maxn][];
ll dfs(int len,int pos,bool flag,int lim)
{
if(len<=) return flag;
if(!lim&&dp[len][pos][flag]>=) return dp[len][pos][flag];
ll res=;
int sz=lim?dig[len]:;
for(int i=;i<=sz;++i)
{
int nxt=ch[pos][i];
res+=dfs(len-,nxt,val[nxt]||flag,lim&&i==sz);
}
if(!lim) dp[len][pos][flag]=res;
return res;
}
ll work(ll x)
{
int len=;
while(x)
{
dig[++len]=x%;
x/=;
}
return dfs(len,,,);
} int main()
{
scanf("%lld%lld%lld%d",&L,&R,&K,&n);
Init();
for(int i=;i<=n;++i)
{
scanf("%s",s);
Insert(s);
}
GetFail();
memset(dp,-,sizeof dp);
ll num=work(L-),mid,ans=;
while(L<=R)
{
mid=L+R>>;
if(work(mid)-num>=K) R=mid-,ans=mid;
else L=mid+;
}
if(ans) printf("%lld\n",ans);
else puts("no such number"); return ;
}
CodeChef FAVNUM FavouriteNumbers(AC自动机+数位dp+二分答案)的更多相关文章
- 【HDU3530】 [Sdoi2014]数数 (AC自动机+数位DP)
3530: [Sdoi2014]数数 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 682 Solved: 364 Description 我们称一 ...
- 【bzoj3530】[Sdoi2014]数数 AC自动机+数位dp
题目描述 我们称一个正整数N是幸运数,当且仅当它的十进制表示中不包含数字串集合S中任意一个元素作为其子串.例如当S=(22,333,0233)时,233是幸运数,2333.20233.3223不是幸运 ...
- BZOJ 3530 [SDOI2014]数数 (Trie图/AC自动机+数位DP)
题目大意:略 裸的AC自动机+数位DP吧... 定义f[i][x][0/1]表示已经匹配到了第i位,当前位置是x,0表示没到上限,1到上限,此时数是数量 然而会出现虚拟前导零,即前几位没有数字的情况, ...
- HDU-4518 吉哥系列故事——最终数 AC自动机+数位DP
题意:如果一个数中的某一段是长度大于2的菲波那契数,那么这个数就被定义为F数,前几个F数是13,21,34,55......将这些数字进行编号,a1 = 13, a2 = 21.现给定一个数n,输出和 ...
- ZOJ 3494 BCD Code(AC自动机+数位DP)
BCD Code Time Limit: 5 Seconds Memory Limit: 65536 KB Binary-coded decimal (BCD) is an encoding ...
- zoj3494BCD Code(ac自动机+数位dp)
l链接 这题想了好一会呢..刚开始想错了,以为用自动机预处理出k长度可以包含的合法的数的个数,然后再数位dp一下就行了,写到一半发现不对,还要处理当前走的时候是不是为合法的,这一点无法移到trie树上 ...
- BZOJ3530:[SDOI2014]数数(AC自动机,数位DP)
Description 我们称一个正整数N是幸运数,当且仅当它的十进制表示中不包含数字串集合S中任意一个元素作为其子串.例如当S=(22,333,0233)时,233是幸运数,2333.20233.3 ...
- BCD Code ZOJ - 3494 AC自动机+数位DP
题意: 问A到B之间的所有整数,转换成BCD Code后, 有多少个不包含属于给定病毒串集合的子串,A,B <=10^200,病毒串总长度<= 2000. BCD码这个在数字电路课上讲了, ...
- 【JZOJ3624】【SDOI2014】数数(count) AC自动机+数位dp
题面 100 容易想到使用AC自动机来处理禁忌子串的问题: 然后在自动机上数位dp,具体是: \(f_{i,j,0/1}\)表示填了\(i\)位,当前在自动机的第\(j\)个结点上,\(0\)表示当前 ...
随机推荐
- H5+app,自动更新后自动删除安装包
H5+app 自动删除安装包 一.前言 之前做好的app自动更新,遗留下了一个问题,就是自动更新后安装包没有自行删除掉. 好像现在的手机的系统是有安装完自动清理安装包的.想我这个H5+的app安装完后 ...
- powerdesigner连接Mysql进行反向工程并生成word文档图文教程
1 软件版本 windows7 64位 powerdesigner 15.1 Mysql 5.1.56 mysql-connector-odbc-3.51.30-winx64 对于mysql-conn ...
- Ansible之templates模板
一.jinja2简介解 Jinja2是Python下一个被广泛应用的模版引擎,他的设计思想来源于Djanjo的模板引擎,并扩展了其语法和一系列强大的功能.ansible的模板配置文件就是用jinja2 ...
- LoadRunner具体流程
创建负载测试场景场景目标:模拟10个用户同时登陆.搜索航班.购买机票.查看航班路线并退出打开Controller并创建一个新场景1.打开HP LoadRunner2.打开Controller在Load ...
- react-router-dom路由
- Install zabbix
- name: Create dir to keep install file file: path=/opt/pacheage state=directory follow=yes force=ye ...
- [ML机器学习 - Stanford University] - Week1 - 01 Introduction
What is Machine Learning? Two definitions of Machine Learning are offered. Arthur Samuel described i ...
- 2019-10-24:伪静态,VULHUB搭建靶场,宽字节sql注入,笔记
伪静态1,需要开启站点的重写机制,需要修改配httpd配置文件,将LoadModule rewrite_module modules/mod_rewrite.so注释取消,需要apache支持解析.h ...
- 【集训Day3 单调队列】【2018寒假集训Day 5更新】最大子序列和
最大子序列和(maxsum) [问题描述] 输入一个长度为n的整数序列(A1,A2,……,An),从中找出一段连续的长度不超过M的子序列,使得这个序列的和最大. 例如: 序列 1, -3, 5, 1, ...
- linux basic
一:date 语法: 打印日期:date [OPTION]..... [+FORMAT] 设定日期:date [MMDDhhmm] [[cc][YY][.ss] 创建带实时日期的文件 touch $ ...