Tag Archives: algorithms

UVA 12478 – Hardest Problem Ever (Easy)

So this was an interesting problem. You could have technically solved it manually but that would have taken too much time. If you didn’t mind at most 7 WA’s you could have just tried all 8 solutions one at a time and gotten it correct. I did it the long way; making my program solve it for me.

I used this string normalization technique I was ‘taught’ in college. It’s a neat little trick that I seem to use every chance I get. Pretty easy solution once you know you can map all permutations of a string to just one string!

import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

/**
 * 
 * @author Sanchit M. Bhatnagar
 * @see http://uhunt.felix-halim.net/id/74004
 * 
 */
public class P12478 {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);

    String[] grid = new String[] { "OBIDAIBKR", "RKAULHISP", "SADIYANNO", "HEISAWHIA", "IRAKIBULS", "MFBINTRNO", "UTOYZIFAH", "LEBSYNUNE", "EMOTIONNAL" };

    String[] originalNames = new String[] { "RAKIBUL", "ANINDYA", "MOSHIUR", "SHIPLU", "KABIR", "SUNNY", "OBAIDA", "WASI" };

    String[] names = new String[originalNames.length];
    int[] count = new int[names.length];

    for (int i = 0; i < names.length; i++) {
      names[i] = normalize(originalNames[i]);
    }

    // Check Horizontally
    for (int zz = 0; zz < 9; zz++) {
      for (int i = 0; i < 9; i++) {
        for (int j = i + 4; j <= 9; j++) { //Shortest name is 4 characters so we are checking for sub-strings of at least that length. We could also have capped the sub-string to at most 7 characters based on the names provided.
          String substring = normalize(grid[zz].substring(i, j));
          for (int k = 0; k < names.length; k++) {
            if (substring.equals(names[k])) {
              count[k]++;
            }
          }
        }
      }
    }

    // Check Vertically
    String[] grid2 = new String[grid.length];
    for (int j = 0; j < 9; j++) {
      char[] tmp = new char[9];
      for (int i = 0; i < 9; i++) {
        tmp[i] = grid[i].charAt(j);
      }
      grid2[j] = new String(tmp);
    }

    for (int zz = 0; zz < 9; zz++) {
      for (int i = 0; i < 9; i++) {
        for (int j = i + 4; j <= 9; j++) {
          String substring = normalize(grid2[zz].substring(i, j));
          for (int k = 0; k < names.length; k++) {
            if (substring.equals(names[k])) {
              count[k]++;
            }
          }
        }
      }
    }

    for (int i = 0; i < count.length; i++) {
      if (count[i] == 2) {
        out.println(originalNames[i]);
      }
    }

    out.close();
    sc.close();
  }

  // Normalizes all strings
  private static String normalize(String word) {
    char[] list = word.toCharArray();
    Arrays.sort(list);
    return new String(list);
  }
}

Edit: Further explanation. I’ve also updated the code to avoid the IndexOutOfBounds exception I was strangely using a try-catch for.

Firstly let us cover the normalize function. As you can tell by the code, it takes a word, splits it into a character array, sorts the character array and then converts it back into a string. Let us take some examples so that this is clear.

Take a word, let’s say: apple once you run it through the normalize function it will become aelpp. The same will happen for all permutations of apple (I won’t list them all) for example aplpe, ppael, leapp, etc. This allows us to quickly check if a particular permutation of a word is in the grid.

If this part is clear then we can just move on to the checking part.

We will go through the grid one row at a time (the code with the comment stating horizontal). Find all substrings within that line, normalize them and then compare them with the names array. If they are in the array then we increment a counter (named count). We do the same thing for the grid vertically.

Factors

A naive way of figuring out the number of factors of a particular number is to try every number from 1 up to the number itself.

public ArrayList getFactors(int N) {
  ArrayList factors = new ArrayList();
  for (int i=0; i if (N%i==0) {
      factors.add(i);
  }
  return factors;
}

We can do much better than this by noticing that for every number i that is a factor of N; the number N/i is also a factor of N! This means that we can change out O(N) algorithm to O(sqrt(N)) which is much much faster.

F(x) vs F(sqrt(X))
O(N) algorithm vs O(sqrt(N)) algorithm
public ArrayList getFactors(int N) {
  ArrayList factors = new ArrayList();
  int sqrt = (int) Math.sqrt(N);
  for (int i=0; i if (N%i==0) {
    factors.add(i);
  }
  if (sqrt * sqrt == N) {
    factors.add(sqrt);
  }
  return factors;
}

Save this code somewhere, I guarantee it’ll come in handy!