CodeForces 315D Sereja and Periods (思维DP)

#include<bits/stdc++.h>
using namespace std;
#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long
#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<int,int>
#define fi first                                  
#define se second
#define mk(x,y) make_pair(x,y)
const int mod=1e9+7;
const int maxn=200+10;
const int maxm=5e5+10;
const ll INF=1e18;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){
    if(y==0) return x;
    return gcd(y,x%y);
}
int b,d;
string a,c;
int nxt[maxn][26];
pii dp[maxn];
int main(){
    ios::sync_with_stdio(false);/*
    freopen("d://in.txt","r",stdin);
    freopen("d://out.txt","w",stdout);*/
    cin>>b>>d>>a>>c;
    rep(i,0,26) nxt[a.size()][i]=-1;
    for(int i=a.size()-1;i>=0;i--){
        rep(j,0,26) nxt[i][j]=nxt[i+1][j];
        nxt[i][a[i]-'a']=i;
    }
    rep(i,0,c.size()){
        int j=i,cnt=0,pos=0;
        while(1){
            pos=nxt[pos][c[j]-'a'];
            if(pos==-1) break;
            else pos=pos+1;
            j=(j+1)%c.size();
            if(!j) cnt++;
        }
        dp[i]=mk(j,cnt);
    }
    int cur=0,ans=0;
    rep(i,0,b){
        ans+=dp[cur].se;
        cur=dp[cur].fi;
    }
    cout<<ans/d<<"\n";
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值