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 ...
随机推荐
- thinkphp系统常量与自定义常量
----------------------------------------Action中使用的系统常量 ----------------------------------------THINK ...
- Intellij Idea中运行tomcat 报內存溢出 解决方案 火龙战士 火龙战士
在Run/Debug configuration 的你要运行行的tomcat里面的 vm options里面输入 -server -XX:PermSize=128M -XX:MaxPermSize=2 ...
- SqlServer 18456 错误解决方案
sqlserver 安装需注意事项: 不要选择,否则运行一段时间后sqlserver登录时会出现18456错误. 发生18456错误(sa 可登录的情况下)解决本地用户登录问题的方案: <1& ...
- 跳出iframe
摘要 有时候需要用到iframe,但里面的单击里面的链接的时候,总是在该iframe中打开. 解决办法 其实解决起来也很简单. 在iframe中的head标签中添加下面的标签即可. <base ...
- thinkphp- 许愿墙-1
控制器的方法, 要显示的模板默认的跟方法名相同. 也可以不同, 但应该 仍然是对应文件夹下的html模板文件: $this->display('其他的模板html文件名, 不用加html扩展名' ...
- Hadoop生态系统
Hadoop 生态圈
- iOS原生的搜索:UISearchController
iOS8之前我们使用UISearchDisplayController做TableView的本地搜索,查看UIKit库,苹果已经使用新控件取代它. NS_CLASS_DEPRECATED_IOS(3_ ...
- Python的Set和List的性能比较 + 两者之间的转换
1.能用set 不用list ~$ python -m timeit -n 1000 "[x for x in range(1000) if x in range(500, 1500)]&q ...
- 打开Excel的报错,提示:不能使用对象链接和嵌入
计算机这几天在打开Excel文档的时候,提示:不能使用对象链接和嵌入, 而且出现如下的提示:Microsoft Office Excel进程已停止工作, 每次打开Excel的时候都是同样的问题,害我跟 ...
- 第二天 django apache
1. 475 brew tap homebrew/apache 476 brew install mod_wsgi 477 ls /usr/local/Cellar/mod_wsgi/4.5.7 ...