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-corasickdynamic-programmingjuly12mediumxcwgf666

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+二分答案)的更多相关文章

  1. 【HDU3530】 [Sdoi2014]数数 (AC自动机+数位DP)

    3530: [Sdoi2014]数数 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 682  Solved: 364 Description 我们称一 ...

  2. 【bzoj3530】[Sdoi2014]数数 AC自动机+数位dp

    题目描述 我们称一个正整数N是幸运数,当且仅当它的十进制表示中不包含数字串集合S中任意一个元素作为其子串.例如当S=(22,333,0233)时,233是幸运数,2333.20233.3223不是幸运 ...

  3. BZOJ 3530 [SDOI2014]数数 (Trie图/AC自动机+数位DP)

    题目大意:略 裸的AC自动机+数位DP吧... 定义f[i][x][0/1]表示已经匹配到了第i位,当前位置是x,0表示没到上限,1到上限,此时数是数量 然而会出现虚拟前导零,即前几位没有数字的情况, ...

  4. HDU-4518 吉哥系列故事——最终数 AC自动机+数位DP

    题意:如果一个数中的某一段是长度大于2的菲波那契数,那么这个数就被定义为F数,前几个F数是13,21,34,55......将这些数字进行编号,a1 = 13, a2 = 21.现给定一个数n,输出和 ...

  5. ZOJ 3494 BCD Code(AC自动机+数位DP)

    BCD Code Time Limit: 5 Seconds      Memory Limit: 65536 KB Binary-coded decimal (BCD) is an encoding ...

  6. zoj3494BCD Code(ac自动机+数位dp)

    l链接 这题想了好一会呢..刚开始想错了,以为用自动机预处理出k长度可以包含的合法的数的个数,然后再数位dp一下就行了,写到一半发现不对,还要处理当前走的时候是不是为合法的,这一点无法移到trie树上 ...

  7. BZOJ3530:[SDOI2014]数数(AC自动机,数位DP)

    Description 我们称一个正整数N是幸运数,当且仅当它的十进制表示中不包含数字串集合S中任意一个元素作为其子串.例如当S=(22,333,0233)时,233是幸运数,2333.20233.3 ...

  8. BCD Code ZOJ - 3494 AC自动机+数位DP

    题意: 问A到B之间的所有整数,转换成BCD Code后, 有多少个不包含属于给定病毒串集合的子串,A,B <=10^200,病毒串总长度<= 2000. BCD码这个在数字电路课上讲了, ...

  9. 【JZOJ3624】【SDOI2014】数数(count) AC自动机+数位dp

    题面 100 容易想到使用AC自动机来处理禁忌子串的问题: 然后在自动机上数位dp,具体是: \(f_{i,j,0/1}\)表示填了\(i\)位,当前在自动机的第\(j\)个结点上,\(0\)表示当前 ...

随机推荐

  1. 基于docker搭建Jenkins+Gitlab+Harbor+Rancher架构实现CI/CD操作(续)---Harbor的安装

    前期安装文档:https://www.cnblogs.com/lq-93/p/11828626.html Harbor的作用:     开发提交代码至gitlab容器中,Jenkins拉取代码构建镜像 ...

  2. Zabbix日志监控插件

    #!/usr/bin/env python # coding:utf-8 import re import os import sys import logging logging.basicConf ...

  3. jade 学习笔记 - gulp 自动编译

    实时监控   jade -P -w .\test1.jade sublime 分栏,可以看到实时修改情况     1. 元素写法 doctype html <!--[if IE8]>< ...

  4. SpringBoot 整合jdbc和mybatis

    摘要 该文章主要为记录如何在SpringBoot项目中整合JDBC和MyBatis,在整合中我会使用简单的用法和测试用例,毕竟该文章目的是为了整合,而不是教大家如何去使用.希望大家多多包涵. 通用配置 ...

  5. cocos creator 3D | 蚂蚁庄园运动会星星球

    上一篇文章写了一个简易版的蚂蚁庄园登山赛,有小伙伴留言说想要看星星球的,那么就写起来吧! 效果预览 配置环境 cocos creator 3d 1.0.0 小球点击 3d里节点无法用 cc.Node. ...

  6. nyoj 63-小猴子下落 (模拟)

    63-小猴子下落 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:2 submit:5 题目描述: 有一颗二叉树,最大深度为D,且所有叶子的深度都相同 ...

  7. 在lldb调试中调用c++函数 - 如何使用QuartzCore里面的日志消息

    承接上一篇,上一篇讲到可以在lldb调试中调用QuartzCore.framework里的CA::Render::Object::show方法来是观察CA::Render模块内的类的信息,但是在lld ...

  8. 装饰者模式学习:模拟咖啡馆的点单系统来剖析装饰者模式的使用 + 装饰者模式在java I/O 中的应用

    通过模拟咖啡馆的点单系统来剖析装饰者模式的使用 参考:https://blog.csdn.net/gududedabai/article/details/81989196 一).传统的点单系统构建,每 ...

  9. vue 原生添加滚动加载更多

    vue中添加滚动加载更多,因为是单页面所以需要在跳出页面时候销毁滚动,要不会出现错乱.我们在mounted建立滚动,destroyed销毁滚动. mounted () { window.addEven ...

  10. MyBaits框架入门总结

    MBaits简介 联系方式:18873247271(微信同步) 廖先生 qq:1727292697 MyBatis的前身叫iBatis,本是apache的一个开源项目, 2010年这个项目由apach ...