Created
April 26, 2016 08:15
-
-
Save PreSoichiSumi/151b4afc05beb146047e4e2b2ded1f96 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.io.InputStream; | |
import java.util.NoSuchElementException; | |
import java.util.Objects; | |
public class Main { | |
public static int N,X; | |
public static boolean arr[]; | |
public static void main(String[] args) { | |
FastScanner sc=new FastScanner(); | |
String str1=sc.next(); | |
String str2=sc.next(); | |
Node tree1= createTree(str1); | |
Node tree2=createTree(str2); | |
Node res= addTree(tree1, tree2); | |
System.out.println(res.toString()); | |
} | |
public static Node addTree(Node a,Node b){ | |
Node left = null; | |
Node right = null; | |
int label = a.label+b.label; | |
if (a.left!=null && b.left!=null){ | |
left = addTree(a.left, b.left); | |
} | |
if (a.right!=null && b.right!=null){ | |
right = addTree(a.right, b.right); | |
} | |
return new Node(label, left, right); | |
} | |
public static Node createTree(String input){ | |
int level=0; | |
int tmp=0; | |
if(Objects.equals(input, "")) | |
return null; | |
for(int i=0;i<input.length();i++){ | |
if(input.charAt(i)=='('){ | |
level++; | |
}else if(input.charAt(i)==')'){ | |
level--; | |
if(level==0){ | |
tmp=i; | |
break; | |
} | |
}else{} | |
} | |
Node left=createTree(input.substring(1, tmp));//left | |
int start=tmp+2; | |
int end=start; | |
while('0'<=input.charAt(end) && | |
input.charAt(end) <= '9'){ | |
end++; | |
} | |
String numStr=input.substring(start,end); | |
int number=Integer.valueOf(numStr); | |
Node right=createTree(input.substring(end+2,input.length()-1)); | |
return new Node(number, left, right); | |
} | |
public static class Node { | |
public int label; | |
public Node left; | |
public Node right; | |
public Node(int label, Node left, Node right){ | |
this.label = label; | |
this.left = left; | |
this.right = right; | |
} | |
public Node(){ | |
} | |
@Override | |
public String toString() { | |
String l = left!=null ? left.toString() : ""; | |
String r = right!=null ? right.toString() : ""; | |
return "("+l+")["+String.valueOf(label)+"]("+r+")"; | |
} | |
} | |
} | |
class ryori implements Comparable<ryori>{ | |
public int t,a; | |
public ryori(int t, int a) { | |
super(); | |
this.t = t; | |
this.a = a; | |
} | |
/*public int compareTo(ryori other){ | |
int value= other.a-this.a; | |
if(value==0){ | |
return this.t-other.t; | |
}else{ | |
return value; | |
} | |
}*/ | |
/*public int compareTo(ryori other){ | |
int value= this.t-other.t; | |
if(value==0){ | |
return other.a-this.a; | |
}else{ | |
return value; | |
} | |
}*/ | |
public int compareTo(ryori other){ | |
return other.a-this.a; | |
} | |
} | |
class FastScanner { | |
private final InputStream in = System.in; | |
private final byte[] buffer = new byte[1024]; | |
private int ptr = 0; | |
private int buflen = 0; | |
private boolean hasNextByte() { | |
if (ptr < buflen) { | |
return true; | |
}else{ | |
ptr = 0; | |
try { | |
buflen = in.read(buffer); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
if (buflen <= 0) { | |
return false; | |
} | |
} | |
return true; | |
} | |
private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;} | |
private boolean isPrintableChar(int c) { return 33 <= c && c <= 126;} | |
private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;} | |
public boolean hasNext() { skipUnprintable(); return hasNextByte();} | |
public String next() { | |
if (!hasNext()) throw new NoSuchElementException(); | |
StringBuilder sb = new StringBuilder(); | |
int b = readByte(); | |
while(isPrintableChar(b)) { | |
sb.appendCodePoint(b); | |
b = readByte(); | |
} | |
return sb.toString(); | |
} | |
public long nextLong() { | |
if (!hasNext()) throw new NoSuchElementException(); | |
long n = 0; | |
boolean minus = false; | |
int b = readByte(); | |
if (b == '-') { | |
minus = true; | |
b = readByte(); | |
} | |
if (b < '0' || '9' < b) { | |
throw new NumberFormatException(); | |
} | |
while(true){ | |
if ('0' <= b && b <= '9') { | |
n *= 10; | |
n += b - '0'; | |
}else if(b == -1 || !isPrintableChar(b)){ | |
return minus ? -n : n; | |
}else{ | |
throw new NumberFormatException(); | |
} | |
b = readByte(); | |
} | |
} | |
public int nextInt() { | |
if (!hasNext()) throw new NoSuchElementException(); | |
int n = 0; | |
boolean minus = false; | |
int b = readByte(); | |
if (b == '-') { | |
minus = true; | |
b = readByte(); | |
} | |
if (b < '0' || '9' < b) { | |
throw new NumberFormatException(); | |
} | |
while(true){ | |
if ('0' <= b && b <= '9') { | |
n *= 10; | |
n += b - '0'; | |
}else if(b == -1 || !isPrintableChar(b)){ | |
return minus ? -n : n; | |
}else{ | |
throw new NumberFormatException(); | |
} | |
b = readByte(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment