bzoj 5337 [TJOI2018] str

Link

Solution

水题

直接 \(f[i][j]\) 表示以第 \(i\) 位为结束位置,当前已经匹配了前 \(j\) 个氨基酸的方案数

使用哈希转移

转移复杂度 \(O(10)\),总复杂度 \(1e7\)

Code

#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<iostream>
#include<queue>
#include<string>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<long long,long long> pll;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define rep(i,j,k) for(register int i=(int)(j);i<=(int)(k);i++)
#define rrep(i,j,k) for(register int i=(int)(j);i>=(int)(k);i--)
#define Debug(...) fprintf(stderr, __VA_ARGS__) ll read(){
ll x=0,f=1;char c=getchar();
while(c<'0' || c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0' && c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
} const int md = 1e9 + 7;
const ll mod = 2002070919ll;
const int maxn = 10010;
int n, len;
char s[maxn], t[maxn];
int l[110][12], a[110], f[maxn][110];
ll hsh[110][12], pw[maxn], h[maxn]; inline void add(int &x, int y) {
x += y;
if (x >= md) x -= md;
}
inline ll calc(int l, int r) {
return (h[r] - h[l - 1] * pw[r - l + 1] % mod + mod) % mod;
}
void work(){
n = read();
scanf("%s", s + 1);
len = strlen(s + 1);
pw[0] = 1; rep(i, 1, len) pw[i] = pw[i - 1] * 100 % mod;
rep(i, 1, len) h[i] = (h[i - 1] * 100 + s[i] - 'A') % mod;
rep(i, 1, n) {
a[i] = read();
rep(j, 1, a[i]) {
scanf("%s", t + 1);
l[i][j] = strlen(t + 1);
rep(k, 1, l[i][j]) hsh[i][j] = (hsh[i][j] * 100 + t[k] - 'A') % mod;
}
}
rep(i, 0, len) f[i][0] = 1;
rep(i, 1, len) rep(j, 1, n) {
rep(k, 1, a[j]) {
int nw = l[j][k];
if (i < nw || !f[i - nw][j - 1]) continue;
ll val = calc(i - nw + 1, i);
if (val == hsh[j][k]) add(f[i][j], f[i - nw][j - 1]);
}
}
int ans = 0;
rep(i, 1, len) add(ans, f[i][n]);
printf("%d\n", ans);
} int main(){
#ifdef LZT
freopen("in","r",stdin);
#endif work(); #ifdef LZT
Debug("My Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
}

Review

注意哈希的模数不能大过 \(INT\_MAX\),不然两个哈希值相乘会爆 \(\text{long long}\)

bzoj 5337 [TJOI2018] str的更多相关文章

  1. 【BZOJ5337】[TJOI2018]str(动态规划,哈希)

    [BZOJ5337][TJOI2018]str(动态规划,哈希) 题面 BZOJ 洛谷 题解 就很呆... 显然按层\(dp\),如果能够匹配上就进行转移,直接哈希判断是否能够匹配就好了... #in ...

  2. bzoj 5338: [TJOI2018]xor (树链剖分+可持久化01Trie)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=5338 题面: 5338: [TJOI2018]xor Time Limit: 30 Sec  ...

  3. BZOJ.5338.[TJOI2018]xor(可持久化Trie)

    BZOJ LOJ 洛谷 惊了,18年了还有省选出模板题吗= = 做这题就是练模板的,我就知道我忘的差不多了 询问一就用以DFS序为前缀得到的可持久化Trie做,询问二很经典的树上差分. 注意求询问二的 ...

  4. BZOJ.5339.[TJOI2018]教科书般的亵渎(拉格朗日插值) & 拉格朗日插值学习笔记

    BZOJ 洛谷 题意的一点说明: \(k\)次方这个\(k\)是固定的,也就是最初需要多少张亵渎,每次不会改变: 因某个怪物死亡引发的亵渎不会计分. 不难发现当前所需的张数是空格数+1,即\(m+1\ ...

  5. 洛谷 P4592: bzoj 5338: [TJOI2018]异或

    题目传送门:洛谷P4592. 题意简述: 题面说的很清楚了. 题解: 发现没有修改很快乐.再看异或最大值操作,很容易想到可持久化 01trie. 这里要把 01trie 搬到树上,有点难受. 树剖太捞 ...

  6. BZOJ5337 [TJOI2018]str

    题意 小豆参加了生物实验室.在实验室里,他主要研究蛋臼质.他现在研究的蛋臼质是由k个氨基酸按一定顺序构成的.每一个氨基酸都可能有a种碱基序 列si_j 构成.现在小豆有一个碱基串s,小豆想知道在这个碱 ...

  7. BZOJ 5334: [Tjoi2018]数学计算

    线段树裸题 难度在于认识到这个没法线性做 #include<cstdio> using namespace std; int n,mod,tr[400005]; void insert(i ...

  8. BZOJ 5336: [TJOI2018]party

    状压最长公共子序列的DP数组,一维最多K(15)个数,且相邻两个数的差不超过1,2^15种状态,预处理转移 #include<cstdio> #include<algorithm&g ...

  9. BZOJ 5338: [TJOI2018]xor 可持久化trie+dfs序

    强行把序列问题放树上,好无聊啊~ code: #include <bits/stdc++.h> #define N 200005 #define setIO(s) freopen(s&qu ...

随机推荐

  1. jsp九大内置对象和其作用详解

    jsp九大内置对象和其作用详解 JSP中一共预先定义了9个这样的对象,分别为:request.response.session.application.out.pagecontext.config.p ...

  2. JQuery调用iframe子页面函数/对象的方法

    父页面有个ID为mainfrm的iframe,iframe连接b.html,该页面有个函数test 在父页面调用b.html的test方法为: $("#mainfrm")[0].c ...

  3. Java丨DBCP连接池完整配置

    <!-- 数据源1 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicData ...

  4. array_2.array_rand

    从数组中随机取出一个或多个单元 <?php $arr = [1, 2, 3]; $rand = array_rand($arr, 2); var_dump($rand);

  5. hdu-2169 Computer(树形dp+树的直径)

    题目链接: Computer Time Limit: 1000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) ...

  6. 文章预告的自我挖坑系列——D3.js 系列之星光闪烁

    D3.js 是个神奇的工具,下面收集了一些与星星相关的可视化的例子,静待慢慢的把坑填上 雷达图http://bl.ocks.org/kevinschaul/8213691      星空 二维(一)h ...

  7. [算法]Trie树

    我是好文章的搬运工,原文来自博客园,博主一线码农,选自”6天通吃树结构“系列,地址:http://www.cnblogs.com/huangxincheng/archive/2012/11/25/27 ...

  8. bjwc Day2 玄学

    早晨起来很开心,因为昨天跟妹子聊天聊到很晚 然后看到了题,感觉:这tm才是冬令营呀! T1构造,并没有找到性质,暴力都懒得打 T2数位dp,状态比较麻烦,看来跟dmy想到一起了,然后搞一下搞完 T3放 ...

  9. HihoCoder1642 : 三角形面积和([Offer收割]编程练习赛37)(求面积)(扫描线||暴力)(占位)

    描述 如下图所示,在X轴上方一共有N个等腰直角三角形.这些三角形的斜边与X轴重合,斜边的对顶点坐标是(Xi, Yi). (11,5) (4,4) /\ /\(7,3) \ / \/\/ \ / /\/ ...

  10. 「P3385」【模板】负环(spfa

    题目描述 暴力枚举/SPFA/Bellman-ford/奇怪的贪心/超神搜索 输入输出格式 输入格式: 第一行一个正整数T表示数据组数,对于每组数据: 第一行两个正整数N M,表示图有N个顶点,M条边 ...