Codeword CodeForces - 666C (字符串计数)

链接

大意:求只含小写字母, 长度为n, 且可以与给定模板串匹配的字符串个数 (多组数据)

 

 

记模板串为P, 长为x, 总串为S.

设$f_i$为S为i时的匹配数, 考虑P最后一位的首次匹配位置.

若为S的最后一位,枚举P的前x-1位的首次匹配位置,其余位不能为下一个要匹配的字符,

有方案数$25^{i-x}\binom{i-1}{x-1}$; 否则的话, 最后一位可以放任意字符, 方案数$26f_{i-1}$.

即$f_i=25^{i-x}\binom{i-1}{x-1}+26f_{i-1}$.

这里可以发现结果只与模板串长度有关, 而长度种类数是$O(\sqrt{n})$的, 所以离线后双指针可以实现$O(n\sqrt{n})$.

 

 

#include <iostream>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <set>
#include <vector>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define pb push_back
#define x first
#define y second
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
void exgcd(ll a,ll b,ll &d,ll &x,ll &y){b?exgcd(b,a%b,d,y,x),y-=a/b*x:x=1,y=0,d=a;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head

const int N = 4e5+10, INF = 0x3f3f3f3f;
int ans[N], n, tot;
vector<pii> g[N];
char s[N];
ll p25[N], in[N];

int main() {
	scanf("%d%s", &n, s);
	int now = strlen(s);
	REP(i,1,n) {
		int op, x;
		scanf("%d", &op);
		if (op==1) {
			scanf("%s", s);
			now = strlen(s);
		} else {
			scanf("%d", &x);
			g[now].pb({x,++tot});
		}
	}
	p25[0] = 1;
	REP(i,1,N-1) {
		p25[i] = p25[i-1]*25%P;
		in[i] = inv(i);
	}
	REP(x,1,N-1) if (g[x].size()) { 
		sort(g[x].begin(),g[x].end());
		int mx = g[x].back().x, now = 0;
		ll C = 1, dp = 0;
		REP(i,x,mx) {
			while (g[x][now].x<i) ++now;
			dp = (dp*26+C*p25[i-x])%P;
			C = C*i%P*in[i+1-x]%P;
			while (g[x][now].x==i) ans[g[x][now++].y]=dp;
		}
	}
	REP(i,1,tot) printf("%d\n", ans[i]);
}

 

转载于:https://www.cnblogs.com/uid001/p/10363069.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值