HDOJ(HDU) 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
格式:每个案例后面都有一个空行!!!
不能直接用字符串相加来做,因为可能到后面会超内存!累加到后面的字符串太长了!!!
所以换位思考,既然是统计字母的个数,为什么不直接来建立整型数组呢。
只要统计出str0和str1中各个字母的个数就可以了。
后面各个字母个数的按照公式来推就行。
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
long num[][] = new long[56][26];
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
for(int i=0;i<num[0].length;i++){
for(int j=0;j<num.length;j++){
num[j][i]=0;
}
}
String str0 = sc.next();
for(int i=0;i<str0.length();i++){
for(int j='a';j<='z';j++){
if(str0.charAt(i)==(char)j){
num[0][j-'a']++;
break;
}
}
}
String str1 = sc.next();
for(int i=0;i<str1.length();i++){
for(int j='a';j<='z';j++){
if(str1.charAt(i)==(char)j){
num[1][j-'a']++;
break;
}
}
}
int n = sc.nextInt();
for(int i=2;i<=n;i++){
for(int k='a';k<='z';k++){
num[i][k-'a'] = num[i-1][k-'a']+num[i-2][k-'a'];
}
}
for(int k='a';k<='z';k++){
System.out.println((char)k+":"+num[n][k-'a']);
}
System.out.println();
}
}
}
HDOJ(HDU) 1708 Fibonacci String的更多相关文章
- hdu 1708 Fibonacci String
Fibonacci String Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- HDOJ/HDU 2539 点球大战(String.endsWith()方法的一个应用~)
Problem Description 在足球比赛中,有不少赛事,例如世界杯淘汰赛和欧洲冠军联赛淘汰赛中,当比赛双方经过正规比赛和加时赛之后仍然不分胜负时,需要进行点球大战来决定谁能够获得最终的胜利. ...
- HDU 1708 简单dp问题 Fibonacci String
Fibonacci String Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Fibonacci String(hdu 1708)
Fibonacci String Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3117 Fibonacci Numbers(围绕四个租赁斐波那契,通过计++乘坐高速动力矩阵)
HDU 3117 Fibonacci Numbers(斐波那契前后四位,打表+取对+矩阵高速幂) ACM 题目地址:HDU 3117 Fibonacci Numbers 题意: 求第n个斐波那契数的 ...
- HDU 4054 Number String
HDU 4054 Number String 思路: 状态:dp[i][j]表示以j结尾i的排列 状态转移: 如果s[i - 1]是' I ',那么dp[i][j] = dp[i-1][j-1] + ...
- HDOJ(HDU).1412 {A} + {B} (STL SET)
HDOJ(HDU).1412 {A} + {B} (STL SET) 点我挑战题目 题意分析 大水题,会了set直接用set即可. 利用的是set的互异性(同一元素有且仅有一项). #include ...
- HDOJ(HDU).1754 I Hate It (ST 单点替换 区间最大值)
HDOJ(HDU).1754 I Hate It (ST 单点替换 区间最大值) 点我挑战题目 题意分析 从题目中可以看出是大数据的输入,和大量询问.基本操作有: 1.Q(i,j)代表求区间max(a ...
- HDOJ(HDU).1166 敌兵布阵 (ST 单点更新 区间求和)
HDOJ(HDU).1166 敌兵布阵 (ST 单点更新 区间求和) 点我挑战题目 题意分析 根据数据范围和询问次数的规模,应该不难看出是个数据结构题目,题目比较裸.题中包括以下命令: 1.Add(i ...
随机推荐
- Maven常用插件--转
=========Maven Report Plugin========= 1.源码分析 <artifactId>maven-pmd-plugin</artifactId> 2 ...
- 案例:计算1!+2!+3!+......+n!
/* * 1!+2!+3!+......+n! * */ import java.util.Scanner; public class ForTest{ public static void main ...
- android应用的不同版本间兼容性处理
在Android系统中向下兼容性比较差,但是一个应用APP经过处理还是可以在各个版本间运行的.向下兼容性不好,不同版本的系统其API版本也不同,自然有些接口也不同,新的平台不能使用旧的API,旧的平台 ...
- Miller_Rabin codevs 1702 素数判定2
/* 直接费马小定理 */ #include<iostream> #include<cstdio> #include<cstdlib> #include<ct ...
- C#解leetcode:119. Pascal's Triangle II
题目是: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return ...
- javascript 实用函数
1.去除字符串空格 /*去左空格*/ function ltrim(s) { return s.replace(/^(\s*| *)/, ""); } /*去右空格*/ funct ...
- OUTPUT 在insnert delete update 的神奇功效
Inserted deleted 个人理解 应该是两个 临时表 分别存储 变动后的数据集 和 变动前的数据集 使用例子: 1.对于INSERT,可以引用inserted表以查询新行的属性 ...
- jrae源码解析(一)
jare用java实现了论文<Semi-Supervised Recursive Autoencoders for Predicting Sentiment Distributions>中 ...
- iOS-点击视图,视图背景颜色随机更改
一.效果图 二.代码 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the v ...
- Swift - 02 - 常量和变量
//: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...