// UVa 1200 - A DP Problem
import java.util.Scanner;
public class Main {
public static class Expression {
public int coeficient;
public int constant;
}
private static Expression parse(String str){
Expression exp = new Expression();
exp.constant = 0;
exp.coeficient = 0;
int number = 0, sign = 1;
boolean x = false, numberSeen = false;
for (int i=0; i<str.length(); i++){
char ch = str.charAt(i);
if (ch >= '0' && ch <= '9'){
number = number*10 + (str.charAt(i) - '0');
numberSeen = true;
}
else if (ch == '-' || ch == '+'){
// reboot
if (numberSeen == false && i != 0)
number = 1;
if (x)
exp.coeficient += sign * number;
else
exp.constant += sign * number;
x = false;
number = 0;
numberSeen = false;
sign = ch == '+' ? 1 : -1;
}
else if (ch == 'x' || ch == 'X'){
x = true;
}
}
// reboot
if (numberSeen == false)
number = 1;
if (x)
exp.coeficient += sign * number;
else
exp.constant += sign * number;
return exp;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int cases = scanner.nextInt();
scanner.nextLine();
for (; cases > 0; cases--) {
String line = scanner.nextLine();
int equal = line.indexOf("=");
String left = line.substring(0, equal);
String right = line.substring(equal+1);
Expression leftExp = parse(left);
Expression rightExp = parse(right);
int coeficient = leftExp.coeficient - rightExp.coeficient;
int constant = rightExp.constant - leftExp.constant;
if (coeficient == 0 && constant == 0)
System.out.println("IDENTITY");
else if (coeficient == 0)
System.out.println("IMPOSSIBLE");
else
System.out.println((int)Math.floor( ((double)constant) / coeficient ) );
}
}
}
Saturday, April 18, 2015
UVa 1200 - A DP Problem
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment