Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class Main {
- static class Node {
- String name;
- Node left;
- Node right;
- int childrenCount;
- long subtreeHash;
- public Node(String name) {
- this.name = name;
- }
- }
- long X = 99991;
- static class Tokenizer {
- String string;
- int at;
- public Tokenizer(String string) {
- this.string = string;
- this.at = 0;
- }
- public boolean hasNext() {
- return at < string.length();
- }
- public char peek() {
- return string.charAt(at);
- }
- public char poll() {
- return string.charAt(at++);
- }
- public String readName() {
- String result = "";
- while (hasNext() && Character.isLetter(peek())) {
- result += poll();
- }
- return result;
- }
- }
- private void solution() throws IOException {
- int ts = in.nextInt();
- while (ts-- > 0) {
- Tokenizer tokenizer = new Tokenizer(in.nextLine());
- Node head = readTree(tokenizer);
- precalc(head);
- StringBuilder result = new StringBuilder();
- Map<Long, Integer> cache = new HashMap<Long, Integer>();
- this.it = 1;
- solve(head, result, cache);
- out.println(result);
- }
- }
- int it;
- private void solve(Node head, StringBuilder result, Map<Long, Integer> cache) {
- if (cache.containsKey(head.subtreeHash)) {
- result.append(cache.get(head.subtreeHash));
- } else {
- cache.put(head.subtreeHash, it++);
- result.append(head.name);
- if (head.left != null) {
- result.append('(');
- solve(head.left, result, cache);
- result.append(',');
- solve(head.right, result, cache);
- result.append(')');
- }
- }
- }
- private void precalc(Node head) {
- if (head != null) {
- precalc(head.left);
- precalc(head.right);
- head.childrenCount = 1 + count(head.left) + count(head.right);
- head.subtreeHash = addToHash(0, head.name);
- if (head.left != null) {
- head.subtreeHash = addToHash(head.subtreeHash, "(");
- head.subtreeHash = addToHash(head.subtreeHash, head.left.subtreeHash, 10 * head.left.childrenCount);
- head.subtreeHash = addToHash(head.subtreeHash, ",");
- head.subtreeHash = addToHash(head.subtreeHash, head.right.subtreeHash, 10 * head.right.childrenCount);
- head.subtreeHash = addToHash(head.subtreeHash, ")");
- }
- }
- }
- private long addToHash(long hash, long otherHash, int length) {
- return hash * pow(X, length) + otherHash;
- }
- private long pow(long a, int k) {
- long res = 1;
- while (k > 0) {
- if ((k & 1) != 0) {
- res = res * a;
- }
- k >>= 1;
- a = a * a;
- }
- return res;
- }
- private long addToHash(long hash, String string) {
- for (char c : string.toCharArray()) {
- hash = hash * X + c;
- }
- return hash;
- }
- private int count(Node head) {
- if (head == null) {
- return 0;
- } else {
- return head.childrenCount;
- }
- }
- private Node readTree(Tokenizer tokenizer) {
- if (!tokenizer.hasNext()) {
- return null;
- } else {
- Node head = new Node(tokenizer.readName());
- if (tokenizer.hasNext() && tokenizer.peek() == '(') {
- tokenizer.poll();
- head.left = readTree(tokenizer);
- tokenizer.poll();
- head.right = readTree(tokenizer);
- tokenizer.poll();
- }
- return head;
- }
- }
- public void run() {
- try {
- solution();
- in.reader.close();
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- System.exit(1);
- }
- }
- private class Scanner {
- StringTokenizer tokenizer;
- BufferedReader reader;
- public Scanner(Reader reader) {
- this.reader = new BufferedReader(reader);
- this.tokenizer = new StringTokenizer("");
- }
- public String nextLine() throws IOException {
- tokenizer = new StringTokenizer("");
- return reader.readLine();
- }
- public boolean hasNext() throws IOException {
- while (!tokenizer.hasMoreTokens()) {
- String line = reader.readLine();
- if (line == null) {
- return false;
- }
- tokenizer = new StringTokenizer(line);
- }
- return true;
- }
- public String next() throws IOException {
- hasNext();
- return tokenizer.nextToken();
- }
- public int nextInt() throws IOException {
- return Integer.parseInt(next());
- }
- }
- public static void main(String[] args) {
- new Main().run();
- }
- Scanner in = new Scanner(new InputStreamReader(System.in));
- PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement