1. Java, Simon Baumgartner



/*
* ACM Programming Contest
*
* Problem: 377 Cowculations
* Status: Accepted
* Run Time: 0.076
* Date: 2009-05-14
* Author: Simon Baumgartner
*/

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Main {

private static char[][][] TABLE_FOR_A = new char[][][]{
{{'V'}, {'U'}, {'C'}, {'D'}},
{{'U'}, {'C'}, {'D'}, {'V','U'}},
{{'C'}, {'D'}, {'V','U'}, {'U','U'}},
{{'D'}, {'V','U'}, {'U','U'}, {'C','U'}}
};

public static void main(String[] args) throws NumberFormatException, IOException {

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
// BufferedReader in = new BufferedReader(new FileReader("input.txt"));
int nrOfTables = Integer.parseInt(in.readLine());
System.out.println("COWCULATIONS OUTPUT");
for(int i=0; i<nrOfTables;i++){
String num1 = in.readLine();
String num2 = in.readLine();
String op1 = in.readLine();
String op2 = in.readLine();
String op3 = in.readLine();
String resultGiven = in.readLine();

num2 = process(op1, num1, num2);
num2 = process(op2, num1, num2);
String result = process(op3, num1, num2);

// fill up with 'V'
int x = 8 - result.length();
for(int j = 0; j < x; j++)
result = "V" + result;

if(result.equals(resultGiven)){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
System.out.println("END OF OUTPUT");
}

private static String process(String op, String num1, String num2) {
if(op.equals("A"))
return A(num1, num2);
if(op.equals("R"))
return R(num2);
if(op.equals("L"))
return L(num2);
if(op.equals("N"))
return num2;

throw new IllegalArgumentException("Unknown Operator: " + op);
}

private static String A(String num1, String num2){
String ret = "";
char carry = '*';
int maxLength = Math.max(num1.length(), num2.length());

for(int i = 0; i < maxLength; i++){
char a = '*';
char b = '*';

try{
a = num1.charAt(num1.length() - i - 1);
}catch(IndexOutOfBoundsException e){}
try{
b = num2.charAt(num2.length() - i - 1);
}catch(IndexOutOfBoundsException e){}

// if there is a carry
if(carry != '*'){
char oldCarry = carry;
carry = '*';
if(a != '*' && b != '*'){
char[] result = getResult(oldCarry, a);
a = result[0];
if(result.length == 2){
carry = result[1];
}
}else if(a != '*'){
char[] result = getResult(oldCarry, a);
ret = result[0] + ret;
if(result.length == 2){
carry = result[1];
}

}else if(b != '*'){
char[] result = getResult(oldCarry, b);
ret = result[0] + ret;
if(result.length == 2){
carry = result[1];
}
}
else{
ret = carry + ret;
}
}

// if we have a and b, get result
if(a != '*' && b != '*'){
char[] result = getResult(a, b);
ret = result[0] + ret;
if(result.length == 2){
carry = result[1];
}
// if we have only a or b, add it to the result
}else if(a != '*'){
ret = a + ret;
}else if(b != '*'){
ret = b + ret;
}
}
if(carry != '*')
ret = carry + ret;
return ret;
}

private static String R(String num2){
return "V" + num2.substring(0, num2.length()-1);
}


private static String L(String num2){
return num2 + "V";
}

/**
* helper, method, retrieve result from TABLE_FOR_A
* @param a
* @param b
* @return
*/
private static char[] getResult(char a, char b){
char[] ret = TABLE_FOR_A[getIndexFor(a)][getIndexFor(b)];
return ret;
}

/**
* helper method. maps V U C D to their index in TABLE_FOR_A
* @param a
* @return
*/
private static int getIndexFor(char a) {
switch (a) {
case 'V': return 0;
case 'U': return 1;
case 'C': return 2;
case 'D': return 3;
}
throw new IllegalArgumentException("No index for " + a);
}
}