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 ...
随机推荐
- java内存不足
-Xmx1024m -Xms1024m -XX:PermSize=128m -XX:MaxPermSize=512m ------------------------- 亲测可用
- [转] Epoll 相对Poll和Select的优点
http://blog.csdn.net/summerhust/article/details/18260117 PS: 相对select来说,Poll的监听列表比select更短,并且Poll的监听 ...
- centos7 部署ssserver
centos7 部署shadowsocks服务端 为什么要选centos7? 以后centos7 肯定是主流,在不重要的环境还是尽量使用新系统吧 centos7 的坑 默认可能会有firewall 或 ...
- codevs1690开关灯
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #def ...
- java 类处理工具
public class ClassUtils { private static final Logger LOGGER = LoggerFactory.getLogger(ClassUtils.cl ...
- 让ie8支持foreach
if ( !Array.prototype.forEach ) { Array.prototype.forEach = function forEach( callback, thisArg ) { ...
- Animation动画(一)
Android的animation由四种类型组成:alpha(渐变透明度动画效果).scale(渐变尺寸伸缩动画效果).translate(画面转换位置移动动画效果).rotate(画面转移旋转动画效 ...
- C#中创建、打开、读取、写入、保存Excel的一般性代码
---转载:http://hi.baidu.com/zhaocbo/item/e840bcf941932d15fe358228 1. Excel对象微软的Excel对象模型包括了128个不同的对象,从 ...
- 分页技术之GridView控件
GridView控件实现分页技术 第一步:设置GridView控件的属性,跟分页相关的属性设置如下: AllowPaging="true":允许分页, PageSize=" ...
- Linux基本权限
首先需要我们了解的是,权限(rwx)对于文件和目录的作用是不一样的 . 权限对文件的作用 r : 读取文件内容(cat , more , head , tail) w: 编辑.新增.修改文件内容(vi ...