题意:随机字母组成一个串,有一个目标串,当这个由随机字母组成的串出现目标串就停止,求这个随机字母组成串的期望长度。

析:由于只要包含目标串就可以停止,所以可以先把这个串进行处理,也就是KMP,然后dp[i] 表示从 i 结点到完全匹配期望长度,所以很容易得到状态转移方程 dp[i] = ∑dp[j] / n + 1,然后用高斯消元即可,要注意,要用全整数的高斯消元。

代码如下:

#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>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#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 fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
//#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#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 = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 150000 + 10;
const int maxm = 3e5 + 10;
const int mod = 10007;
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[20];
int f[maxn];
LL A[20][20]; void getFail(int n){
f[0] = f[1] = 0;
for(int i = 1; i < n; ++i){
int j = f[i];
while(j && s[i] != s[j]) j = f[j];
f[i+1] = s[i] == s[j] ? j+1 : 0;
}
} void Gauess(int n){
for(int i = 0; i < n; ++i){
int r = i;
while(r < n && !A[r][i]) ++r;
if(r != i) for(int j = 0; j <= n; ++j) swap(A[r][j], A[i][j]); for(int k = i+1; k < n; ++k) if(A[k][i]){
LL f = A[k][i];
for(int j = i; j <= n; ++j) A[k][j] = A[k][j] * A[i][i] - f * A[i][j];
}
}
for(int i = n-1; i >= 0; --i){
for(int j = i+1; j < n; ++j)
A[i][n] -= A[j][n] * A[i][j];
A[i][n] /= A[i][i];
}
} int main(){
int T; cin >> T;
for(int kase = 1; kase <= T; ++kase){
scanf("%d %s", &n, s);
m = strlen(s);
getFail(m);
ms(A, 0);
for(int i = 0; i < m; ++i){
A[i][i] += n;
A[i][m+1] += n;
for(int k = 0; k < n; ++k){
int j = i;
while(j && s[j] != 'A' + k) j = f[j];
if(s[j] == 'A' + k) ++j;
--A[i][j];
}
}
A[m][m] = 1;
Gauess(m + 1);
printf("Case %d:\n", kase);
printf("%lld\n", A[0][m+1]);
if(kase != T) puts("");
}
return 0;
}

  

UVaLive 3490 Generator (KMP + DP + Gauss)的更多相关文章

  1. UVALive - 3490 Generator (AC自动机+高斯消元dp)

    初始有一个空串s,从前n个大写字母中不断随机取出一个字母添加到s的结尾,出现模式串t时停止,求停止时s的长度期望. 这道题解法不唯一,比较无脑的方法是对模式串t建一个单串AC自动机,设u为自动机上的一 ...

  2. [HDOJ5763]Another Meaning(KMP, DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5763 题意:给定两个字符串a和b,其中a中的字符串如果含有子串b,那么那部分可以被替换成*.问有多少种 ...

  3. POJ 3336 Count the string (KMP+DP,好题)

    参考连接: KMP+DP: http://www.cnblogs.com/yuelingzhi/archive/2011/08/03/2126346.html 另外给出一个没用dp做的:http:// ...

  4. 【KMP+DP】Count the string

    KMP算法的综合练习 DP很久没写搞了半天才明白.本题结合Next[]的意义以及动态规划考察对KMP算法的掌握. Problem Description It is well known that A ...

  5. codeforces432D Prefixes and Suffixes(kmp+dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud D. Prefixes and Suffixes You have a strin ...

  6. [kmp+dp] hdu 4628 Pieces

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4622 Reincarnation Time Limit: 6000/3000 MS (Java/Ot ...

  7. 洛谷P3193 [HNOI2008]GT考试 kmp+dp

    正解:kmp+dp+矩阵优化 解题报告: 传送门! 啊刚说想做矩阵优化dp的字符串题就找到辣QwQ虽然不是AC自动机的但都差不多嘛QwQ 首先显然可以想到一个dp式?就f[i][j]:凑出i位了,在s ...

  8. [BZOJ1009] [HNOI2008] GT考试(KMP+dp+矩阵快速幂)

    [BZOJ1009] [HNOI2008] GT考试(KMP+dp+矩阵快速幂) 题面 阿申准备报名参加GT考试,准考证号为N位数X1X2-.Xn,他不希望准考证号上出现不吉利的数字.他的不吉利数学A ...

  9. HDU 6153 A Secret ( KMP&&DP || 拓展KMP )

    题意 : 给出两个字符串,现在需要求一个和sum,考虑第二个字符串的所有后缀,每个后缀对于这个sum的贡献是这个后缀在第一个字符串出现的次数*后缀的长度,最后输出的答案应当是 sum % 1e9+7 ...

随机推荐

  1. java爬取网页Unicode转UTF-8中文

    unicode编码简而言之就是将每一个字符用16位2进制数标识.但是通常都用4位的16进制数标识. 例如: 1)中文字符串"你好"的unicode码为:\u60\u597d; 2) ...

  2. android的四种线程池

    使用线程池的好处: 首先通过线程池中线程的重用,减少创建和销毁线程的性能开销.其次,能控制线程池中的并发数,否则会因为大量的线程争夺CPU资源造成阻塞.最后,线程池能够对线程进行管理,比如使用Sche ...

  3. node.js中npm包管理工具

    现在安装node.js,默认就会帮我们装上了npm包管理工具,npm主要用来下载,安装,管理第三方模块. 创建一个包描述文件: npm init [-y] 查看包的信息 npm info <pa ...

  4. YII2中使用控制台命令

    有些时候我们需要通过crontab在后台跑一些定时脚本,这时候就需要用到控制台命令了. 我们在commands目录下创建TestController.php,当然脚本的位置是可以随意指定的,只需要在c ...

  5. golang基础学习

    一.输出hello,world程序 package main; import "fmt"; func main() { fmt.Printf("hello,world&q ...

  6. Repeater控件添加序号列

    在项目开发过程中,会经常遇到ASP.NET repeater控件添加序号列,有些新手可能还不会,网上搜集整理了一些,需要的朋友可以参考下 ASP.NET repeater添加序号列的方法 1.< ...

  7. c#devexpress GridContorl添加进度条

    demo 的实现图 下边是步骤和代码 1定义 时钟事件,定时的增加进度条的增量. 2:  添加进度条 3;定义字段属性 using System; using System.Collections.G ...

  8. [转载]linux awk命令详解

    简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再 ...

  9. 应用内直接跳转到Appstore

    iOS开发中,应用内直接跳转到Appstore .进入appstore中指定的应用 NSString *str = [NSString stringWithFormat: @"itms:// ...

  10. node.js 关于 async的使用

    第一次使用,感觉有点糊涂,后来实验明白了. 在串行执行中,经常会只做了第一步.后来明白了.是没有把回调函数放在里面简单就是:  async.series(                 {     ...