class Crosstab {

    public static Crosstab merge(Crosstab left, Crosstab right) {
        Crosstab crosstab = new Crosstab();

        Set<String> rowTags = new HashSet<>(left.rowTags);
        rowTags.addAll(right.rowTags);

        Set<String> colTags = new HashSet<>(left.colTags);
        colTags.addAll(right.colTags);

        crosstab.rowTags = new ArrayList<>(rowTags);
        crosstab.colTags = new ArrayList<>(colTags);

        int rowLen = crosstab.rowTags.size();
        int colLen = crosstab.colTags.size();
        crosstab.data = new long[rowLen][colLen];

        //merge data
        int i = 0;
        for (String row : crosstab.rowTags) {
            int j = 0;
            for (String col : crosstab.colTags) {
                long tmp = 0;
                if (left.rowTags.contains(row) && left.colTags.contains(col)) {
                    tmp += left.data[left.rowTags.indexOf(row)][left.colTags.indexOf(col)];
                }
                if (right.rowTags.contains(row) && right.colTags.contains(col)) {
                    tmp += right.data[right.rowTags.indexOf(row)][right.colTags.indexOf(col)];
                }
                crosstab.data[i][j] = tmp;
                j++;
            }
            i++;
        }

        return crosstab;
    }

}