HDU

来源:互联网 发布:淘宝客佣金5元怎么设置 编辑:程序博客网 时间:2024/06/08 00:33

1sting

Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6310 Accepted Submission(s): 2451

Problem Description

You will be given a string which only contains ‘1’; You can merge two adjacent ‘1’ to be ‘2’, or leave the ‘1’ there. Surly, you may get many different results. For example, given 1111 , you can get 1111, 121, 112,211,22. Now, your work is to find the total number of result you can get.

Input

The first line is a number n refers to the number of test cases. Then n lines follows, each line has a string made up of ‘1’ . The maximum length of the sequence is 200.

Output

The output contain n lines, each line output the number of result you can get .

Sample Input

3
1
11
11111

Sample Output

1
2
8

Author

z.jt
题意: 给你个都是’1’的字符串,相邻的1可以结合为2,问你有几种方式组合

分析: 第一眼看以为是组合数,然后xjb推了一会,发现情况有点复杂,然后转换角度发现是个递推,我们用F[i],代表i位长的1的字符串的组合方式,例如当“111111”,我们考虑最左端的1,我们发现只有两种可能要么不和右边的1组合,要么不组合,如果组合的话恰好是剩下四个1的组合方式,不组合的话是五个1的组合方式,正好和斐波那契数列重合了,由于200项,考虑大数了,偷个懒直接java

参考代码

import java.util.Arrays;import java.util.Scanner;import  java.math.BigInteger;public class Main {    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        BigInteger[] f = new BigInteger[300];        f[1] = BigInteger.valueOf(1);        f[2] = BigInteger.valueOf(2);        for(int i = 3;i < 210;i++) {            f[i] = f[i-1].add(f[i-2]);        }        String s;        int T = in.nextInt();        while (T-- > 0) {            s = in.next();            int len = s.length();            System.out.println(f[len]);        }    }}
  • 如有错误或遗漏,请私聊下UP,thx
原创粉丝点击