恶补了一下AC自动机,花了一天时间终于全部搞明白了。

思路:将每个人的串加入AC自动机,在AC自动机生成的状态图上建边,注意单词末尾的节点只能转移到自己概率为1,

然后将矩阵自乘几十次后误差就很小了, 或者可以高斯消元搞出精确解。

#include<bits/stdc++.h>
#define LL long long
#define ll long long
#define fi first
#define se second
#define mk make_pair
#define pii pair<int, int>
#define y1 skldjfskldjg
#define y2 skldfjsklejg using namespace std; const int N = + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ; int n, l, m, pos[];
double pro[];
char s[]; struct Matrix {
int r, c;
double a[][];
Matrix(int r = , int c = ) {
this->r = r;
this->c = c;
memset(a, , sizeof(a));
} Matrix operator * (const Matrix &B) const {
Matrix C(r, c);
for(int i = ; i < r; i++)
for(int j = ; j < c; j++)
for(int k = ; k < r; k++)
C.a[i][j] += a[i][k] * B.a[k][j];
return C;
}
}; struct Ac {
int val[N], ch[N][], f[N], last[N], cnt, SZ; void init(int SZ = ) {
cnt = ; this->SZ = SZ;
for(int c = ; c < SZ; c++) ch[][c] = ;
} int getId(char c) {
return c - 'A';
} int newNode() {
cnt++;
memset(ch[cnt], , sizeof(ch[cnt]));
val[cnt] = f[cnt] = last[cnt] = ;
return cnt;
} void add(char *s, int &pos) {
int u = ;
for(int i = ; s[i]; i++) {
int c = getId(s[i]);
if(!ch[u][c]) ch[u][c] = newNode();
u = ch[u][c];
}
val[u]++;
pos = u;
} void build() {
queue<int> que;
f[] = ;
for(int c = ; c < SZ; c++) {
if(!ch[][c]) continue;
f[ch[][c]] = last[ch[][c]] = ;
que.push(ch[][c]);
}
while(!que.empty()) {
int u = que.front(); que.pop();
for(int c = ; c < SZ; c++) {
int v = ch[u][c];
if(!v) {
ch[u][c] = ch[f[u]][c];
continue;
} else {
que.push(v);
f[v] = ch[f[u]][c];
last[v] = val[f[v]] ? f[v] : last[f[v]];
}
}
}
} void buildMatrix(Matrix &A) {
for(int u = ; u <= cnt; u++) {
if(val[u]) A.a[u][u] = ;
else {
for(int c = ; c < m; c++) {
int v = ch[u][c];
A.a[u][v] += pro[c];
}
}
}
}
} ac; int main() {
scanf("%d%d%d", &n, &l, &m);
for(int i = ; i < m; i++) {
double p, q;
scanf("%lf%lf", &p, &q);
pro[i] = p / q;
} ac.init(m); for(int i = ; i <= n; i++) {
scanf("%s", s);
ac.add(s, pos[i]);
} ac.build();
Matrix A(ac.cnt + , ac.cnt + );
ac.buildMatrix(A); for(int i = ; i <= ; i++)
A = A * A; for(int i = ; i <= n; i++) printf("%.2f\n", A.a[][pos[i]]);
return ;
} /*
*/

bzoj 1444 AC自动机 + 矩阵乘法 | 高斯消元的更多相关文章

  1. hdu5955 Guessing the Dice Roll【AC自动机】【高斯消元】【概率】

    含高斯消元模板 2016沈阳区域赛http://acm.hdu.edu.cn/showproblem.php?pid=5955 Guessing the Dice Roll Time Limit: 2 ...

  2. 4.23 子串 AC自动机 概率期望 高斯消元

    考虑40分. 设出状态 f[i]表示匹配到了i位还有多少期望长度能停止.可以发现这个状态有环 需要高斯消元. 提供一种比较简单的方法:由于期望的线性可加性 可以设状态f[i]表示由匹配到i到匹配到i+ ...

  3. 2016ACM/ICPC亚洲区沈阳站H - Guessing the Dice Roll HDU - 5955 ac自动机+概率dp+高斯消元

    http://acm.hdu.edu.cn/showproblem.php?pid=5955 题意:给你长度为l的n组数,每个数1-6,每次扔色子,问你每个串第一次被匹配的概率是多少 题解:先建成ac ...

  4. 【BZOJ4820】[Sdoi2017]硬币游戏 AC自动机+概率DP+高斯消元

    [BZOJ4820][Sdoi2017]硬币游戏 Description 周末同学们非常无聊,有人提议,咱们扔硬币玩吧,谁扔的硬币正面次数多谁胜利.大家纷纷觉得这个游戏非常符合同学们的特色,但只是扔硬 ...

  5. bzoj 3503: [Cqoi2014]和谐矩阵【高斯消元】

    如果确定了第一行,那么可以推出来整个矩阵,矩阵合法的条件是n+1行全是0 所以推出来n+1行和1行的关系,然后用异或高斯消元来解即可 #include<iostream> #include ...

  6. 【AC自动机】【高斯消元】hdu5955 Guessing the Dice Roll

    http://blog.csdn.net/viphong/article/details/53098489 我有一点不是很懂,这样算出来转移到AC自动机根节点的概率是一个远大于1的数. 按我的理解,因 ...

  7. BZOJ.4820.[SDOI2017]硬币游戏(思路 高斯消元 哈希/AC自动机/KMP)

    BZOJ 洛谷 建出AC自动机,每个点向两个儿子连边,可以得到一张有向图.参照 [SDOI2012]走迷宫 可以得到一个\(Tarjan\)+高斯消元的\(O((nm)^3)\)的做法.(理论有\(6 ...

  8. 【bzoj1444】[Jsoi2009]有趣的游戏 AC自动机+矩阵乘法

    题目描述 输入 注意 是0<=P 输出 样例输入 样例输出 题解 AC自动机+矩阵乘法 先将所有字符串放到AC自动机中,求出Trie图. 然后构建邻接矩阵:如果x不是某个字符串的末位置,则x连向 ...

  9. BZOJ_3503_[Cqoi2014]和谐矩阵_高斯消元

    BZOJ_3503_[Cqoi2014]和谐矩阵_高斯消元 题意: 我们称一个由0和1组成的矩阵是和谐的,当且仅当每个元素都有偶数个相邻的1.一个元素相邻的元素包括它本身,及他上下左右的4个元素(如果 ...

随机推荐

  1. HDU1914 稳定婚姻匹配

    The Stable Marriage Problem Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (J ...

  2. POJ 3087 Shuffle'm Up DFS

    link:http://poj.org/problem?id=3087 题意:给你两串字串(必定偶数长),按照扑克牌那样的洗法(每次从S2堆底中拿第一张,再从S1堆底拿一张放在上面),洗好后的一堆可以 ...

  3. HDU 1874 SPFA/Dijkstra/Floyd

    这题作为模板题,解法好多... 最近周围的人都在搞图论阿,感觉我好辣鸡,只会跟风学习. 暂时只有SPFA和Dijkstra的 SPFA (邻接表版.也可以写成临接矩阵存图,但题目可能给出平行边的,所以 ...

  4. Mybatis xml 写sql如何判断集合的size

    在mybtis的映射文件中判断集合大小  list.size  例子如下: <if test="groupIds != null and groupIds.size>0" ...

  5. 归并排序Merge sort2

    原理,把原始数组分成若干子数组,对每一个子数组进行排序, 继续把子数组与子数组合并,合并后仍然有序,直到全部合并完,形成有序的数组 举例 无序数组[6 2 4 1 5 9] 先看一下每个步骤下的状态, ...

  6. Spring Boot 启动报错:LoggingFailureAnalysisReporter

    17:57:19: Executing task 'bootRun'... Parallel execution with configuration on demand is an incubati ...

  7. html+js实现的触屏版贪吃蛇

    查看线上demo(服务器经常断开,推荐下载源码本地打开): http://47.93.103.19:8044/client/ ; 使用手机打开或者chrome浏览器的手机模式打开 源码地址 :http ...

  8. 科猫网项目总结(基于SSM框架)

    1.配置文件的添加 SSM整合需要web.xml配置文件,springmvc的配置文件,spring和mybatis整合的配置文件. 1.web.xml文件的配置 1.在WEB-INF下新建web.x ...

  9. Python switch-case语句的实现 -- 字典模拟实现

    static void print_asru_status(int status, char *label) { char *msg = NULL; switch (status) { : msg = ...

  10. Java多线程学习(三)volatile关键字

    转载请备注地址:https://blog.csdn.net/qq_34337272/article/details/79680693 系列文章传送门: Java多线程学习(一)Java多线程入门 Ja ...