(UVA) Magic Square Palindromes - Solution

Link to the problem: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2162

The solution below tries to find out if the entry is palindrome. Then, if this condition is fulfilled, it is checked it the sentence can be allocated in a matrix NxN. Finally, we check if the sentence can be read from the table in the given four different ways. 


import java.io.*;
import java.util.*;

class Main {
    public HashSet<Character> notValid;
    public HashSet<String> string;
    public char[][] matrix;
    
    public void initSet() {
        notValid.add('.');
        notValid.add(' ');
        notValid.add(',');
        notValid.add('?');
        notValid.add('!');
        notValid.add('(');
        notValid.add(')');
    }
    
    public void check(int start, int end, boolean lateral) {
        StringBuilder sb = new StringBuilder();
        for (int i = start; i < end; i++) {
            for (int j = start; j < end; j++) {
                if (lateral) {
                    sb.append(matrix[j][i]);
                }
                else {
                    sb.append(matrix[i][j]);
                }
            }
        }
        string.add(sb.toString());
        sb = sb.reverse(); // consider the reverse (starting from the end)
        string.add(sb.toString());
    }
    
    public void process() throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        notValid = new HashSet<>();
        initSet();
        
        String line = br.readLine();
        int numTests = Integer.parseInt(line);
        for (int i = 0; i < numTests; i++) {
            line = br.readLine();
            
            int discard = 0;
            boolean palindrome = true;
            for (int j = 0, k = line.length()-1; j <= k; j++, k--) {
                if (j == k && notValid.contains(line.charAt(j))) {
                    discard++;
                    break;
                }
                while (notValid.contains(line.charAt(j))) { // character not valid
                    discard++;
                    j++;
                }
                while (notValid.contains(line.charAt(k))) { // character not valid
                    discard++;
                    k--;
                }
                if (line.charAt(j) != line.charAt(k)) {
                    palindrome = false;
                    break;
                }
            } 
            
            System.out.println("Case #" + (i+1) + ":");
            int sqrt = (int)Math.sqrt(line.length()-discard);
            if (sqrt*sqrt != line.length()-discard || !palindrome) {
                System.out.println("No magic :(");
            }
            else {
                int index = 0;
                matrix = new char[sqrt][sqrt];
                // transfer the sentence to a matrix
                for (int j = 0; j < sqrt; j++) {
                    for (int k = 0; k < sqrt; k++) {
                        while (notValid.contains(line.charAt(index))) {
                            index++;
                        }
                        matrix[j][k] = line.charAt(index);
                        index++;
                    }
                }
                
                string = new HashSet<>(); // check if the sentence is read in the same way
                check(0, sqrt, false);
                check(0, sqrt, true);
                
                if (string.size() > 1) {
                    System.out.println("No magic :(");
                    break;
                }
                System.out.println(sqrt);
            }
        }            
                       
        return;
    }
    
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main m = new Main();
        m.process();
        
        System.exit(0);
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução