题意:给出一个字符集和一个字符串和正整数n,问由给定字符集组成的所有长度为n的串中不以给定字符串为连续子串的有多少个?

析:n 实在是太大了,如果小的话,就可以用动态规划做了,所以只能用矩阵快速幂来做了,dp[i][j] 表示匹配完 i 到匹配 j 个有多少种方案,利用矩阵的性质,就可以快速求出长度为 n 的个数,对于匹配的转移,正好可以用KMP的失配函数来转移。

代码如下:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <cctype>
  14. #include <cmath>
  15. #include <stack>
  16. #include <sstream>
  17. #define debug() puts("++++");
  18. #define gcd(a, b) __gcd(a, b)
  19. #define lson l,m,rt<<1
  20. #define rson m+1,r,rt<<1|1
  21. #define freopenr freopen("in.txt", "r", stdin)
  22. #define freopenw freopen("out.txt", "w", stdout)
  23. using namespace std;
  24.  
  25. typedef long long LL;
  26. typedef unsigned long long ULL;
  27. typedef pair<int, int> P;
  28. const int INF = 0x3f3f3f3f;
  29. const LL LNF = 1e16;
  30. const double inf = 0x3f3f3f3f3f3f;
  31. const double PI = acos(-1.0);
  32. const double eps = 1e-6;
  33. const int maxn = 50 + 10;
  34. const int mod = 1e9 + 7;
  35. const int dr[] = {-1, 0, 1, 0};
  36. const int dc[] = {0, 1, 0, -1};
  37. const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
  38. int n, m;
  39. const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  40. const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  41. inline bool is_in(int r, int c){
  42. return r > 0 && r <= n && c > 0 && c <= m;
  43. }
  44.  
  45. char s[maxn], t[maxn];
  46. int f[maxn];
  47.  
  48. struct Matrix{
  49. unsigned int a[maxn][maxn];
  50. int n;
  51. Matrix(int nn) : n(nn){
  52. memset(a, 0, sizeof a);
  53. }
  54.  
  55. void toOne(){
  56. for(int i = 0; i < n; ++i)
  57. a[i][i] = 1;
  58. }
  59.  
  60. friend Matrix operator * (const Matrix &lhs, const Matrix &rhs){
  61. Matrix res(lhs.n);
  62. for(int i = 0; i < lhs.n; ++i)
  63. for(int j = 0; j < rhs.n; ++j)
  64. for(int k = 0; k < lhs.n; ++k)
  65. res.a[i][j] += lhs.a[i][k] * rhs.a[k][j];
  66. return res;
  67. }
  68. };
  69.  
  70. Matrix fast_pow(Matrix a, int n){
  71. Matrix res(a.n);
  72. res.toOne();
  73. while(n){
  74. if(n&1) res = res * a;
  75. a = a * a;
  76. n >>= 1;
  77. }
  78. return res;
  79. }
  80.  
  81. void getFail(char *s, int n){
  82. f[0] = f[1] = 0;
  83. for(int i = 1; i < n; ++i){
  84. int j = f[i];
  85. while(j && s[j] != s[i]) j = f[j];
  86. f[i+1] = s[i] == s[j] ? j+1 : 0;
  87. }
  88. }
  89.  
  90. int main(){
  91. int T; cin >> T;
  92. for(int kase = 1; kase <= T; ++kase){
  93. scanf("%d", &n);
  94. scanf("%s %s", t, s);
  95. int lens = strlen(s);
  96. getFail(s, lens);
  97. Matrix ans(lens);
  98. for(int i = 0; i < lens; ++i)
  99. for(int k = 0; t[k]; ++k){
  100. int j = i;
  101. while(j && s[j] != t[k]) j = f[j];
  102. if(s[j] == t[k]) ++j;
  103. ++ans.a[i][j];
  104. }
  105. ans = fast_pow(ans, n);
  106. unsigned int res = 0;
  107. for(int i = 0; i < lens; ++i) res += ans.a[0][i];
  108. printf("Case %d: %u\n", kase, res);
  109. }
  110. return 0;
  111. }

  

LightOJ 1268 Unlucky Strings (KMP+矩阵快速幂)的更多相关文章

  1. [bzoj1009](HNOI2008)GT考试 (kmp+矩阵快速幂加速递推)

    Description 阿 申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0<=Xi<=9),他不希望准考证号上出现不吉利的数字.他的不吉利数学 A1A2...Am(0&l ...

  2. LightOJ 1268 Unlucky Strings(KMP+矩阵乘法+基础DP)

    题意 给出字符串的长度 \(n\) ,以及该字符串是由哪些小写字母组成,现给出一个坏串 \(S\) ,求存在多少种不同的字符串,使得其子串不含坏串. \(1 \leq n \leq 10^9\) \( ...

  3. LightOj 1065 - Number Sequence (矩阵快速幂,简单)

    题目 和 LightOj 1096 - nth Term 差不多的题目和解法,这道相对更简单些,万幸,这道比赛时没把模版给抽风坏. #include<stdio.h> #include&l ...

  4. 2018.10.22 bzoj1009: [HNOI2008]GT考试(kmp+矩阵快速幂优化dp)

    传送门 f[i][j]f[i][j]f[i][j]表示从状态"匹配了前i位"转移到"匹配了前j位"的方案数. 这个东西单次是可以通过跳kmp的fail数组得到的 ...

  5. BZOJ 1009 [HNOI2008]GT考试 (KMP + 矩阵快速幂)

    1009: [HNOI2008]GT考试 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 4266  Solved: 2616[Submit][Statu ...

  6. LightOJ 1070 Algebraic Problem:矩阵快速幂 + 数学推导

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1070 题意: 给你a+b和ab的值,给定一个n,让你求a^n + b^n的值(MOD ...

  7. bzoj1009 [HNOI2008]GT考试——KMP+矩阵快速幂优化DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1009 字符串计数DP问题啊...连题解都看了好多好久才明白,别提自己想出来的蒟蒻我... 首 ...

  8. LightOj 1096 - nth Term (矩阵快速幂,简单)

    题目 这道题是很简单的矩阵快速幂,可惜,在队内比赛时我不知什么时候抽风把模版中二分时判断的 ==1改成了==0 ,明明觉得自己想得没错,却一直过不了案例,唉,苦逼的比赛状态真让人抓狂!!! #incl ...

  9. [HNOI2008][bzoj1009] GT考试 [KMP+矩阵快速幂]

    题面 传送门 思路 首先,如果$n$和$m$没有那么大的话,有一个非常显然的dp做法: 设$dp[i][j]$表示长度为i的字符串,最后j个可以匹配模板串前j位的情况数 那么显然,答案就是$\sum_ ...

随机推荐

  1. 运维平台cmdb开发-day3

    后台管理 1. 访问过程 我们在前面2天得到了各个客户端的资产数据信息,我们将各个这个数据打包发送到api,这个api其实就是django的url传参,之后按照数据的格式对比数据库增删改 2. 后台页 ...

  2. DjangoORM执行原生sql

    在Django中使用原生Sql主要有以下几种方式: 一:extra:结果集修改器,一种提供额外查询参数的机制 二:raw:执行原始sql并返回模型实例 三:直接执行自定义Sql   这种方式完全不依赖 ...

  3. [转] geochart 地图控件官方示例

    <html> <head> <script type='text/javascript' src='https://www.google.com/jsapi'>&l ...

  4. Tkinter Colors(颜色)

          Tkinter Colors: Tkinter的代表与字符串的颜色.一般有两种方式来指定Tkinter的颜色 Tkinter的代表与字符串的颜色.一般有两种方式来指定Tkinter的颜色: ...

  5. Tkinter LabelFrame

       Tkinter LabelFrame: 在一个labelframe一个简单的容器构件.其主要目的是作为一个间隔或复杂的窗口布局容器. 在一个labelframe一个简单的容器构件.其主要目的是作 ...

  6. 核主成分分析(Kernel Principal Component Analysis, KPCA)的公式推导过程

    KPCA,中文名称”核主成分分析“,是对PCA算法的非线性扩展,言外之意,PCA是线性的,其对于非线性数据往往显得无能为力,例如,不同人之间的人脸图像,肯定存在非线性关系,自己做的基于ORL数据集的实 ...

  7. Django 的分页器

    from  不能发json格式   只能发四种格式 view from django.shortcuts import render,HttpResponse # Create your views ...

  8. Django的contenttypes应用、缓存相关

    一.django的contenttypes contenttypes 是Django内置的一个应用 , 可以追踪项目中所有app 和 model 的对应关系, 并记录djang_content_typ ...

  9. 「小程序JAVA实战」小程序通用模板的使用(17)

    转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-17/ 小程序也为了页面增加了通用模板的功能,如何去理解一个通用的模板呢?模板的定义就是为了让我们的 ...

  10. Spring boot + Gradle + Eclipse打war包发布总结

    首先感谢两位博主的分享 http://lib.csdn.net/article/git/55444?knId=767 https://my.oschina.net/alexnine/blog/5406 ...