HDU 1708 Fibonacci string (mathematical problem)

Source: Internet
Author: User

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1708


Problem descriptionafter little Jim learned Maid number in the class, he was very interest in it.
Now he is thinking about a new thing -- maid string.

He defines: Str [N] = STR [n-1] + STR [N-2] (n> 1)

He is so crazying that if someone gives him two strings STR [0] and STR [1], he will calculate the STR [2], STR [3], STR [4], STR [5]...

For example:
If STR [0] = "AB"; STR [1] = "BC ";
He will get the result, STR [2] = "abbc", STR [3] = "bcabbc", STR [4] = "abbcbcabbc "............;

As the string is too long, Jim can't write down all the strings in paper. so he just want to know how many times each letter appears in kth Fibonacci string. can you help him?
Inputthe first line contains a integer n which indicates the number of test cases.
Then n cases follow.
In each case, there are two strings STR [0], STR [1] and a integer K (0 <= k <50) which are separated by a blank.
The string in the input will only contains less than 30 low-case letters.
Outputfor each case, you shocould count how many times each letter appears in the kth fig string and print out them in the format "X: N ".
If you still have some questions, look the sample output carefully.
Please output a blank line after each test case.

To make the problem easier, you can assume the result will in the range of Int.
Sample Input
1ab bc 3
Sample output
a:1b:3c:2d:0e:0f:0g:0h:0i:0j:0k:0l:0m:0n:0o:0p:0q:0r:0s:0t:0u:0v:0w:0x:0y:0z:0
Authorlinle sourcehdu 2007-spring Programming Contest


PS:

At that time, the array that recorded the Fibonacci was also converted into 26, which resulted in the failure to find the cause after wa!

The pattern is broken!



The Code is as follows:

#include <cstdio>#include <cstring>int main(){    int t;    char s0[30], s1[30];    int k, a0[26], a1[26];    int  c[50];    c[0] = 0, c[1] = 1;    for(int i = 2; i <= 50; i++)    {        c[i] = c[i-1] + c[i-2];    }    scanf("%d",&t);    while(t--)    {        memset(s0,0,sizeof(s0));        memset(s1,0,sizeof(s1));        memset(a0,0,sizeof(a0));        memset(a1,0,sizeof(a1));        scanf("%s%s%d",s0,s1,&k);        for(int i = 0; i < strlen(s0); i++)        {            int tt = s0[i]-'a';            a0[tt]++;        }        for(int i = 0; i < strlen(s1); i++)        {            int tt = s1[i]-'a';            a1[tt]++;        }        for(int i = 0; i < 26; i++)        {            if(k == 0)            {                printf("%c:%d\n",'a'+i,a0[i]);            }            else                printf("%c:%d\n",'a'+i,a0[i]*c[k-1]+a1[i]*c[k]);        }        printf("\n");    }    return 0;}


HDU 1708 Fibonacci string (mathematical problem)

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.