题意:给出一个键盘,按键都是大写字母。给出一个目标单词和一个长度L。最大值或者最大长度都是100。现在随机按键盘,每个按键的概率相同。

敲击出一个长度为L的序列。求该序列中目标单词最多可能出现几次,期望出现几次。输出两者的差。

分析:概率题

先求最大次数。直接看该目标单词首尾最大重叠多长,暴力求解即可。然后通过除法运算求最多出现几次。

求期望涉及到一个Linearity of Expecation的知识,用中文形象的描述可以称之为“期望重组”。

期望重组

现有随机变量X,传统求X期望的方法是把X的每个取值乘以其概率再加和。

而现在我们要对X的每个取值进行重组。

例如,E(X)=sigma(xi*pi)。当X=xi时,我们把X看作是n个随机变量的和。pi是恰好和为xi时的概率。

这想当与是按照X的每种取值进行分类计算。

现在我们给出另外一种求法。

设这n个随机变量总共有M种不同的取值方法。

(如果这些随机变量相互独立,那么M=m1*m2*...*mn,mi表示第i个随机变量有多少种取值。)

我们对于每一个随机变量ai都把M种情况枚举一次,计算每种情况发生的概率乘以ai在该种情况下的取值,并加和。

最后把所有随机变量的加和再加和,就是我们要求的E(X)。

详细说明请google搜索linearity of expectation。

根据期望重组我们可以轻松求出单词出现的次数,我们先求出在总长度的每个位置出现目标单词的期望。当然出现次数只能是0或1。

再乘以可能出现位置的总数即可。

这其实相当于对每个可能出现单词的位置都枚举了整个长度L串的所有情况。

但因为对于一个位置,很多位的取值并不会影响该位置是否会出现目标单词,所以也不用进行分类直接把那些位的所有情况看成一个整体,概率为1。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; #define D(x) const int MAX_N = ;
const int MAX_KEY = ; int key_len, word_len, tot_len;
char keyboard[MAX_N], word[MAX_N];
int num[MAX_N]; void input()
{
scanf("%d%d%d", &key_len, &word_len, &tot_len);
scanf("%s%s", keyboard, word);
} bool ok(int a)
{
for (int i = a; i < word_len; i++)
if (word[i] != word[i - a])
return false;
return true;
} int get_max_time(int overlap)
{
for (int i = ; i < word_len; i++)
if (num[word[i] - 'A'] == )
return ;
if (tot_len < word_len)
return ;
return + (tot_len - word_len) / overlap;
} void work()
{
fill_n(num, , );
for (int i = ; i < key_len; i++)
num[keyboard[i] - 'A']++; int max_time = ;
int overlap = word_len;
for (int i = ; i < word_len; i++)
{
if (ok(i))
{
overlap = i;
break;
}
}
max_time = get_max_time(overlap); double ans = ;
for (int i = ; i < word_len; i++)
ans *= num[word[i] - 'A'] * 1.0 / key_len;
ans *= tot_len - word_len + ;
D(printf("%.3f\n", ans));
D(printf("%d\n", max_time));
printf("%.8f\n", max_time - ans);
} int main()
{
int t;
scanf("%d", &t);
int case_num = ;
while (t--)
{
case_num++;
printf("Case #%d: ", case_num);
input();
work();
}
return ;
}

Google Code Jam 2015 R1C B的更多相关文章

  1. Google Code Jam 2015 R2 C

    题意:给出若干个句子,每个句子包含多个单词.确定第一句是英文,第二句是法文.后面的句子两者都有可能.两个语种会有重复单词. 现在要找出一种分配方法(给每个句子指定其文种),使得既是英文也是法文的单词数 ...

  2. Google Code Jam 2015 Round1A 题解

    快一年没有做题了, 今天跟了一下 GCJ Round 1A的题目, 感觉难度偏简单了, 很快搞定了第一题, 第二题二分稍微考了一下, 还剩下一个多小时, 没仔细想第三题, 以为 前两个题目差不多可以晋 ...

  3. [C++]Infinite House of Pancakes——Google Code Jam 2015 Qualification Round

    Problem It’s opening night at the opera, and your friend is the prima donna (the lead female singer) ...

  4. [C++]Standing Ovation——Google Code Jam 2015 Qualification Round

    Problem It’s opening night at the opera, and your friend is the prima donna (the lead female singer) ...

  5. [Google Code Jam (Qualification Round 2014) ] B. Cookie Clicker Alpha

    Problem B. Cookie Clicker Alpha   Introduction Cookie Clicker is a Javascript game by Orteil, where ...

  6. [Google Code Jam (Qualification Round 2014) ] A. Magic Trick

    Problem A. Magic Trick Small input6 points You have solved this input set.   Note: To advance to the ...

  7. [C++]Store Credit——Google Code Jam Qualification Round Africa 2010

    Google Code Jam Qualification Round Africa 2010 的第一题,很简单. Problem You receive a credit C at a local ...

  8. [C++]Saving the Universe——Google Code Jam Qualification Round 2008

    Google Code Jam 2008 资格赛的第一题:Saving the Universe. 问题描述如下: Problem The urban legend goes that if you ...

  9. Google Code Jam Africa 2010 Qualification Round Problem B. Reverse Words

    Google Code Jam Africa 2010 Qualification Round Problem B. Reverse Words https://code.google.com/cod ...

随机推荐

  1. git alias和gitconfig配置

    [alias] st = status -sb co = checkout br = branch mg = merge ci = commit ds = diff --staged dt = dif ...

  2. ASP.NET MVC使用Bootstrap系列(3)——使用Bootstrap 组件

    阅读目录 Bootstrap 导航条 列表组 徽章 媒体对象 页头 路径导航 分页 输入框组 按钮式下拉菜单 警告框 进度条 小结 Bootstrap为我们提供了十几种的可复用组件,包括字体图标.下拉 ...

  3. CentOS7挂载分区教程

    http://www.centoscn.com/CentOS/config/2014/1016/3955.html

  4. Java8初体验(一)lambda表达式语法

    感谢同事[天锦]的投稿.投稿请联系 tengfei@ifeve.com 本文主要记录自己学习Java8的历程,方便大家一起探讨和自己的备忘.因为本人也是刚刚开始学习Java8,所以文中肯定有错误和理解 ...

  5. ajax实例详解(2)

    说到ajax当然离不开json格式了.在ajax的数据传递过程中用到最多的便是json格式了吧. 什么是json格式,在刚开始用它的时候,着实让人迷茫了一阵子呀.说的白了点所谓json不就是一种数据的 ...

  6. CF449B Jzzhu and Cities (最短路)

    CF449B CF450D http://codeforces.com/contest/450/problem/D http://codeforces.com/contest/449/problem/ ...

  7. POJ 3252 Round Numbers

     组合数学...(每做一题都是这么艰难) Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7607 A ...

  8. Mac安装OpenCV

    安装过程参考这篇文章Mac平台上OpenCV开发环境搭建 也可以参考文档官网上的安装文档Installation in Linux(不知道为什么没有Installation in Mac...) 我的 ...

  9. 类加载器ClassLoader之jar包隔离

    小引子 最近做了一个根据同一模块的不同jar版本做同时测试的工具,感觉挺有意思,特此记录. 类加载器(ClassLoader)是啥? 把类加载阶段中的"通过一个类的全限定名(博主注:绝对路径 ...

  10. BZOJ3223——Tyvj 1729 文艺平衡树

    1.题目大意:维护序列,只有区间翻转这个操作 2.分析:splay的经典操作就是实现区间翻转,就是在splay中有一个标记,表示这个区间被翻转了 然后就是记得各种的操作访问某个点时,记得下传,顺便交换 ...