UVaLive 3490 Generator (KMP + DP + Gauss)
题意:随机字母组成一个串,有一个目标串,当这个由随机字母组成的串出现目标串就停止,求这个随机字母组成串的期望长度。
析:由于只要包含目标串就可以停止,所以可以先把这个串进行处理,也就是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)的更多相关文章
- UVALive - 3490 Generator (AC自动机+高斯消元dp)
初始有一个空串s,从前n个大写字母中不断随机取出一个字母添加到s的结尾,出现模式串t时停止,求停止时s的长度期望. 这道题解法不唯一,比较无脑的方法是对模式串t建一个单串AC自动机,设u为自动机上的一 ...
- [HDOJ5763]Another Meaning(KMP, DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5763 题意:给定两个字符串a和b,其中a中的字符串如果含有子串b,那么那部分可以被替换成*.问有多少种 ...
- POJ 3336 Count the string (KMP+DP,好题)
参考连接: KMP+DP: http://www.cnblogs.com/yuelingzhi/archive/2011/08/03/2126346.html 另外给出一个没用dp做的:http:// ...
- 【KMP+DP】Count the string
KMP算法的综合练习 DP很久没写搞了半天才明白.本题结合Next[]的意义以及动态规划考察对KMP算法的掌握. Problem Description It is well known that A ...
- codeforces432D Prefixes and Suffixes(kmp+dp)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud D. Prefixes and Suffixes You have a strin ...
- [kmp+dp] hdu 4628 Pieces
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4622 Reincarnation Time Limit: 6000/3000 MS (Java/Ot ...
- 洛谷P3193 [HNOI2008]GT考试 kmp+dp
正解:kmp+dp+矩阵优化 解题报告: 传送门! 啊刚说想做矩阵优化dp的字符串题就找到辣QwQ虽然不是AC自动机的但都差不多嘛QwQ 首先显然可以想到一个dp式?就f[i][j]:凑出i位了,在s ...
- [BZOJ1009] [HNOI2008] GT考试(KMP+dp+矩阵快速幂)
[BZOJ1009] [HNOI2008] GT考试(KMP+dp+矩阵快速幂) 题面 阿申准备报名参加GT考试,准考证号为N位数X1X2-.Xn,他不希望准考证号上出现不吉利的数字.他的不吉利数学A ...
- HDU 6153 A Secret ( KMP&&DP || 拓展KMP )
题意 : 给出两个字符串,现在需要求一个和sum,考虑第二个字符串的所有后缀,每个后缀对于这个sum的贡献是这个后缀在第一个字符串出现的次数*后缀的长度,最后输出的答案应当是 sum % 1e9+7 ...
随机推荐
- mysql定时删除6个月前的表
查看定时是否开启: 查看event是否开启 : SHOW VARIABLES LIKE '%event_sche%'; 将事件计划开启 : ; 将事件计划关闭 : ; 代码: BEGIN -- 保存表 ...
- iPhone X系列 的获取 - 安全区顶部和底部高度
///1. 获得当前窗口 var JY_WINDOW: UIWindow? { get{ if let app = UIApplication.shared.delegate as? AppDeleg ...
- linux命令学习之:ps
Linux中的ps命令是Process Status的缩写.ps命令用于报告当前系统的进程状态,列出系统中当前运行的那些进程.可以搭配kill指令随时中断.删除不必要的程序. 要对进程进行监测和控制, ...
- vue2.0细节剖析
1.样式切换 单个切换样式 /*html部分*/ <div class="bg" v-bind:class="{active:isActive}"> ...
- 转载:MongoDB之旅(超赞,适合初学者)
MongoDB是目前工作中经常使用到的NoSQL数据库. 本博客只记录相关理论知识和技巧,涉及到实践的部分都会单开Blog来记录实践过程. ------------------------------ ...
- (转)在WCF服务的ServiceReferences.ClientConfig中使用相对路径
问题: Silverlight项目中添加服务引用后会在Silverlight项目中生成一个ServiceReferences.ClientConfig文件,这个文件中包含了引用服务的绑定(bindin ...
- 开源项目CircleImageView
1.在自己MainActivity所在包下创建CircleImageView.class文件 package 自己包名; import android.content.Context; import ...
- 20172306《java程序设计与数据结构》第六周学习总结
20172306<Java程序设计>第六周学习总结 教材学习内容总结 第八章关键学习了数组的相关内容.我觉得主要分一下几点: 1.索引是从0开始,要区分好索引值和个数值.0的索引处是第一个 ...
- IOS初级:NSTimer
@property (nonatomic, strong) NSTimer *timer; 添加定时器 self.timer = [NSTimer scheduledTimerWithTimeInte ...
- js中将斜杠\替换的方法
js中将/替换的方法replace(/\//g, '-') 中间涉及到js的一些转义问题,试了几个方法,发现这个可以,就记下来.