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 ?
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.
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.
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
1 #include <iostream> 2 #include <algorithm> 3 #include <stdio.h> 4 #include <string.h> 5 #include <math.h> 6 using namespace std; 7 long long f(int n)//斐波那契数列函数 8 { 9 long long i,a[100]; 10 a[0]=0;a[1]=1; 11 for(i=2;i<=n;i++) 12 a[i]=a[i-1]+a[i-2]; 13 return a[n]; 14 } 15 int main() 16 { 17 char str1[100],str2[100],ch; 18 long long n,m,i,j,k,sum,len1,len2; 19 while(~scanf("%lld",&m)) 20 { 21 while(n--) 22 { 23 scanf("%s%s%lld",str1,str2,&m); 24 len1=strlen(str1); 25 len2=strlen(str2); 26 if(m==0)//一个特例 27 { 28 for(i=0;i<26;i++) 29 { 30 sum=0;ch=‘a‘+i; 31 for(j=0;j<len1;j++) 32 { 33 if(ch==str1[j]) 34 sum=sum+1; 35 } 36 printf("%c:%lld\n",ch,sum); 37 } 38 } 39 else 40 { 41 for(i=0;i<26;i++) 42 { 43 sum=0;ch=‘a‘+i;//找出从a~z 44 for(j=0;j<len1;j++) 45 { 46 if(ch==str1[j])//进行判断 47 sum=sum+f(m-1);//求出总和 48 } 49 for(j=0;j<len2;j++) 50 { 51 if(ch==str2[j]) 52 sum=sum+f(m); 53 } 54 printf("%c:%lld\n",ch,sum);//分别打印 55 } 56 } 57 printf("\n"); 58 } 59 } 60 return 0; 61 }
这个问题中一个特点就是使用scanf输入,如果是cin的话就会超时。
另附一个从网上找到的代码:
1 #include<stdio.h> 2 #include<string.h> 3 char c[1000],s[1000]; 4 int a[27][100];//储存第1~100次所求字符串里边的第1~26个字母的个数. 5 int main() 6 { 7 int t,m,n,k,i,j; 8 scanf("%d",&t); 9 while(t--) 10 { 11 scanf("%s%s%d",c,s,&n); 12 int len=strlen(c);//测长度 13 int lem=strlen(s); 14 memset(a,0,sizeof(a));//清零a数组. 15 for(j=0;j<len;j++) 16 for(i=1;i<=26;i++) 17 if(c[j]==i+‘a‘-1)//如果当前字符等于第i个字母 18 a[i][1]++;//则在a[i][1]++; 19 for(j=0;j<lem;j++) 20 for(i=1;i<=26;i++) 21 if(s[j]==i+‘a‘-1) 22 a[i][2]++; //同理得到第二个字符串的 每一个字母有多少个. 23 for(i=1;i<=26;i++) 24 for(j=3;j<=n+1;j++) 25 a[i][j]=a[i][j-1]+a[i][j-2];//进行斐波那契相加. 26 for(i=1;i<=26;i++) 27 printf("%c:%d\n",i+‘a‘-1,a[i][n+1]); 28 printf("\n");//每一次样例后需要加一个换行,因为没看这个pe了一次. 29 } 30 return 0; 31 }