bzoj 5337 [TJOI2018] str
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的更多相关文章
- 【BZOJ5337】[TJOI2018]str(动态规划,哈希)
[BZOJ5337][TJOI2018]str(动态规划,哈希) 题面 BZOJ 洛谷 题解 就很呆... 显然按层\(dp\),如果能够匹配上就进行转移,直接哈希判断是否能够匹配就好了... #in ...
- bzoj 5338: [TJOI2018]xor (树链剖分+可持久化01Trie)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=5338 题面: 5338: [TJOI2018]xor Time Limit: 30 Sec ...
- BZOJ.5338.[TJOI2018]xor(可持久化Trie)
BZOJ LOJ 洛谷 惊了,18年了还有省选出模板题吗= = 做这题就是练模板的,我就知道我忘的差不多了 询问一就用以DFS序为前缀得到的可持久化Trie做,询问二很经典的树上差分. 注意求询问二的 ...
- BZOJ.5339.[TJOI2018]教科书般的亵渎(拉格朗日插值) & 拉格朗日插值学习笔记
BZOJ 洛谷 题意的一点说明: \(k\)次方这个\(k\)是固定的,也就是最初需要多少张亵渎,每次不会改变: 因某个怪物死亡引发的亵渎不会计分. 不难发现当前所需的张数是空格数+1,即\(m+1\ ...
- 洛谷 P4592: bzoj 5338: [TJOI2018]异或
题目传送门:洛谷P4592. 题意简述: 题面说的很清楚了. 题解: 发现没有修改很快乐.再看异或最大值操作,很容易想到可持久化 01trie. 这里要把 01trie 搬到树上,有点难受. 树剖太捞 ...
- BZOJ5337 [TJOI2018]str
题意 小豆参加了生物实验室.在实验室里,他主要研究蛋臼质.他现在研究的蛋臼质是由k个氨基酸按一定顺序构成的.每一个氨基酸都可能有a种碱基序 列si_j 构成.现在小豆有一个碱基串s,小豆想知道在这个碱 ...
- BZOJ 5334: [Tjoi2018]数学计算
线段树裸题 难度在于认识到这个没法线性做 #include<cstdio> using namespace std; int n,mod,tr[400005]; void insert(i ...
- BZOJ 5336: [TJOI2018]party
状压最长公共子序列的DP数组,一维最多K(15)个数,且相邻两个数的差不超过1,2^15种状态,预处理转移 #include<cstdio> #include<algorithm&g ...
- BZOJ 5338: [TJOI2018]xor 可持久化trie+dfs序
强行把序列问题放树上,好无聊啊~ code: #include <bits/stdc++.h> #define N 200005 #define setIO(s) freopen(s&qu ...
随机推荐
- openstack制作镜像官网地址
http://docs.ocselected.org/openstack-manuals/kilo/image-guide/content/ch_creating_images_automatical ...
- jQuery选项卡tabulous
jQuery选项卡tabulous,jQuery,选项卡,tab标签切换代码,扁平设计,jQuery选项卡tabulous是一款支持Scale.Slide.Scale Up.Flip等效果jquery ...
- css3线性渐变兼容
火狐浏览器: background:-moz-linear-gradient(top, red, rgba(0, 0, 255, 0.5)); 谷歌: .l6{background: -webkit- ...
- 重新拾取:ASP.NET Core WebApi 使用Swagger支持授权认证
园子里已经有很多.NET Core 集成Swagger的文章,但对于使用授权的介绍蛮少的. public static class SwaggerServiceExtensions { public ...
- codeforces 659A A. Round House(水题)
题目链接: A. Round House time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 【LeetCode】064. Minimum Path Sum
题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...
- 运维程序】简单的命令控制器(支持定时命令执行、重复定时任务命令和进程管理,开发这个小程序主要是为了方便管理服务进程)【个人github项目】
一.前言: command-controller 一个运维程序,简单的命令控制器(支持定时命令执行和重复定时命令,开发这个程序主要是为了方便管理服务进程) 本来是要用python做的,但是之前做ffm ...
- /etc/bashrc和/etc/profile
在一般的 linux 或者 unix 系统中, 都可以通过编辑 bashrc 和 profile来设置用户的工作环境, 很多文章对于 profile 和 bashrc 也都有使用, 但究竟每个文件都有 ...
- POJ1287(最小生成树入门题)
Networking Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7753 Accepted: 4247 Descri ...
- Python 通用日志模块
import os base_dir=os.path.dirname(os.path.dirname(__file__)) base_db=os.path.join(base_dir,'db') ba ...