LightOJ 1268 Unlucky Strings (KMP+矩阵快速幂)
题意:给出一个字符集和一个字符串和正整数n,问由给定字符集组成的所有长度为n的串中不以给定字符串为连续子串的有多少个?
析:n 实在是太大了,如果小的话,就可以用动态规划做了,所以只能用矩阵快速幂来做了,dp[i][j] 表示匹配完 i 到匹配 j 个有多少种方案,利用矩阵的性质,就可以快速求出长度为 n 的个数,对于匹配的转移,正好可以用KMP的失配函数来转移。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int maxn = 50 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r > 0 && r <= n && c > 0 && c <= m;
} char s[maxn], t[maxn];
int f[maxn]; struct Matrix{
unsigned int a[maxn][maxn];
int n;
Matrix(int nn) : n(nn){
memset(a, 0, sizeof a);
} void toOne(){
for(int i = 0; i < n; ++i)
a[i][i] = 1;
} friend Matrix operator * (const Matrix &lhs, const Matrix &rhs){
Matrix res(lhs.n);
for(int i = 0; i < lhs.n; ++i)
for(int j = 0; j < rhs.n; ++j)
for(int k = 0; k < lhs.n; ++k)
res.a[i][j] += lhs.a[i][k] * rhs.a[k][j];
return res;
}
}; Matrix fast_pow(Matrix a, int n){
Matrix res(a.n);
res.toOne();
while(n){
if(n&1) res = res * a;
a = a * a;
n >>= 1;
}
return res;
} void getFail(char *s, int n){
f[0] = f[1] = 0;
for(int i = 1; i < n; ++i){
int j = f[i];
while(j && s[j] != s[i]) j = f[j];
f[i+1] = s[i] == s[j] ? j+1 : 0;
}
} int main(){
int T; cin >> T;
for(int kase = 1; kase <= T; ++kase){
scanf("%d", &n);
scanf("%s %s", t, s);
int lens = strlen(s);
getFail(s, lens);
Matrix ans(lens);
for(int i = 0; i < lens; ++i)
for(int k = 0; t[k]; ++k){
int j = i;
while(j && s[j] != t[k]) j = f[j];
if(s[j] == t[k]) ++j;
++ans.a[i][j];
}
ans = fast_pow(ans, n);
unsigned int res = 0;
for(int i = 0; i < lens; ++i) res += ans.a[0][i];
printf("Case %d: %u\n", kase, res);
}
return 0;
}
LightOJ 1268 Unlucky Strings (KMP+矩阵快速幂)的更多相关文章
- [bzoj1009](HNOI2008)GT考试 (kmp+矩阵快速幂加速递推)
Description 阿 申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0<=Xi<=9),他不希望准考证号上出现不吉利的数字.他的不吉利数学 A1A2...Am(0&l ...
- LightOJ 1268 Unlucky Strings(KMP+矩阵乘法+基础DP)
题意 给出字符串的长度 \(n\) ,以及该字符串是由哪些小写字母组成,现给出一个坏串 \(S\) ,求存在多少种不同的字符串,使得其子串不含坏串. \(1 \leq n \leq 10^9\) \( ...
- LightOj 1065 - Number Sequence (矩阵快速幂,简单)
题目 和 LightOj 1096 - nth Term 差不多的题目和解法,这道相对更简单些,万幸,这道比赛时没把模版给抽风坏. #include<stdio.h> #include&l ...
- 2018.10.22 bzoj1009: [HNOI2008]GT考试(kmp+矩阵快速幂优化dp)
传送门 f[i][j]f[i][j]f[i][j]表示从状态"匹配了前i位"转移到"匹配了前j位"的方案数. 这个东西单次是可以通过跳kmp的fail数组得到的 ...
- BZOJ 1009 [HNOI2008]GT考试 (KMP + 矩阵快速幂)
1009: [HNOI2008]GT考试 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 4266 Solved: 2616[Submit][Statu ...
- LightOJ 1070 Algebraic Problem:矩阵快速幂 + 数学推导
题目链接:http://lightoj.com/volume_showproblem.php?problem=1070 题意: 给你a+b和ab的值,给定一个n,让你求a^n + b^n的值(MOD ...
- bzoj1009 [HNOI2008]GT考试——KMP+矩阵快速幂优化DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1009 字符串计数DP问题啊...连题解都看了好多好久才明白,别提自己想出来的蒟蒻我... 首 ...
- LightOj 1096 - nth Term (矩阵快速幂,简单)
题目 这道题是很简单的矩阵快速幂,可惜,在队内比赛时我不知什么时候抽风把模版中二分时判断的 ==1改成了==0 ,明明觉得自己想得没错,却一直过不了案例,唉,苦逼的比赛状态真让人抓狂!!! #incl ...
- [HNOI2008][bzoj1009] GT考试 [KMP+矩阵快速幂]
题面 传送门 思路 首先,如果$n$和$m$没有那么大的话,有一个非常显然的dp做法: 设$dp[i][j]$表示长度为i的字符串,最后j个可以匹配模板串前j位的情况数 那么显然,答案就是$\sum_ ...
随机推荐
- Canvas帧数和步长实例
<!DOCTYPE HTML> <html lang="zh-CN"> <head> <meta http-equiv="Con ...
- 【Java】编程
3.Java I/O流输入输出,序列化,NIO,NIO.2 https://www.cnblogs.com/jiangwz/p/9193776.html 4.JAVA调用WCF(转) https:// ...
- sql查询语句常用例子
1.查找与jams在同一个单位的员工姓名.性别.部门和职称:select emp_no, emp_name, dept, title from employee where emp_name< ...
- 来谈谈 WebAssembly 是个啥?为何说它会影响每一个 Web 开发者?
作者:link 原文:What is WebAssembly and why it affects web developers! 你听说过WebAssembly吗?这是由Google, Micros ...
- MySQL批量添加表字段
ALTER TABLE custom ADD contacts2 VARCHAR(50) NOT NULL DEFAULT '' COMMENT '客户联系人2',ADD phone2 VARCHAR ...
- Linux 统计当前目录下文件数
Linux 统计文件数 linux统计当前目录下文件数 ls -l |grep "^-"|wc -l linux统计当前目录下文件(包括子文件夹下的)数 ls -lR|grep & ...
- 领域Command
一.项目结构 二.代码 /// <summary> /// /// </summary> public interface ICommand { } /// <summa ...
- 解决Tomcat 一闪而过的问题
启动tomcat时cmd窗口一闪而过解决方法. 问题现象: 在实际开发中一般都是eclipse+tomcat(也许还会用到tomcat的插件),我们只需要在eclipse中单击servers上的按钮就 ...
- linux运维笔记——curl
** 1.获取网站返回码 ** [root@Cacti ~]# curl -I www.qq.com HTTP/1.1 200 OK Server: squid/3.4.1 Date: Wed, 08 ...
- php-yii-form标签
yii 标签用法(模板) (2013-08-14 17:28:19) 转载▼ 标签: it 分类: yii yii模板中的label标签 <?php echo $form->labelEx ...