Google Code Jam 2015 R1C B
题意:给出一个键盘,按键都是大写字母。给出一个目标单词和一个长度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的更多相关文章
- Google Code Jam 2015 R2 C
题意:给出若干个句子,每个句子包含多个单词.确定第一句是英文,第二句是法文.后面的句子两者都有可能.两个语种会有重复单词. 现在要找出一种分配方法(给每个句子指定其文种),使得既是英文也是法文的单词数 ...
- Google Code Jam 2015 Round1A 题解
快一年没有做题了, 今天跟了一下 GCJ Round 1A的题目, 感觉难度偏简单了, 很快搞定了第一题, 第二题二分稍微考了一下, 还剩下一个多小时, 没仔细想第三题, 以为 前两个题目差不多可以晋 ...
- [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) ...
- [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) ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- 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 ...
随机推荐
- HBase命令(三) -- 增删改查
新增 //语法:put <table>,<rowkey>,<family:column>,<value>,<timestamp> //新增或 ...
- jq 构造函数,然后再表单提交过程中对数据进行修改
先贴代码 <script type="text/javascript"> function appendText(){ var content = $("#t ...
- smarty string_format用法 取小数点后2位
<{if $d.ul_pv}> <{$d.sum/$d.ul_pv|string_format:'%.2f'}> <{else}> 0.00 <{/if}&g ...
- [译]angularjs directive design made easy
原文: http://seanhess.github.io/2013/10/14/angularjs-directive-design.html AngularJS directives很酷 Angu ...
- 【AngularJS】—— 6基于AngularJS的过滤与排序
阅读目录 程序设计分析 代码以及结果 前面了解了AngularJS的使用方法,这里就简单的写个小程序,实现查询过滤以及排序的功能. 本程序中可以了解到: 1 angularjs的过滤器 2 n ...
- Memcached原理分析
Memcached的内存管理方式 Memcached采用了名为Slab Allocation的机制分配,管理内存. Slab Allocation的原理相当简单.将分配的内存分割成各种尺寸的块(chu ...
- html 常用标签补充
<body> <!--预处理标签 <pre>--> <pre> 你好, 空格 换3行. 你<sup>上标</sup>好<s ...
- LR常用函数整理
1,变量转参数lr_save_string("aaa","param"):将字符串"aaa"或者一个字符串变量,转变成LR的参数{param ...
- Javascript高级程序设计——函数内部属性与函数属性
函数内部属性 函数内部有两个特殊的属性arguments和this.其中,arguments是类数组对象,包含传入函数中的所有值,这个arguments还有一个属性:callee,这个属性是一个指针, ...
- [HDU2089]不要62
[HDU2089]不要62 试题描述 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer).杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就 ...