HackRank Two Two

9 篇文章 0 订阅
7 篇文章 0 订阅

原题网址:https://www.hackerrank.com/challenges/two-two

Prof. Twotwo as the name suggests is very fond powers of 2. Moreover he also has special affinity to number 800. He is known for carrying quirky experiments on powers of 2.

One day he played a game in his class. He brought some number plates on each of which a digit from 0 to 9 is written. He made students stand in a row and gave a number plate to each of the student. Now turn by turn, he called for some students who are standing continuously in the row say from index i to index j (i<=j) and asked them to find their strength.

The strength of the group of students from i to j is defined as:

strength(i , j)
{
    if a[i] = 0
        return 0; //If first child has value 0 in the group, strength of group is zero
    value = 0;
    for k from i to j
        value = value*10 + a[k]
    return value;
} 

Prof called for all possible combinations of i and j and noted down the strength of each group. Now being interested in powers of 2, he wants to find out how many strengths are powers of two. Now its your responsibility to get the answer for prof.

Input Format 
First line contains number of test cases T 
Next T line contains the numbers of number plates the students were having when standing in the row in the form of a string A.

Constraints

1 ≤ T ≤ 100 
1 ≤ len(A) ≤ 105 
0 ≤ A[i] ≤ 9

Output Format
Output the total number of strengths of the form 2x such that 0 ≤ x ≤ 800.

Sample Input

5
2222222
24256
65536
023223
33579

Sample Output

7
4
1
4
0

Explanation:

In following explanations group i-j is group of student from index i to index j (1-based indexing)

  • In first case only 2 is of form power of two. It is present seven times for groups 1-1,2-2,3-3,4-4,5-5,6-6,7-7

  • In second case 2,4 and 256 are of required form. 2 is strength of group 1-1 and 3-3, 4 is strength of group 2-2 and 256 is strength of group 3-5.

  • In third case 65536 is only number in required form. It is strength of group 1-5

  • In fourth case 2 and 32 are of forms power of 2. Group 1-2 has values 0,2 but its strength is 0, as first value is 0.

  • In fifth case, None of the group has strength of required form.



方法:使用Trie。

import java.io.*;
import java.util.*;
import java.math.BigInteger;
    
public class Solution {
    private static char[][] pows = new char[801][];
    private static Trie root = new Trie();
    static {
        BigInteger bi = new BigInteger("1");
        pows[0] = bi.toString().toCharArray();
        BigInteger two = new BigInteger("2");
        for(int i = 1; i <= 800; i++) {
            bi = bi.multiply(two);
            pows[i] = bi.toString().toCharArray();
        }
        for(int i = 0; i <= 800; i++) {
            Trie node = root;
            for(int j = 0; j < pows[i].length; j++) {
                node = node.append(pows[i][j] - '0');
            }
            node.hasData = true;
        }
    }

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        for(int i = 0; i < t; i++) {
            char[] sa = scanner.next().toCharArray();
            int count = 0;
            List<Trie> currents = new ArrayList<>();
            for(int j = 0; j < sa.length; j++) {
                currents.add(root);
                List<Trie> nexts = new ArrayList<>();
                for(Trie node : currents) {
                    if (node.nexts[sa[j] - '0'] != null) {
                        if (node.nexts[sa[j] - '0'].hasData) count++;
                        nexts.add(node.nexts[sa[j] - '0']);
                    }
                }
                currents = nexts;
            }
            System.out.println(count);
        }
    }
}
class Trie {
    boolean hasData;
    Trie[] nexts = new Trie[10];
    Trie append(int n) {
        if (nexts[n] == null) nexts[n] = new Trie();
        return nexts[n];
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值