类型:概率 + 解方程组(高斯消元法) + KMP(好吧其实我用的是暴力~)
题意:你可以等概率的选择大写字母里的前n个字母,在纸上写啊写,一直到出现给定的字符串。问写的字母个数的期望。
思路:

期望递推法。(不过这里推出了个环……)
下一个状态是看现在这个串,加上一个字母之后,能匹配到原串的哪里。(就是KMP里面的失配数组,写字符串的过程,就是一边写一边匹配)
不过我KMP不太熟悉,就直接暴力了。。
推完后发现,推出了一个环。怎么办,只能用高斯消元法来解这个方程组了。
这题比较特殊,经过证明(我不会= =)可以得到,答案必定为整数。
高斯消元法用double精度卡死(样例都过不了),用分数还是WA(可能溢出了),最后纯用long long 终于过了它。。。

代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std; char str[];
char tmpstr[]; long long matrix[][]; int check() {
int res = ;
for (int i = ; tmpstr[i]; i++) {
bool ok = true;
for (int j = ; tmpstr[j+i]; j++) {
if (str[j] != tmpstr[j+i]) {
ok = false;
break;
}
}
if (ok) {
res = strlen(tmpstr) - i;
break;
}
}
return res;
} bool gauss(int row, int col) {
for (int i = ; i < row; i++) {
int k = -;
for (int j = i; j < row; j++) {
if (matrix[j][i] != ) {
k = j;
break;
}
}
if (k == -) return false;
for (int j = ; j < col; j++) {
swap(matrix[i][j],matrix[k][j]);
}
if (matrix[i][i] < ) {
for (int j = ; j < col; j++) {
matrix[i][j] *= -;
}
}
for (int j = ; j < row; j++) {
if (j == i) continue;
if (matrix[j][i] == ) continue;
if (matrix[j][i] < ) {
for (int k = ; k < col; k++) {
matrix[j][k] *= -;
}
}
long long gcdnum = __gcd(matrix[i][i], matrix[j][i]);
long long lcanum = matrix[i][i]/gcdnum*matrix[j][i];
long long jmul = lcanum / matrix[j][i];
long long imul = lcanum / matrix[i][i];
for (int k = ; k < col; k++) {
matrix[j][k] = matrix[j][k]*jmul - matrix[i][k] * imul;
}
}
}
return true;
} void print(int len) {
puts("--------");
for (int i = ; i < len; i++) {
for (int j = ; j < len+; j++) {
printf("%lld ", matrix[i][j]);
}puts("");
}
} int main() {
int t;
scanf("%d", &t);
for (int cas = ; cas <= t; cas++) {
if (cas != ) puts("");
printf("Case %d:\n", cas); int n;
scanf("%d%s", &n, str);
int len = strlen(str); //计算dp[0]~dp[len-1] len条方程
for (int i = ; i < len; i++) {
for (int j = ; j < len+; j++) matrix[i][j] = ;
matrix[i][i] = -n;
matrix[i][len] = -n;
sprintf(tmpstr, "%s", str);
tmpstr[i+] = ;
for (int j = ; j < n; j++) {
tmpstr[i] = 'A'+j;
if (check() != len) matrix[i][check()]++;
}
}
//print(len);
if (!gauss(len, len+)) puts("ERROR");
//print(len);
//printf("%lld(%lld/%lld)\n", matrix[0][len]/matrix[0][0], matrix[0][len], matrix[0][0]);
printf("%lld\n", matrix[][len]/matrix[][]);
}
return ;
}

ZOJ 2619: Generator的更多相关文章

  1. JavaScript异步编程:Generator与Async

    从Promise开始,JavaScript就在引入新功能,来帮助更简单的方法来处理异步编程,帮助我们远离回调地狱. Promise是下边要讲的Generator/yield与async/await的基 ...

  2. ES6新特性三: Generator(生成器)函数详解

    本文实例讲述了ES6新特性三: Generator(生成器)函数.分享给大家供大家参考,具体如下: 1. 简介 ① 理解:可以把它理解成一个函数的内部状态的遍历器,每调用一次,函数的内部状态发生一次改 ...

  3. ES6 异步编程之一:Generator

    Generator 生成器是es6原生提供的异步编程方案,其语法行为和传统函数完全不同,阮大的<ECMAScript 6 入门>一书中对生成器有比较详尽的介绍,还有一些其他的文章可以参考, ...

  4. ES6 - Note7:Generator函数

    Generator函数 1.Generator函数是ES6增加的异步编程解决方案之一,与普通的函数行为完全不同,类似于一个状态机,内部封装了多个状态. 在函数定义的形式上,跟普通函数差不多,有两处不同 ...

  5. Python:generator的send()方法流程分析

    先来一个简单地例子: def foo(): print('starting') while True: r = yield 2 print(r) f = foo() print(f.send(None ...

  6. C++版 - 剑指Offer 面试题45:圆圈中最后剩下的数字(约瑟夫环问题,ZOJ 1088:System Overload类似)题解

    剑指Offer 面试题45:圆圈中最后剩下的数字(约瑟夫环问题) 原书题目:0, 1, - , n-1 这n个数字排成一个圈圈,从数字0开始每次从圆圏里删除第m个数字.求出这个圈圈里剩下的最后一个数字 ...

  7. django1.11 启动错误:Generator expression must be parenthesized

    错误信息: Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0 ...

  8. ES6入门十一:Generator生成器、async+await、Promisify

    生成器的基本使用 生成器 + Promise async+await Promise化之Promisify工具方法 一.生成器的基本使用 在介绍生成器的使用之前,可以简单理解生成器实质上生成的就是一个 ...

  9. django 启动错误:Generator expression must be parenthesized 错误信息:

    错误为: Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x ...

随机推荐

  1. 【NOIP2017提高组模拟7.3】B

    树上路径统计,点分治解决. 统计一段区间,naive地用了set解决,这样的复杂度是O(nlog^2n)的 考场代码出了个问题,统计答案时找到了之前的最优答案,但是没有加上新的一段,导致60分 #in ...

  2. wdcp 使用说明总结(持续更新中。。。)

    wdcp 使用说明总结(持续更新中...) 1.移动文件时,如果是上一层,直接填写../即可

  3. 采用Atlas+Keepalived实现MySQL读写分离、读负载均衡

    ========================================================================================== 一.基础介绍 == ...

  4. Java-downloadFileByLink

    import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.ByteArrayOutputStr ...

  5. python模块之datetime

    相比于time模块,datetime模块的接口则更直观.更容易调用 datetime模块定义了下面这几个类: datetime.date:表示日期的类.常用的属性有year, month, day: ...

  6. micrium ucprobe使用笔记

    前段时间在学习ucos-iii的时候,用到了micrium ucprobe,发现在调试的时候,很方便,可以直观的看到任务的运行使用情况,全局变量的值变化等,当然详细的可以参考官方文档,也可以参考网上的 ...

  7. Erasing and Winning UVA - 11491 贪心

    题目:题目链接 思路:不难发现,要使整体尽量大,应先满足高位尽量大,按这个思路优先满足高位即可 AC代码: #include <iostream> #include <cstdio& ...

  8. 使用Blend的一些问题和技巧

    WPF开发,界面处理首选Blend,如果你开发了两年WPF都没接触过blend(当然这种几率不高),或者你刚接触WPF,可以考虑使用Blend,这货也算得上一个神器,上手也不难.以下有两位讲得不错,大 ...

  9. Hive学习笔记(二)

    Hive内部表跟外部表之间的区别 创建外部表 先删除上面创建的表,指定location 此时在hdfs根目录下就有一个hivedata文件夹 上传文本数据到hivedata目录下 查询表中数据 删除上 ...

  10. dev c++ 提示没有iostream.h文件

    dev c++ 提示没有iostream.h文件 解决办法路径没有打通最好是这样写:#include <iostream>using namespace std;int main(int ...