Need help solving Huffman decoding
I'm given a list of Huffman codes as an input. Each element in the list is separated by a tab character (ex. a [tab] 100100):
a 100100
b 100101
c 110001
d 100000
[newline] 111111
p 111110
q 000001
as well as an encoded string 111110000001100100111111100101110001111110
.
I am being asked to return the correct string from the code. In this case it would be:
pqa
bcp
since p = 111110, q = 000001 and so on.
I've tried submitting my code and the autograder works for 2 cases but doesn't work for the hidden cases.
Would anyone be able to tell me what I might be doing wrong?
static String decode(List<String> codes, String encoded) {
String s = "";
String ans = "";
String[] splited = new String[2];
Hashtable<String, String> numbers = new Hashtable<String, String>();
for(int i = 0; i < codes.size(); i++){
splited = codes.get(i).split("\t");
numbers.put(splited[1],splited[0]);
}
for(int j = 0; j < encoded.length(); j++){
s = s+encoded.charAt(j);
if(numbers.containsKey(s)){
if(s.equals("111111") ){
ans = ans +"\n";
} else{
ans = ans + numbers.get(s);}
s = "";
}
}
return ans;
}
Just to clarify, I am using the first for loop to split each letter from the number code by the tab and putting it into a temp array. Then I am adding these 2 values to a Hashtable (letter, code).
In the second for loop I am iterating each character in the decoded string until the string matches on of the codes, then adding the letter to an answer string and returning that answer string.
See also questions close to this topic

How to prevent char from being entered into JTextField after dispatching event?
In my MainWindow class (a JFrame), I use the "+" and "" keys as hotkeys to modify the value of a certain JTextField called degreeField up or down. I add a KeyEventDispatcher with the following dispatchKeyEventMethod to my KeyboardFocusManger:
@Override public boolean dispatchKeyEvent(KeyEvent evt) { if(simPanel.main.isFocused()){ int type = evt.getID(); if(type == KeyEvent.KEY_PRESSED){ int keyCode = evt.getKeyCode(); switch(keyCode){ // other cases case KeyEvent.VK_PLUS: // increase degreeField by 1, if double // otherwise set it to "270.0" return true; case KeyEvent.VK_MINUS: // decrease degreeField by 1, if double // otherwise set it to "270.0" return true; default: return false; } } else if(type == KeyEvent.KEY_RELEASED){ // irrelevant return false; } } return false; }
The KeyEventDispatcher works and degreeField's text is modified as expected. However, when I have another JTextField focused, the "+" or "" is also entered into that field.
Since I return true, I was under the impression that the event should no longer be dispatched by the JTextField I have focused. Using the NetBeans debugger, I put a break point into the relevant case and checked the text of the focused text field. At the time, there was no + appended. The + is therefore appended after I finish dispatching this event and return true.
Anyone got any ideas, how I can actually prevent the event from being passed down further?
I know I could put an extra listener on the text field to prevent "+" and "" chars from being entered, but i would prefer a solution that works for nonchar keys as well. (I have a similar problem with up and down arrow keys; it doesn't break anything, just annoyingly cycles through my text fields)
Any help would be greatly appreciated. Thanks in advance

How can i load an activity when user connection error?
I want to load an activity when no connection  loadurl("#") not working. I tried to solve this problem but i can't so please if you can help me!

Is it possible to access a variable, using a string which holds that variables name in JAVA?
In Java, If I had a String already declared and it holds the name of a variable. Can I use that String to access that variable? For Example
int sample=10; String test = "sample";
Here,is it possible use the string test to access integer variable sample. If yes, then how.

Comparing two TRIE tries
Does anyone know of an algorithm that can compare two trie trees, and come up with what items from one trie matched what items from another trie?
Ex:
 I have 10 million semistatic rules (change once per day)
 and 100k dynamic items (change on every run)
 each rule/item has 99 string/numeric fields that need to be compared
 80 of those fields can have a limited set of values between 1 and 1k (ex: compare: height vs height; range: age vs min/max age; regex: name starts with, etc)
I need to know what items match which of the rules, and I need to run this as many times per second as possible.
 With simple brute force, i would get 1x10^12 checks.
 Since each column in my data sets have a limited number of values, if I build a trie I get 9.9*10^6 checks to match all rules, worst case scenario (99 columns * 100k items)
 But if I would compare a trie vs a trie, I would get something like 99x10^3 checks (1k values * 99 columns)
Anyone know the name of the algorithm that compares two tries?

find number of nonoverlaping pairs of arrays
The problem is to count number of nonoverlaping pairs of arrays from given set of arrays.
For example given,
a = [1,2,3] b = [2,3,4] c = [6,7,8] d = [7,10,20]
The following pairs are nonoverlaping (a,c), (a,d), (b,c), (b,d) so the answer to this instance of problem is 4
I have an n^2 solution which computes intersection of every array with every other array, and increments the count if the intersection is empty set.
Is there an efficient way to solve this? (better than n^2)

ADDREV SPOJ Time
I am trying to solve the ADDREV problem on SPOJ. However, I am encountering this issue where I get RUN Time error. I have a feeling that the loop could be optimised. Here is the question to the problem.
The input consists of N cases (equal to about 10000). The first line of the input contains only positive integer N. Then follow the cases. Each case consists of exactly one line with two positive integers separated by space. These are the reversed numbers you are to add.
Output For each case, print exactly one line containing only one integer  the reversed sum of two reversed numbers. Omit any leading zeros in the output.
using namespace std; int reversedNumber=0,remainder; int reverse(int n){ while(n != 0) { remainder = n%10; reversedNumber = reversedNumber*10 + remainder; n /= 10; } return reversedNumber; } int main() { // your code goes here int a, revsum=0; cin >>a; while (true){ revsum=revsum+reverse(a); } return reverse(revsum); return 0; }

How to define constant in SASOPTPY
I'm doing optimization in python3.6 using sasoptpy.My objective function looks like z=(ap+qb)*c. where all of these variables are columns of a data frame. a,p,q,b are my variables & c is constant. I'm defining a,b,p,q in following way
va=m.add_variable(a, name='va',lb=LB2,ub=UB2)
. But not sure how to define constant c. Initially I triedc=data['z']
& use it in my objective functionm.set_objective((a*p+q*b)*c)
But got error messageWARNING: An invalid type is passed to create an Expression: KeyError: 'CONST'
I also tried
c=data['z'].values
but again same error. Can you help me to correct the issue 
Multidimensional Variable with Different index length in Pyomo or Gurobi
I want to solve an optimization problem in Python. I am trying to define a variable x_{g,h} where the index g belongs to the set G and the index h belongs to the set H(g), i.e., the set of indices h varies for different indices g. Is there any way to define variable x with these indices in Pyomo or GurobiPython?
In Pyomo, I tried to define it in a loop, like
for g in G: for h in H(g): model.x = Var(g,h)
I got this error:
TypeError: 'int' object is not iterable.
I appreciate any help or comment!

Is there anyone have material available for ASO(App Store Optimization) Android?
I am an android developer, But I don't know anything about ASO. So I need some help or resources which helps me to learn ASO. I want to learn it like professional personal.
Thanks

When reinserting into queue  Huffman Code
Example
3 2 5 5 a b c d
Joining first two
5  5 5 3 2  c d a b 
I have to put the new tree of five into the queue Am I obligated to put it in the end like this:
5 5 5 c d / \ 3 2 a b
Or can I put it in the beginning:
5 5 5 3 2 c d a b
Or even in the middle of 'c' and 'd'
Is it my choice or is there a rule?

How to traverse tree for making binary code from a HuffmanTree?
I'm trying to make binary code from a Huffman tree. I am stuck at tree traversal. To traverse tree and print code for every character frequency, I've written following code.
def printCode(node,prefix=""): if node.left: prefix = prefix+"0" printCode(node.left,prefix) elif node.right: prefix = prefix+"1" printCode(node.right,prefix) elif node.internal ==False: print(node.data,prefix) printCode(node,prefix="") #need change
Here is the necessary explanation: If a node is not an internal node(node.internal=False) then it is a leaf, and at this point of traversal I'm printing the code with character frequencies. But i'm unable to go back to the previous internal node and continue with another branch of tree that has not been traversed yet. So this code is ended with only returning binary code for the first leaf of the tree.
The every node of tree is created with following code:
class Node: def __init__(self,data,internal=False): self.data = data #frequency of a char self.internal = internal self.left = None self.right = None

Huffman Tree algorithm in c application
Given an input number n and a serial input sentences, please implement the Huffman algorithm then output the Huffman code of each symbol and its compression ratio.
While building the Huffman tree by bottomup method, please follow the rules below:
First, count the frequency of each character in all input sentences ordered by A to Z and then a to z
(Ignoring ‘ ’, ‘.’, ‘,’, ‘~’, and so on).
Second, put the counted frequencies to a priority queue in ascending order (if more two different characters have the same frequencies, order them by A to Z and then a to z). In this priority queue, the smaller frequencies get the higher priorities. So, pop the highest two priorities in this queue. The highest one is a leftchild, and the second highest is a rightchild. Sum them up as a new node and add it to the queue. Do it repeatedly and finally, you’ll get a unique Huffman tree. The values of left branches are 0, and the values of right branches are 1.
The input format:
First input are amount of the input sentences. Starting from the second line, each input sentence is smaller than 1000 characters.All the inputs end with a new line “\n”.
The output format:
Show the Huffman code of each symbol and a “\n” at the end.
The output ordered by the shortest Huffman code to the longest (A to Z and then a to z if having the same length).
Calculate its compression ratio rounded off to the second decimal place.
The input and output like this huffman tree
How to I do this in c language?