Gym - 101669A Concerts

题目大意就是给两个字符串,一个子串长为k,一个母串长为n,问母串中有多少个子序列长得和子串一样

题目里面的数据范围不对,所以可能有很多RE,N是小于1e5,k是小于300的数据范围

解题思路,从后往前处理子串的字符,如子串是ADBCD,就先处理D,将母串中所有D的位置的贡献+1,然后处理子串中的前一个字符‘C’,所以就在母串中找到所有的‘C’

然后这些'C'的位置的贡献为后继的D的贡献和,这个思路去一直处理就OK了

但是,我这样写了后TLE了,所以要优化,把求后继的字符的贡献和要能迅速获取就能解决问题,所以我开了30个树状数组来维护

不过这个算法不是最好的,听啊pei说可以用O(NK)的算法解决,不过我还没想出来

AC代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<cstring>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int MOD=1e9+7;
int num[30];
char s[405],t[100005];
int Pos[30][100005];
int lenP[30];
long long val[100005];
int T[30][100005];
int N,K;
struct node
{
    int c;
    int id;
    ll val;
};
void update(int which,int x,int val)
{
  while(x>0)
  {
    T[which][x]=(T[which][x]+val)%MOD;
    x-=x&(-x);
  }
  T[which][0]=(T[which][0]+val)%MOD;
}
int query(int which,int x)
{
  int res=0;
  if(x==0)return T[which][x];
  while(x<K+3)
  {
    res=(res+T[which][x])%MOD;
    x+=x&(-x);
  }
  return res;
}
queue<node>q;
int main()
{
  scanf("%d%d",&N,&K);
  memset(lenP,0,sizeof lenP);
  memset(Pos,0,sizeof Pos);
  for(int i=0;i<26;i++)scanf("%d",&num[i]);
  scanf("%s",s);
  scanf("%s",t);
  int sta=(int)(s[0]-'A');
  for(int i=0;i<K;i++){
    int c=(int)(t[i]-'A');
    Pos[c][lenP[c]++]=i;
  }
  for(int i=N-1;i>=0;i--){
    int c=(int)(s[i]-'A');
    if(i==N-1)
    {
      for(int k=0;k<lenP[c];k++)
      {
        val[Pos[c][k]]=1;
        update(c,Pos[c][k],1);
      }
    }
    else
    {
      int hc=(int)(s[i+1]-'A');
      int hhh=lenP[hc]-1;
      Pos[hc][hhh+1]=INF;
      for(int k=lenP[c]-1;k>=0;k--)
      {
        int now=Pos[c][k]+num[c];
        int low=upper_bound(Pos[hc],Pos[hc]+hhh+1,now)-Pos[hc];
        ll tans=0;
        tans=query(hc,Pos[hc][low])-query(hc,Pos[hc][hhh]+1);
        tans%=MOD;
        while(tans<0)tans+=MOD;
        node temp;
        temp.c=c;
        temp.id=Pos[c][k],temp.val=tans;
        q.push(temp);
        if(tans==0)lenP[c]--;
      }
      while(!q.empty())
      {
          node temp=q.front();q.pop();
          update(temp.c,temp.id,-val[temp.id]);
          val[temp.id]=temp.val;
          update(temp.c,temp.id,temp.val);
      }
    }
  }
  ll ans=query(sta,0)-query(sta,Pos[sta][lenP[sta]-1]+1);
  while(ans<0)ans+=MOD;
  printf("%lld\n",ans);
  return 0;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值