码迷,mamicode.com
首页 > 其他好文 > 详细

GCJ 2015 Round 1C B. Typewriter Monkey

时间:2015-05-11 00:09:29      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:

用最多需要的香蕉数减去目标串出现的概率就行啦。。。。

 如何求出现的概率?

每个字符出现的概率乘起来……再乘以目标串能摆的位置个数……
技术分享
技术分享

Problem

Your publishing house has decided to use monkeys randomly typing at keyboards to write great works of literature. You are the supervisor for one monkey with a keyboard containing K keys, each of which is labeled with an uppercase English letter. (There may be multiple keys displaying the same letter.) The monkey will start with an empty string and repeat the following S times: choose a key from its keyboard uniformly at random and press it, adding a copy of that key‘s letter to the right end of the string. The final resulting string will have length S.

You have a target word of length L that you are hoping the monkey will type. (The target word will not necessarily be a real English word.) This target word may even appear multiple times in what the monkey types. (Overlapping instances count too -- for example, if "ABA" is the target word and the monkey types "ABABA", that contains two instances of the target.)

You plan to pay the monkey one banana for each instance of the target word that it types. When you go to inspect the monkey‘s work, you will bring along the minimum number of bananas that you need to ensure that you will always have enough bananas to pay the monkey, no matter what it has typed. Then, you will pay the monkey one banana for each instance of the target word that it actually typed. You will keep the remaining bananas that you brought with you.

What is the expected number of bananas that you will get to keep?

Input

The first line of the input gives the number of test cases, TT test cases follow. Each consists of three lines. The first contains three space-separated positive integers: KL, and S. The second contains a string of K uppercase English letters representing the monkey‘s keyboard. The third contains a string of L uppercase English letters representing the target word.

Output

For each test case, output one line containing "Case #x: y", where y is the expected number of bananas you will get to keep after paying the monkey.

y will be considered correct if it is within an absolute or relative error of 10-6 of the correct answer. See the FAQ for an explanation of what that means, and what formats of real numbers we accept.

Limits

1 ≤ T ≤ 100.

Small dataset

1 ≤ K ≤ 7.
1 ≤ L ≤ S ≤ 7.

Large dataset

1 ≤ K ≤ 100.
1 ≤ L ≤ S ≤ 100.

Sample


Input 
 

Output 
 
5
7 6 6
BANANAS
MONKEY
2 3 4
AA
AAA
2 1 2
AB
B
6 2 2
GOOGLE
GO
26 11 100
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ROSENCRANTZ

Case #1: 0.0
Case #2: 0.0
Case #3: 1.0
Case #4: 0.8888889
Case #5: 9.0

Note that Case #5 is not within the limits for the Small dataset.

In Case #1, the monkey has no chance of typing the target word "MONKEY" even once (because his keyboard lacks most of the letters in "MONKEY"), so you do not bring any bananas along when you visit, and of course you do not pay any. Poor monkey!

In Case #2, the monkey is guaranteed to type "AAAA", which has two overlapping instances of the target word "AAA". You will bring two bananas and then pay both.

In Case #3, the monkey will produce the following outputs with equal probability (1/4 each): "AA", "AB", "BA", "BB". These have 0, 1, 1, and 2 instances of the target word, respectively. You must bring 2 bananas to be ready for the "BB" case, but you will on average pay (0 + 1 + 1 + 2) / 4 = 1.

In Case #4, the monkey has a 1/3 chance of typing a "G" first and a 1/3 chance of typing an "O" second, for a 1/9 chance of typing "GO". You will bring one banana and give it up 1/9 of the time.

In Case #5, the monkey could in theory type "ROSENCRANTZ" up to nine times, but the chances of this happening even once are so small that they are negligible compared to the acceptable margin of error for answers.
#include <bits/stdc++.h>
using namespace std;
#define prt(k) cerr<<#k" = "<<k<<endl
const int N = 233;
const int inf = 0x3f3f3f3f;
typedef long long ll;
#define SZ(v) ((int)(v).size())
#define SZ(v) ((int)(v).size())
#define pb push_back
#define ALL(v) (v).begin(), (v).end()
#define Fill(a,b) memset(a,b,sizeof(a))
#define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)
int K, L , S;
string a, b;
bool have(string a , char c)
{
    for (char x : a)
        if (x==c)
        return true;
    return false;
}
int main()
{
    int re,ca=1;cin>>re;
    for (;ca<=re;ca++)
    {
        cin>>K>>L>>S;
        cin>>a>>b;
        printf("Case #%d: ", ca);
        double p[555];
        memset(p,0,sizeof p);
        for(char x : a) p[x]++;
        double ans = (S - L + 1);
        for (char x:b) ans *= p[x] / double(K);
        int len  = 0;
        for (int i=1;i<L;i++)
        {
            if (b.substr(0, i) == b.substr(L - i))
            {
                len = i;
            }
        }
        int maxv = (S - len) / (L - len);
        ans = maxv - ans;
        bool ok = true;
        for (char x : b)
        {
            if (!have(a, x))
                ok = false;
        }
        if (!ok) {
            cout<<"0.0\n";
            continue;
        }
        printf("%.8f\n", ans);
    }
}


GCJ 2015 Round 1C B. Typewriter Monkey

标签:

原文地址:http://blog.csdn.net/oilover/article/details/45627893

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!