Skip to content

Instantly share code, notes, and snippets.

@joennlae
Created March 21, 2017 09:02
Show Gist options
  • Save joennlae/309dd9f3d6a202d8d28849150cbff83e to your computer and use it in GitHub Desktop.
Save joennlae/309dd9f3d6a202d8d28849150cbff83e to your computer and use it in GitHub Desktop.
public static String decrypt(String s) {
StringBuffer res = new StringBuffer();
for( int i = 0; i < s.length(); i++){
res.append((char)(s.charAt(i) - 3));
}
return res.toString();
}
public class KD {
/**
* Parse a "Klammerdarstellung" (KD) of a tree.
*
* <ul>
* <li>An empty tree is encoded as '-'.</li>
* <li>A node is encoded as an uppercase letter, i.e. everything accepted by {@link Character#isUpperCase(char)}.</li>
* <li>Children are appended to the father as a ','-separated list of nodes enclosed in round brackets.</li>
* </ul>
*
* @param kd Tree encoded in KD
* @throws ParseException if the given String is not a valid KD of a tree.
*/
static int counter = 0;
static String input = "";
public static void parse(String kd) throws ParseException
{
counter = 0;
input = kd;
baum();
if(counter < input.length()) throw new ParseException("nicht bsi ans ende", counter);
}
private static void baum() throws ParseException{
knoten();
if(outOfBoundCheck() && (char)(input.charAt(counter))=='('){
counter ++;
nachfolger();
if(outOfBoundCheck() && (char)(input.charAt(counter))==')'){
counter++;
}
else throw new ParseException("Missing closing bracket", counter);
}
}
private static boolean outOfBoundCheck(){
return input.length()>counter;
}
private static void nachfolger() throws ParseException{
baum();
while(outOfBoundCheck() && (char)(input.charAt(counter)) == ','){
counter++;
baum();
}
}
private static void knoten() throws ParseException{
if(outOfBoundCheck() && (char)(input.charAt(counter)) <= 'Z' && (char)(input.charAt(counter)) >= 'A' || outOfBoundCheck() && (char)(input.charAt(counter)) == '-' && counter > 0){
counter ++;
}
else throw new ParseException("Nicht erlaubtes Knotenzeichen", counter);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment