GCJ Round 1C B. Typewriter Monkey

Source: Internet
Author: User

Subtract the probability of the target string from the number of bananas needed ....

How to find the probability of occurrence?
The probability that each character appears is multiplied ... Multiply the number of positions the target string can swing ... Problem

Your Publishing House have decided to use monkeys randomly typing at keyboards to write great works of literature. You is the supervisor for one monkey with a keyboard containingKKeys, each of which are labeled with an uppercase 中文版. (There may multiple keys displaying the same letter.) The monkey would start with a empty string and repeat the followingSTimes:choose a key from their keyboard uniformly at random and press it, adding a copy of this key ' s letter to the right en D of the string. The final resulting string would have the lengthS.

You have aTarget Wordof lengthLThat's hoping the monkey would type. (The target word won't necessarily be a real 中文版 word.) This target word could even appear multiple times in what the monkey types. (Overlapping instances count too--for example, if "ABA" are the target word and the monkey types "Ababa", that contains T Wo instances of the target.)

The monkey one banana for each instance of the target word it types. When your go to inspect the monkey's work, you'll bring along the minimum number of bananas that's you need toensureThat's all you'll have enough bananas to pay the monkey and no matter what it has typed. Then, you'll pay the monkey one banana for each instance of the target word itactuallyTyped. You'll keep the remaining bananas that's brought with you.

What is the expected number of bananas so you'll get to keep?

Input

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 中文版 letters representing the monkey ' s keyboard. The third contains a string of L uppercase 中文版 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'll get to Kee p after paying the monkey.

y'll be considered correct if it's within an absolute or relative error of -6Of the correct answer. See theFAQfor the explanation of what is means, and what formats of real numbers we accept.

Limits

1 ≤ T ≤100.

Small DataSet

1 ≤ K ≤7.
1 ≤ LS ≤7.

Large DataSet

1 ≤ K ≤100.
1 ≤ LS ≤100.

Sample

Input

Output
57 6 6bananasmonkey2 3 4aaaaa2 1 2abb6 2 2googlego26 11 100ABCDEFGHIJKLMNOPQRSTUVWXYZROSENCRANTZ 
case #1:0.0Case #2:0.0Case #3:1.0Case #4:0.8888889Case #5:9.0 
Note This case #5 is a within the limits for the Small dataset.

in case #1, the monkey have no chance of typing the target word "monkey" even once (because he keyboard lacks most of The letters in "MONKEY"), so if you don't bring any bananas along when you visit, and of the course you don't pay any. Poor monkey!

in case #2, the monkey was guaranteed to type "AAAA", which had a overlapping instances of the target word "AAA". you'll bring the bananas and then pay both.

in case #3, the monkey would produce the following outputs with equal probability (section each): "AA", "AB", "BA", "BB". T Hese has 0, 1, 1, and 2 instances of the target word, respectively. You must bring 2 bananas to being ready for the ' BB ' case, but you'll on average pay (0 + 1 + 1 + 2)/4 = 1.

in case #4, the monkey have a 1/3 chance of typing a "G" first and a 1/3 chance of typing an "O" second, for a 1/9 Chan Ce of typing "GO". You'll 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 ONC E is so small that they is negligible compared to the acceptable margin of error for answers.
#include <bits/stdc++.h>using namespace std; #define PRT (k) cerr<< #k "=" <<k<<endlconst int N = 23  3;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 (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 has (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 Round 1C B. Typewriter Monkey

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.