标签:
用最多需要的香蕉数减去目标串出现的概率就行啦。。。。
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?
The first line of the input gives the number of test cases, T. T test cases follow. Each consists of three lines. The first contains three space-separated positive integers: K, L, 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.
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.1 ≤ T ≤ 100.
1 ≤ K ≤ 7.
1 ≤ L ≤ S ≤ 7.
1 ≤ K ≤ 100.
1 ≤ L ≤ S ≤ 100.
#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