题目传送门

https://lydsy.com/JudgeOnline/problem.php?id=2085

题解

考虑暴力 DP 的做法。令 \(dp[i][j]\) 表示以 \(j\) 为开头的子串,并且已经总共出现 \(i\) 次的最小长度。

\[dp[i][j] = \min_{k=1}^n\{dp[i-1][k] + len_j - LC(j, k) \}
\]

其中 \(LC(j, k)\) 表示最长的 \(j\) 的后缀等于 \(k\) 的前缀。


然后这个东西可以用矩阵来加速。

矩阵的定义就是先加再取 \(\min\)。


时间复杂度 \(O(n^3\log m)\)。

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;} typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii; template<typename I> inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
} const int N = 200 + 7;
const int M = 1e5 + 7;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int base = 1997; int n, m, mxl;
int len[N];
char s[N][M];
ull ha[N][M], bin[M]; struct Matrix {
ll a[N][N]; inline Matrix() { memset(a, 0x3f, sizeof(a)); }
inline Matrix(const int &x) {
memset(a, 0x3f, sizeof(a));
for (int i = 1; i <= n; ++i) a[i][i] = x;
} inline Matrix operator * (const Matrix &b) {
Matrix c;
for (int k = 1; k <= n; ++k)
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
smin(c.a[i][j], a[i][k] + b.a[k][j]);
return c;
}
} A, B; inline Matrix fpow(Matrix x, int y) {
Matrix ans(0);
for (; y; y >>= 1, x = x * x) if (y & 1) ans = ans * x;
return ans;
} inline ull get_hash(ull *h, int l, int r) { return h[r] - h[l - 1] * bin[r - l + 1]; } inline void work() {
bin[0] = 1;
for (int i = 1; i <= mxl; ++i) bin[i] = bin[i - 1] * base;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
int cnt = 0;
for (int k = 1; k <= std::min(len[i], len[j]) - 1; ++k)
if (get_hash(ha[i], 1, k) == get_hash(ha[j], len[j] - k + 1, len[j])) smax(cnt, k);
A.a[j][i] = len[j] - cnt;
// dbg("i = %d, j = %d, l = %d\n", i, j, cnt);
}
for (int i = 1; i <= n; ++i) B.a[i][1] = len[i];//, dbg("len[%d] = %d\n", i, len[i]);
B = fpow(A, m - 1) * B;
// B = A * B;
ll ans = INF;
for (int i = 1; i <= n; ++i) smin(ans, B.a[i][1]);//, dbg("B.a[%d][1] = %lld\n", i, B.a[i][1]);
printf("%lld\n", ans);
} inline void init() {
read(n), read(m);
for (int i = 1; i <= n; ++i) {
scanf("%s", s[i] + 1);
len[i] = strlen(s[i] + 1), smax(mxl, len[i]);
for (int j = 1; j <= len[i]; ++j) ha[i][j] = ha[i][j - 1] * base + (s[i][j] - 'a' + 1);
}
} int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}

bzoj2085 [Poi2010]Hamsters 矩阵快速幂+字符串hash的更多相关文章

  1. HDU - 2604 矩阵快速幂 字符串递推 两种解法

    记dp[i]为长度i且符合题意的方案数,dp[n]就是解 符合方案的是不含fmf和fff子串的字符串 考虑如何从前面几项递推出后面第i项 (★表示存在生成的非法方案)←其实没啥用处 i=1时 m③ f ...

  2. 【BZOJ4002】[JLOI2015]有意义的字符串(数论,矩阵快速幂)

    [BZOJ4002][JLOI2015]有意义的字符串(数论,矩阵快速幂) 题面 BZOJ 洛谷 题解 发现我这种题总是做不动... 令\(A=\frac{b+\sqrt d}{2},B=\frac{ ...

  3. luogu3263/bzoj4002 有意义的字符串 (数学+矩阵快速幂)

    首先我们发现$\frac{b+\sqrt{d}}{2}$这个形式好像一元二次方程的求根公式啊(???反正我发现不了) 然后我们又想到虽然这个东西不好求但是$(\frac{b-\sqrt{d}}{2}) ...

  4. BZOJ.4180.字符串计数(后缀自动机 二分 矩阵快速幂/倍增Floyd)

    题目链接 先考虑 假设S确定,使构造S操作次数最小的方案应是:对T建SAM,S在SAM上匹配,如果有S的转移就转移,否则操作数++,回到根节点继续匹配S.即每次操作一定是一次极大匹配. 简单证明:假设 ...

  5. 51nod 1197 字符串的数量 V2(矩阵快速幂+数论?)

    接上一篇,那个递推式显然可以用矩阵快速幂优化...自己随便YY了下就出来了,学了一下怎么用LaTeX画公式,LaTeX真是个好东西!嘿嘿嘿 如上图.(刚画错了一发...已更新 然后就可以过V2了 or ...

  6. $bzoj1009-HNOI2008$ $GT$考试 字符串$dp$ 矩阵快速幂

    题面描述 阿申准备报名参加\(GT\)考试,准考证号为\(N\)位数\(x_1,x_2,...,x_n\ (0\leq x_i\leq 9)\),他不希望准考证号上出现不吉利的数字. 他的不吉利数字\ ...

  7. POJ2778(SummerTrainingDay10-B AC自动机+矩阵快速幂)

    DNA Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17160   Accepted: 6616 Des ...

  8. 51nod 算法马拉松18 B 非010串 矩阵快速幂

    非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...

  9. hdu2604(递推,矩阵快速幂)

    题目链接:hdu2604 这题重要的递推公式,找到公式就很easy了(这道题和hdu1757(题解)类似,只是这道题需要自己推公式) 可以直接找规律,推出递推公式,也有另一种找递推公式的方法:(PS: ...

随机推荐

  1. 从 Quora 的 187 个问题中学习机器学习和NLP

    从 Quora 的 187 个问题中学习机器学习和NLP 原创 2017年12月18日 20:41:19 作者:chen_h 微信号 & QQ:862251340 微信公众号:coderpai ...

  2. React-Native 之 GD (三)近半小时热门

    1.设置页面跳转 半小时热门组件  GDHalfHourHot.js /** * 近半小时热门 */ import React, { Component } from 'react'; import ...

  3. 自定义控件 - 切换开关:SwitchView

    自定义控件一般的几个步骤:1.初始化相关背景图片,布局文件,自定义属性2.设置控件宽高OnMeasure()3.布局或者排版OnLayout()4.绘制控件OnDraw()5.处理触摸事件OnTouc ...

  4. linux设置开机启动程序?

    /etc/rc.d/init.d 是 /etc/init.d的目标链接. 如果/etc/rc.d下面没有 rc.local脚本文件, 则需要 手动创建: 而 /etc/bashrc 是在登陆bash ...

  5. vundle的安装笔记-20160721

    vundle是一个vim管理插件, 而bundle是命令, 用来操作vundle的. bundle 英[ˈbʌndəl] 美[ˈbʌndəl] n. 捆,束,包:大量:一大笔钱:极度 v. 归拢:捆: ...

  6. idea的热部署

    1:先找到你要热部署的tomcat之后 ,在设置tomcat时  先选择 server,里面有On 'Update' action ()  和 On frame deactivation 这两项  都 ...

  7. String 和 new String()的区别

    String 和 new String()的区别 For Example String str1 = "ABC" String str2 = new String("AB ...

  8. 数组的includes方法

    Array.prototype.includes方法返回一个布尔值,表示某个数组是否包含给定的值,与字符串的includes方法类似.该方法属于 ES7 ,但 Babel 转码器已经支持. [1, 2 ...

  9. js获取select下拉框中的值

    现在有一id为userType的下拉框,怎么获取选中的值: 用户类型: <select name="type" id="userType"> < ...

  10. CDH平台搭建遇到的问题

    安装之前遇到的问题:   Note: Forwarding request to 'systemctl disable NetworkManager.service'. Removed symlink ...