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. XML文件的特点

    1.这个东西的规则比较简单,并且人眼看起来也比较容易理解,结构上也比较好操作,并且并无与任何编程语言绑定. 所以,很多编程语言就都为XML这个东西编写了读写XML的库,所以XML看起来所谓的通用,其实 ...

  2. 国画经典之梅花PSD素材

    国画经典之梅花图片PSD素材,由huiyi8素材网提供. 地址:http://www.huiyi8.com/meihua/​

  3. javascript(9)

    var a=[890,10]; window.alert(a.valueOf()); window.alert(a); //个人觉得,输出a.valueOf()和a没有什么区别 //使用valueOf ...

  4. 使用svg的几种方式

    <!-- 图片,背景,框架引入svg文件 --> <img src="test.svg" alt=""> <?xml versio ...

  5. 「LOJ#6121」「网络流 24 题」孤岛营救问题(BFS

    题目描述 1944 年,特种兵麦克接到国防部的命令,要求立即赶赴太平洋上的一个孤岛,营救被敌军俘虏的大兵瑞恩.瑞恩被关押在一个迷宫里,迷宫地形复杂,但幸好麦克得到了迷宫的地形图.迷宫的外形是一个长方形 ...

  6. CodeForces - 434D Nanami's Power Plant

    Codeforces - 434D 题目大意: 给定一个长为n的序列,序列中的第i为上的值\(x_i\),序列第i位上的值\(x_i\in[l_i,r_i]\),价值为\(f_i(x_i)\),其中\ ...

  7. 【LeetCode】027. Remove Element

    题目: Given an array and a value, remove all instances of that value in place and return the new lengt ...

  8. poj1456Supermarket——并查集压缩查找

    题目:http://poj.org/problem?id=1456 排序+贪心,每次选利润最大的,放在可能的最靠后的日期卖出,利用并查集快速找到下一个符合的日期. 代码如下: #include< ...

  9. POJ2253(djkstra求最长最短边)

    Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32257   Accepted: 10396 Descrip ...

  10. Oracle获取日期的特定部分

    (1)oracle中extract()函数从oracle 9i中引入,用于从一个date或者interval类型中截取到特定的部分 ,语法:extract ({ year | month | day ...