Fibonacci String 1708

Source: Internet
Author: User
Tags printf
Problem Description after little Jim learned Fibonacci number in the class, he is very interest in it.
Now he's thinking about a new thing--Fibonacci String.

He Defines:str[n] = str[n-1] + str[n-2] (n > 1)

He's so crazying if someone gives him, strings str[0] and str[1], he'll calculate the str[2],str[3],str[4], St R[5] ....

For example:
If str[0] = "AB"; STR[1] = "BC";
He'll get the result, str[2]= "ABBC", str[3]= "BCABBC", str[4]= "ABBCBCABBC" ...;

As the string is too long and Jim can ' t write down all the strings in paper. So he just want to know how many times all letter appears in Kth Fibonacci String. Can you help him? Input The first line contains a integer N which indicates the number of test cases.
Then N cases follow.
In each case,there is strings str[0], str[1] and a integer k (0 <= K <) which is separated by a blank.
The string in the input would only be contains less than low-case letters. Output for each case,you should count how many times all letter appears in the Kth Fibonacci String and print out them in The format "X:n".
If you still has some questions, look the sample output carefully.
Please output a blank line after each test case.

The problem easier, you can assume the result would in the range of int. Sample Input
1 ab BC 3 Sample Output
a:1 b:3 c:2 d:0 e:0 f:0 g:0 h:0 i:0 j:0 k:0 l:0 m:0 n:0 o:0 p:0 q:0 r:0 s:0 t:0 u:0 v:0 w:0 x:0 y:0 z:0 AC Code
#include <cstdio> #include <map> #include <string> #include <algorithm> #include <iostream
    > int main (int argc, const char* argv[]) {int ncases = 0;
    scanf ("%d", &ncases);
        while (ncases--) {int k;
        Std::string sz0, sz1;

        Std::cin >> sz0 >> sz1 >> k;
        Initializes the first with a second Std::map<char, __int64> map_a, Map_b, Map_c;
            for (Char ch= ' a '; Ch <= ' z '; ++ch) {Map_a[ch] = count (Sz0.begin (), Sz0.end (), ch);
        Map_b[ch] = count (Sz1.begin (), Sz1.end (), ch);
                }///analog backwards plus for (int i=2; i<=k; ++i) {if (i%3 = = 2) {
                for (Char ch= ' a '; Ch <= ' z '; ++ch) {Map_c[ch] = Map_a[ch] + map_b[ch];
                }} if (i%3 = = 0) {for (char ch= ' a '; Ch <= ' z '; ++ch)
               {     MAP_A[CH] = Map_b[ch] + map_c[ch];
                }} if (i%3 = = 1) {for (char ch= ' a '; Ch <= ' z '; ++ch)
                {Map_b[ch] = Map_a[ch] + map_c[ch]; }}}///output result if (k%3 = = 0) {for (char ch= ' a '; Ch <= ' z '; + +)
            CH) {printf ("%c:%i64d\n", CH, map_a[ch]);
                }} else if (k%3 = = 1) {for (char ch= ' a '; Ch <= ' z '; ++ch) {
            printf ("%c:%i64d\n", CH, map_b[ch]); }} else {for (char ch= ' a '; Ch <= ' z '; ++ch) {printf
            ("%c:%i64d\n", CH, map_c[ch]);
    }} printf ("\ n");
} return 0;
 }

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.