HDU 1708 Fibonacci String

Source: Internet
Author: User

Topic Links: Fibonacci string Fibonacci string


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


See the topic I do not think, directly on the simulation, handed several hair are mle, because the string is too long, and then look at the key, found that everyone is basically using DP write .... dp[i][j] denotes the value of J for number I

Reference http://blog.sina.com.cn/s/blog_96e2982001014tak.html

Code:

#include <stdio.h>
#include <string.h>
const int maxn=55;
int DP[MAXN][MAXN];
int main ()
{
    //freopen ("In.txt", "R", stdin);
    Char a[50],b[50];
    int k;
    int t;
    scanf ("%d", &t);
    while (t--)
    {
        memset (dp,0,sizeof (DP));
        scanf ("%s%s%d", a,b,&k);
        for (int i=0;a[i];i++)
            dp[0][a[i]-' a ']++;
        for (int i=0;b[i];i++)
            dp[1][b[i]-' a ']++;
        for (int. i=2;i<=k;i++) for
            (int j=0;j<26;j++)
            dp[i][j]=dp[i-1][j]+dp[i-2][j];
        for (int i=0;i<26;i++)
            printf ("%c:%d\n", i+ ' a ', dp[k][i]);
        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.