链接

大意:求只含小写字母, 长度为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 (字符串计数)的更多相关文章

  1. codeforces 666C Codeword

    codeforces 666C Codeword 题意 q个询问,一种询问是给你一个字符串,还有一种是问长度为n的,包含当前字符串为子序列的字符串有多少个. 题解 容易写出式子,但是不好化简. 观察一 ...

  2. 【BZOJ-4180】字符串计数 后缀自动机 + 矩阵乘法

    4180: 字符串计数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 146  Solved: 66[Submit][Status][Discuss] ...

  3. 【BZOJ 4180】 4180: 字符串计数 (SAM+二分+矩阵乘法)

    4180: 字符串计数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 164  Solved: 75 Description SD有一名神犇叫做Oxe ...

  4. 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 ...

  5. CodeForces 625B 字符串模拟+思维

    题意 给出字符串a与b 可以将a中的单个字符改为# 问最少改多少次 a中就找不到b了 一开始想的是用strstr 因为如果找到 可以将strstr(a,b)-a+1改成# 即改首字母 用while循环 ...

  6. bzoj 4180: 字符串计数

    Description SD有一名神犇叫做Oxer,他觉得字符串的题目都太水了,于是便出了一道题来虐蒟蒻yts1999. 他给出了一个字符串T,字符串T中有且仅有4种字符 'A', 'B', 'C', ...

  7. CodeForces - 1025C 字符串处理,画一个圆。。。

    题目链接: https://vjudge.net/problem/1810469/origin 题目大意: 给你一个字符串,中间切一刀,左右两边均反转,然后右边的串拼接到左边上. 思路: 比如  aa ...

  8. Swapping Characters CodeForces - 903E (字符串模拟)

    大意: 给定k个字符串, 长度均为n, 求是否存在一个串S, 使得k个字符串都可以由S恰好交换两个字符得到. 暴力枚举交换的两个字符的位置, 计算出交换后与其他串不同字符的个数, 若为1或>2显 ...

  9. Maximum Questions CodeForces - 900E (字符串,dp)

    大意:给定长$n$的字符串$s$, 只含'a','b','?', '?'可以替换为任意字符, 在给定长$t$的字符串, "ababab...", 求替换尽量少的'?', 使得$s$ ...

随机推荐

  1. js 数组操作

    toString():把数组转换成一个字符串 toLocaleString():把数组转换成一个字符串 join():把数组转换成一个用符号连接的字符串 shift():将数组头部的一个元素移出 un ...

  2. linux测试带宽命令,Linux服务器网络带宽测试iperf

    linux测试带宽命令,Linux服务器网络带宽测试iperf必须先运行iperf serveriperf -s -i 2客户端iperf -c 服务端IP地址 iperf原理解析 iperf工具可以 ...

  3. C/C++之extern "C"的用法解析

    extern "C"的用法解析 http://blog.sina.com.cn/u/494a1ebc010004g5 C++中extern “C”含义深层探索 1.引言 C++语言 ...

  4. web前端----JavaScript的DOM(三)

    一.JS中for循环遍历测试 for循环遍历有两种 第一种:是有条件的那种,例如    for(var i = 0;i<ele.length;i++){} 第二种:for (var i in l ...

  5. MySQL数据库----多表查询

    一.介绍 首先先准备表 员工表和部门表 #建表 create table department( id int, name varchar(20) ); create table employee1( ...

  6. Linux下Oracle常用命令

    1. 备份表 exp database_user/pass tables='(table1,table2)' file=filename.dmp(例如:exp ismrenbao/iflytek ta ...

  7. Android widget

    1,TextView :走马灯效果 2,EditText ,AutoCompleteText MutiAutoCompleteTextView 3,Button,ImageButton,RadioBu ...

  8. Python3基础 super 子类调用父类的__init__

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  9. github issues的操作

    https://help.github.com/en/articles/searching-issues-and-pull-requests 根据 reporter筛选issues https://h ...

  10. MongoDB树形结构表示法

    http://docs.mongodb.org/manual/tutorial/model-tree-structures/ MongoDB五种树形结构表示法 第一种:父链接结构 db.categor ...