Problem Description
After little Jim learned Fibonacci Number in the class , he was very interest in it.
Now he is thinking about a new thing -- Fibonacci 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 ?
Now he is thinking about a new thing -- Fibonacci 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 ?
Input
The 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.
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.
Output
For each case,you should count how many times each letter appears in the Kth Fibonacci 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.
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
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题目大意:斐波那契数列的变式,给两个字符串,str[0]和str[1],作为斐波那契数列的开始,规则是str[n]等于str[n-2]和str[n-1]的连接,以及整数k,求出str[k]各个字母的个数。例:str[0]="ab",str[1]="bc",str[2]="abbc",str[3]="bcabbc",其中a的个数为1个,b为3个,c为2个大致思路,即统计出两个字符串中每个字母的个数,然后循环做斐波那契,得出答案即可。#include<stdio.h> #include<string.h> int book1[26],book2[26]; int main() { int n,i,k,add1,add2,add3; scanf("%d",&n); char buf1[31],buf2[31]; for(i=0;i<n;i++) { scanf("%s %s",buf1,buf2); scanf("%d",&k); memset(book1,0,sizeof(book1)); memset(book2,0,sizeof(book2)); for(int t=0;t<strlen(buf1);t++)//统计str1每个字母的个数 { book1[buf1[t]-'a']++; } for(int t=0;t<strlen(buf2);t++)//统计str2每个字母的个数 { book2[buf2[t]-'a']++; } for(int p=0;p<26;p++) { add1=book1[p]; add2=book2[p]; if(add1==0&&add2==0) { printf("%c:%d\n",'a'+p,0); } else { if(k<2) { if(k==0) printf("%c:%d\n",'a'+p,add1); else printf("%c:%d\n",'a'+p,add2); } else { for(int b=0;b<k-1;b++)//循环得出结果 { add3=add1+add2; add1=add2; add2=add3; } printf("%c:%d\n",'a'+p,add3); } } } printf("\n"); } }
本来这题也没什么好讲,但是晚上在交流讨论的时候,有同学提出一个很好的思路,预处理。不仅在这道题目中,还可以做推广。原理是这样的:假设新的斐波那契的第一项为A,第二项为B,那么第三项就是A+B,第四项A+2B,第五项2A+3B.....我们可以看到从第二项开始B的部分个数也是1,1,2,3,5.....呈斐波那契数列增长从第三项开始A的部分个数也是1,1,2,3,5.....也 呈斐波那契数列增长所以总结可得到:若设斐波那契数列的第n项为Fn的话,那么新的斐波那契数列等于F(n-2)*A+F(n-1)*B
因此可以先做一次预处理,然后利用公式来计算出结果,避免多次循环
代码如下:#include<stdio.h> #include<string.h> long long Fibonacci[51]; long long book1[26],book2[26]; void pre_active()//预处理算出斐波那契前50项 { int i; Fibonacci[0]=Fibonacci[1]=1; for(i=2;i<51;i++) { Fibonacci[i]=Fibonacci[i-1]+Fibonacci[i-2]; //printf("%lld ",Fibonacci[i]); } } int main() { pre_active();//预处理 int n,k; char str1[31],str2[31]; scanf("%d",&n); for(int i=0;i<n;i++) { memset(book1,0,sizeof(book1)); memset(book2,0,sizeof(book2)); scanf("%s%s%d",str1,str2,&k); for(int t=0;t<strlen(str1);t++)//统计出str1各个字母的个数 { book1[str1[t]-'a']++; } for(int t=0;t<strlen(str2);t++)//统计出str2各个字母的个数
{ book2[str2[t]-'a']++; } for(int t=0;t<26;t++) { if(k==0)//对n<2的情况做特殊处理 printf("%c:%lld\n",'a'+t,book1[t]); else if(k==1) printf("%c:%lld\n",'a'+t,book2[t]); else printf("%c:%lld\n",'a'+t,book1[t]*Fibonacci[k-2]+book2[t]*Fibonacci[k-1]);//套用公式 } printf("\n"); } }