1708 Fibonacci String

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 } 

时间: 2024-10-05 19:15:15

1708 Fibonacci String的相关文章

HDU 1708 Fibonacci String(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1708 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] + s

HDU 1708 Fibonacci String(斐波那契字串)

Fibonacci String Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5358    Accepted Submission(s): 1819 Problem Description After little Jim learned Fibonacci Number in the class , he was very in

杭电 HDU ACM 1708 Fibonacci String

 Fibonacci String Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4102    Accepted Submission(s): 1396 Problem Description After little Jim learned Fibonacci Number in the class , he was very

HDU 1708 简单dp问题 Fibonacci String

Fibonacci String Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4568    Accepted Submission(s): 1540 Problem Description After little Jim learned Fibonacci Number in the class , he was very int

Fibonacci String(hdu 1708)

Fibonacci String Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5008    Accepted Submission(s): 1690 Problem Description After little Jim learned Fibonacci Number in the class , he was very int

斐波拉切字符串统计个数 Fibonacci String

Problem:  s0 = "a", s1 = "b", s2 = "ba", s3 = "bab", s4 = "babba", s4 = "babbabab", is called Fibonacci string. For the string with index n, given a string str = "bb", calculate how man

UVA - 12041 BFS (Binary Fibonacci String)

Description Problem B - BFS (Binary Fibonacci String) We are familiar with the Fibonacci sequence (1, 1, 2, 3, 5, 8, ...). What if we define a similar sequence for strings? Sounds interesting? Let's see. We define the follwing sequence: BFS(0) = 0 BF

杭电1708Fibonacci String

Fibonacci String Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4641    Accepted Submission(s): 1566 Problem Description After little Jim learned Fibonacci Number in the class , he was very in

【做题】SRM701 Div1 Hard - FibonacciStringSum——数学和式&矩阵快速幂

原文链接 https://www.cnblogs.com/cly-none/p/SRM701Div1C.html 题意:定义"Fibonacci string"为没有连续1的01串.现在,给出\(a,b\),定义一个"Fibonacci string"的权值为\(x^a y^b\),其中\(x\)为0的个数,\(y\)为1的个数. 要求对所有长度为\(n\)的"Fibonacci string"的权值求和,对\(10^9 + 7\)取模. \(n