U605564 后来的我们 题解
huangchuanyi · · 个人记录
题意
给出一个
特殊数据部分分
本题有多个特殊数据,我们从头开始爬塔。
(注意到:所有特殊数据都有一个性质
First 5pts
所有含
输出
#include<bits/stdc++.h>
using namespace std;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cout<<0;
return 0;
}
Second 5pts
从
#include<bits/stdc++.h>
using namespace std;
#define N 10010
int n,m,s[4],ans;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>n>>m;
for(int i=1;i<=m;++i){
cin>>s[i];
}
for(int i=1;i<=n;++i){
int k=i,d=1;
while(k){
for(int j=1;j<=m;++j){
if(k%10==s[j]){
d=0;
break;
}
}
if(!d) break;
k/=10;
}
ans+=d;
}
cout<<ans;
return 0;
}
因为代码逻辑是排掉所有含有
Third 5pts
只有
#include<bits/stdc++.h>
using namespace std;
#define N 110
int n,q;
int a,len,ans;
bool check(int x){
int l=x,s=0;
while(l){
++s;
l/=10;
}
for(int i=1;i<=q;++i){
for(int j=0;j+len<=s;++j){
if(a==x/int(pow(10,j))%int(pow(10,len))){
return 0;
}
}
}
return 1;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>n>>q;
for(int i=1;i<=q;++i){
cin>>a;
int x=a;
while(x){
++len;
x/=10;
}
}
for(int i=1;i<=n;++i){
if(check(i)){
++ans;
}
}
cout<<ans;
return 0;
}
The 30pts
看到数据范围
可以考虑
从
#include<bits/stdc++.h>
using namespace std;
#define N 110
int n,q;
int a[N],len[N],ans;
bool check(int x){
int l=x,s=0;
while(l){
++s;
l/=10;
}
for(int i=1;i<=q;++i){
for(int j=0;j+len[i]<=s;++j){
if(a[i]==x/int(pow(10,j))%int(pow(10,len[i]))){
return 0;
}
}
}
return 1;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>n>>q;
for(int i=1;i<=q;++i){
cin>>a[i];
int x=a[i];
while(x){
++len[i];
x/=10;
}
}
for(int i=1;i<=n;++i){
if(check(i)){
++ans;
}
}
cout<<ans;
return 0;
}
正解
思考
数据范围:
AC自动机
虽然知道了这是
针对及时返回,我们可以另外构造一个在线版的
关于 alert_p 的设定
我们定义
但如果该点不是字典树上被设置为
在经典
明显的,如果我们采用一般的跳
首先,我们将所有
这样,如果我们跑到一个节点
大致原理讲述完毕,还有部分细节处理请看代码
Code
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define N 3010
const ll Mod = 1e9+7;
ll m,n,dp[N][N],a[N],trie[N][10],cnt,fail[N],straight[N][10],alert[N];
string t,s[N];
bool isEnd[N];
void insert(string x){
int len=x.size();
int p=0;
for(int i=0;i<len;++i){
int c=x[i]-'0';
if(!trie[p][c]){
trie[p][c]=++cnt;
}
p=trie[p][c];
}
isEnd[p]=alert[p]=1;
}
void bfs(){
queue<int> q;
for(int i=0;i<10;++i){
if(trie[0][i]){
int v=trie[0][i];
straight[0][i]=v;
fail[v]=0;
alert[v]|=alert[0];
q.push(v);
}
}
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=0;i<10;++i){
if(trie[u][i]){
int v=trie[u][i];
straight[u][i]=v;
fail[v]=straight[fail[u]][i];
alert[v]|=alert[fail[v]];
q.push(v);
}
else{
straight[u][i]=straight[fail[u]][i];
}
}
}
}
inline ll dfs(int pos,int p,int lead,int limit){
if(pos<0){
return !lead;
}
if(dp[pos][p]!=-1&&!lead&&!limit){
return dp[pos][p];
}
int ret=0;
int res=limit?a[pos]:9;
for(int i=0;i<=res;++i){
int key=p;
if(!lead||i){
if(trie[key][i]){
key=trie[key][i];
}
else{
key=straight[fail[key]][i];
}
}
if(alert[key]) continue;
ret=(ret+dfs(pos-1,key,lead&&!i,limit&&i==res))%Mod;
}
if(!lead&&!limit){
dp[pos][p]=ret;
}
return ret;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
memset(dp,-1,sizeof(dp));
cin>>t>>m;
n=t.size();
for(int i=0;i<n;++i){
a[n-i-1]=t[i]-'0';
}
for(int i=1;i<=m;++i){
cin>>s[i];
insert(s[i]);
}
bfs();
cout<<dfs(n-1,0,1,1);
return 0;
}