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 ...
随机推荐
- python字符类型的一些方法
python 字符串和字节互转换.bytes(s, encoding = "utf8") str(b, encoding = "utf-8") i.isspac ...
- spring的PathMatchingResourcePatternResolver-通配符的Resource查找器
PathMatchingResourcePatternResolver是一个通配符的Resource查找器,包括: /WEB-INF/*-context.xml com/mycompany/**/ap ...
- QQ空间HD(2)-UIPopoverController其它使用
DJTestViewController.m #import "DJTestViewController.h" #import "DJColorTableViewCont ...
- php怎么获取mac地址?
如何用php获取mac地址呢?大家知道mac地址是电脑在全球范围的唯一标识,所以这个就非常实用,比如说要做一个投票功能,那mac地址是必不可少 的,如果单纯的靠ip地址来判断这个肯定是不准确的,水分太 ...
- 11个很棒的 jQuery 图表库
如果你曾经使用过任何类型的数据,你应该知道阅读一排排数据的痛苦.通过所有这些数据弄清楚他们的意思是非常不容易的.可视化对于解决这个问题起到了重要的作用.可视化降低了数据阅读的难度,帮助决策者获得可操作 ...
- EF-联合查询-结果集-Group by-统计数目
EF框架用着痛并且快乐着··· 毕竟用习惯了SQL语句直接硬查的··· SELECT C0.ID,C_C.Name,C_C.C_COUNT FROM article_type C0 INNER JOI ...
- GCD与NSOperationQueue
1> GCD是纯C语言的API,NSOperationQueue是基于GCD的OC版本封装 2> GCD只支持FIFO(先入先出)的队列,NSOperationQueue可以很方便地调整执 ...
- LYDSY模拟赛day3 序列
NOIP不考可持久,弃坑
- Hanoi问题
#include<stdio.h>int main(){ int m; void hanoi(int n,char x,char y,char z); printf("input ...
- python 多线程就这么简单(转)
多线程和多进程是什么自行google补脑 对于python 多线程的理解,我花了很长时间,搜索的大部份文章都不够通俗易懂.所以,这里力图用简单的例子,让你对多线程有个初步的认识. 单线程 在好些年前的 ...