题面

bzoj

我要向师父学习善待每一只数据结构

考虑成环,那么高斯消元

然鹅这道题太小了 所以直接转移矩阵自乘就好啦

终点不向外连边 有一条向自己的,概率为一的自环来作为结尾

对于其他店 若有边\((u -> v) = p\) 那么mat[u][v] += p


#include <cmath>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <complex>
#include <ctime>
#include <vector>
#include <queue>
#include <bitset>
#define mp(x, y) make_pair(x, y)
using namespace std;
const int N = 15;
const int M = 105;
int n, m, len, sz, en[N];
double p[N];
struct Matrix{
double w[M][M];
void clear(){
for(int i = 0; i <= sz; ++i)
for(int j = 0; j <= sz; ++j)
w[i][j] = 0;
}
void print(){
printf("--------------------\n");
for(int i = 0; i <= sz; ++i){
for(int j = 0; j <= sz; ++j)
printf("%.2lf ", w[i][j]);
printf("\n");
}
printf("--------------------\n");
}
friend Matrix operator *(const Matrix x, const Matrix y){
Matrix z; z.clear();
for(int i = 0; i <= sz; ++i)
for(int j = 0; j <= sz; ++j)
for(int k = 0; k <= sz; ++k)
z.w[i][j] += x.w[i][k] * y.w[k][j];
// z.print();
return z;
}
}mat, res;
struct AC{
int ch[M][N], f[M];
bool flag[M];
queue<int> que;
void ins(char* str, int id){
int now = 0;
for(int i = 1, cc; i <= len; ++i){
cc = str[i] - 'A';
if(!ch[now][cc]) ch[now][cc] = ++sz;
now = ch[now][cc];
}
en[id] = now, flag[now] = 1;
}
void build(){
int now = 0;
for(int i = 0; i < m; ++i) if(ch[0][i]) que.push(ch[0][i]);
while(!que.empty()){
int fro = que.front(); que.pop();
for(int i = 0; i < m; ++i){
if(ch[fro][i]) f[ch[fro][i]] = ch[f[fro]][i], que.push(ch[fro][i]);//!!
else ch[fro][i] = ch[f[fro]][i];
}
}
mat.clear();
for(int i = 0; i <= sz; ++i){
if(flag[i]){
mat.w[i][i] = 1; continue;
}
for(int j = 0; j < m; ++j){
mat.w[i][ch[i][j]] += p[j];
}
}
}
}ac; int main(){
scanf("%d%d%d", &n, &len, &m);
for(int i = 0; i < m; ++i){
double x, y; scanf("%lf%lf", &x, &y);
p[i] = x / y;
}
char str[N];
for(int i = 1; i <= n; ++i){
scanf("%s", str + 1);
ac.ins(str, i);
}
ac.build();
for(int i = 1; i <= 100; ++i) mat = mat * mat;
//转移矩阵自乘 得到来自0的解
for(int i = 1; i <= n; ++i) printf("%.2lf\n", mat.w[0][en[i]]);
return 0;
}

最新文章

  1. C#中timer类的用法
  2. 跨平台的.NET运行环境 Mono 3.2发布
  3. 从CPU的运行到函数调用做个了解
  4. 怎样在win7系统配置数据源
  5. 理解Linux中断 (2)【转】
  6. 初始JSON
  7. 属性readwrite,readonly,assign,retain,copy,nonatomic
  8. PHP PDO函数库具体解释
  9. PHP字符
  10. mysql dos启动出现1067错误的解决方法
  11. org.springframework.context.event.AbstractApplicationEventMulticaster
  12. iOS 判断奇偶数
  13. 3--OC -- 点语法
  14. [LeetCode] Trapping Rain Water II 题解
  15. 学好js的步骤
  16. Spring 官方教程:使用 Restdocs 创建 API 文档
  17. synchronized与Lock的区别与使用
  18. JDBC ---获取数据字段 -- 转成map
  19. 配置DHCP服务
  20. Spring Boot 入门案例与配置说明

热门文章

  1. 限制TextBox只允许输入数字和字母
  2. dotnet检测类型是否为泛型
  3. acrobat pdf 按页拆分
  4. asp.net Core HttpClient 出现Cannot access a disposed object. Object name: &#39;SocketsHttpHandler&#39; 的问题。
  5. 第三次上机,ADO接口的使用
  6. node配置微信小程序解密消息以及推送消息
  7. Debain/Ubuntu/Deepin 下使用 ss
  8. 第五篇Scrum冲刺博客
  9. 点击 Button触发事件将GridView1 CheckBox勾选的行添加到GridView2中
  10. docker下编译mangoszero WOW60级服务端(三)