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 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

Author

linle

Source

HDU 2007-Spring Programming Contest

题解:求第你n个斐波那契字符串的字母统计.....求出第n个斐波那契字符串再统计。

AC代码:

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<iomanip>
#include<algorithm>
#include<time.h>
typedef long long LL;
using namespace std;

int main()
{
	int t;
    string s0,s1;
	int n;
	LL s[50]={0};
	s[1]=1;
	for(int i=2;i<50;i++)
	{
		s[i]=s[i-1]+s[i-2];
	}
	cin>>t;
	while(t--)
	{
		cin>>s0>>s1>>n;
		LL ch1[26]={0};
		LL ch2[26]={0};

		for(int i=0;i<s0.size();i++)
		{
			ch1[s0[i]-'a']++;    //字符转换成数字
		}
		for(int i=0;i<s1.size();i++)
		{
			ch2[s1[i]-'a']++;   //字符转换成数字
		}

		if(n==0)
		{
			for(int i=0;i<26;i++)
			{
				cout<<(char)('a'+i)<<":"<<ch1[i]<<endl;   //n=0时,只输出第一个
			}
			cout<<endl; continue;
		}
		for(int i=0;i<26;i++)
		{
			cout<<(char)('a'+i)<<":"<<s[n-1]*ch1[i]+s[n]*ch2[i]<<endl; //累加到第n-1个和第n个
		}
		cout<<endl;
	}

    return 0;

}
时间: 2024-11-05 04:53:23

HDU 1708 Fibonacci String(斐波那契字串)的相关文章

HDOJ/HDU 1250 Hat&#39;s Fibonacci(大数~斐波拉契)

Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1. F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4) Your task is to take

在sqlserver中做fibonacci(斐波那契)规律运算

--利用sqlserver来运算斐波那契规律 declare @number intdeclare @A intdeclare @B intdeclare @C int set @A=1 set @B=2 set @Number=3 select @[email protected][email protected] while(@Number<60) begin    set @[email protected][email protected]  if(@@ERROR<>0)  go

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

GameTheory(二):Fibonacci Game(斐波那契博弈)

本质描述: 有n个物品,游戏双方轮流取物品,规则为: 1.先手不能在第一次把所有的物品取完 2.之后每次可以取的物品个数为[ 1 , 2 * 对手取的数量] 轮到某人取,这个人没东西取就是输了. 结论: 当n为斐波那契数的时候,先手处于必败态 分析一下: 我们可以看到,这个博弈跟Bash Game不同,这个规则是动态的.证明这个Game要用到Zeckendorf(齐肯多夫定理):任何正整数都可以表示成若干个不连续的斐波那契数(1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 ,

Fibonacci series(斐波纳契数列)的几种常见实现方式

费波那契数列的定义: 费波那契数列(意大利语:Successione di Fibonacci),又译费波拿契数.斐波那契数列.斐波那契数列.黄金分割数列. 在数学上,费波那契数列是以递归的方法来定义: (n≧2) 用文字来说,就是费波那契数列由0和1开始,之后的费波那契系数就由之前的两数相加. 首几个费波那契系数是:0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233-- 特别指出:0不是第一项,而是第零项. 下面是费波那契数列的几种常见编程实现:

POJ 3070 Fibonacci【斐波那契数列/矩阵快速幂】

Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17171   Accepted: 11999 Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequen

Computational Complexity of Fibonacci Sequence / 斐波那契数列的时空复杂度

Fibonacci Sequence 维基百科 \(F(n) = F(n-1)+F(n-2)\),其中 \(F(0)=0, F(1)=1\),即该数列由 0 和 1 开始,之后的数字由相邻的前两项相加而得出. 递归 def fibonacci(n): assert n >= 0, 'invalid n' if n < 2: return n return fibonacci(n - 1) + fibonacci(n -2) 递归方法的时间复杂度为高度为 \(n-1\) 的不完全二叉树的节点数,

Codeforces Round #FF (Div. 2) E. DZY Loves Fibonacci Numbers(斐波那契的定理+线段树)

/* 充分利用了菲波那切数列的两条定理: ①定义F[1] = a, F[2] = b, F[n] = F[n - 1] + F[n - 2](n≥3). 有F[n] = b * fib[n - 1] + a * fib[n - 2](n≥3),其中fib[i]为斐波那契数列的第 i 项. ②定义F[1] = a, F[2] = b, F[n] = F[n - 1] + F[n - 2](n≥3). 有F[1] + F[2] + -- + F[n] = F[n + 2] - b 这题还有一个事实,

Fibonacci(斐波那契数列)的第N位数的第N位数

无穷数列1,1,2,3,5,8,13,21,34,55...称为Fibonacci数列,它可以递归地定义为F(n)=1 ...........(n=1或n=2)F(n)=F(n-1)+F(n-2).....(n>2)现要你来求第n个斐波纳奇数.(第1个.第二个都为1): 输入第一行是一个整数m(m<5)表示共有m组测试数据每次测试数据只有一行,且只有一个整形数n(输出对每组输入n,输出第n个Fibonacci数 #include<stdio.h> long int fib(long