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]);
}
Codeword CodeForces - 666C (字符串计数)的更多相关文章
- codeforces 666C Codeword
codeforces 666C Codeword 题意 q个询问,一种询问是给你一个字符串,还有一种是问长度为n的,包含当前字符串为子序列的字符串有多少个. 题解 容易写出式子,但是不好化简. 观察一 ...
- 【BZOJ-4180】字符串计数 后缀自动机 + 矩阵乘法
4180: 字符串计数 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 146 Solved: 66[Submit][Status][Discuss] ...
- 【BZOJ 4180】 4180: 字符串计数 (SAM+二分+矩阵乘法)
4180: 字符串计数 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 164 Solved: 75 Description SD有一名神犇叫做Oxe ...
- Codeforces Beta Round #17 C. Balance (字符串计数 dp)
C. Balance time limit per test 3 seconds memory limit per test 128 megabytes input standard input ou ...
- CodeForces 625B 字符串模拟+思维
题意 给出字符串a与b 可以将a中的单个字符改为# 问最少改多少次 a中就找不到b了 一开始想的是用strstr 因为如果找到 可以将strstr(a,b)-a+1改成# 即改首字母 用while循环 ...
- bzoj 4180: 字符串计数
Description SD有一名神犇叫做Oxer,他觉得字符串的题目都太水了,于是便出了一道题来虐蒟蒻yts1999. 他给出了一个字符串T,字符串T中有且仅有4种字符 'A', 'B', 'C', ...
- CodeForces - 1025C 字符串处理,画一个圆。。。
题目链接: https://vjudge.net/problem/1810469/origin 题目大意: 给你一个字符串,中间切一刀,左右两边均反转,然后右边的串拼接到左边上. 思路: 比如 aa ...
- Swapping Characters CodeForces - 903E (字符串模拟)
大意: 给定k个字符串, 长度均为n, 求是否存在一个串S, 使得k个字符串都可以由S恰好交换两个字符得到. 暴力枚举交换的两个字符的位置, 计算出交换后与其他串不同字符的个数, 若为1或>2显 ...
- Maximum Questions CodeForces - 900E (字符串,dp)
大意:给定长$n$的字符串$s$, 只含'a','b','?', '?'可以替换为任意字符, 在给定长$t$的字符串, "ababab...", 求替换尽量少的'?', 使得$s$ ...
随机推荐
- Azkaban学习笔记(二)
官方文档:http://azkaban.github.io/ 一.Azkaban主要的组成: 1. 关系型数据库——MySQL 2. AzkabanWebServer 3. AzkabanExcuto ...
- python练习-生成一个1到50的大字符串每个数字之间有个空格
#-*-encoding:UTF-8-*- string=[] for i in range(1,51): string.append(str(i)) print string#打印一下string ...
- python模块-json、pickle、shelve
json模块 用于文件处理时其他数据类型与js字符串之间转换.在将其他数据类型转换为js字符串时(dump方法),首先将前者内部所有的单引号变为双引号,再整体加上引号(单或双)转换为js字符串:再使用 ...
- 企业应用开发中最常用c++库
log4cpp,http://log4cpp.sourceforge.net/,跟log4j一样,不说最重要,绝对是最常用的. zk 客户端,https://blog.csdn.net/yangzhe ...
- MemcacheQ安装
一.memcacheq介绍 特性: 1.简单易用 2.处理速度快 3.多条队列 4.并发性能好 5.与memcache的协议兼容 6.在zend framework中使用方便 memcacheq依赖于 ...
- 在wamp 2.0环境下面安装Zend Optimizer的方法
原文链接:http://blog.sina.com.cn/s/blog_8dc13ec50101pbat.html 我是用WAMP来做PHP的服务器,进行本机测试和开发PHP项目. wamp环境是刚刚 ...
- datagridview控件的使用
http://home.cnblogs.com/group/topic/40730.html datagridview定位到最后一行的方法 this.dataGridView2.CurrentCell ...
- codeforces 1097 Hello 2019
又回来了.. A - Gennady and a Card Game 好像没什么可说的了. #include<bits/stdc++.h> using namespace std; cha ...
- WebForm、MVC、流式计算
11月.NET技术讨论会圆满结束,会议纪要及相关文档如下如下: 1.WebForm预编译演示 文档:http://gitlab.light.fang.com/kongguDonet.Demo/Pre ...
- 解决 Github:failed to add file / to index 问题
参考: Github:failed to add file / to index 解决 Github:failed to add file / to index 问题 在通过Github for Ma ...