您的位置:首页 > 其它

HackRank Two Two

2016-07-11 04:36 1931 查看
原题网址: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
== null) nexts
= new Trie();
return nexts
;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hackerrank