HI. Can you please help me figure out why i’m getting a WA for test set 2?
I got Ac for test set 1, and realised i need to use ‘long’ instead of ‘int’ for the 2nd test set. But still i’m getting WA. Please help.
Problem link : Kick Start - Google’s Coding Competitions
This is my code :
public class Solution {
static int MOD = 1000000000;
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int tt = sc.nextInt();
for(int i = 1;i <= tt;i++) {
solve(i);
}
}
public static void solve(int tt) {
String s = sc.next();
int n = s.length();
long prod[] = new long[n/2 + 1];
prod[0] = (long)1;
int iProd = 0;
//up-down, and left-right
long ud = 0;
long lr = 0;
for(int i = 0;i < n;i++) {
if(s.charAt(i) == 'N') {
//System.out.println("north " + prod[iProd] + " times");
ud = (ud - prod[iProd]) % MOD;
}
else if(s.charAt(i) == 'S') {
//System.out.println("south " + prod[iProd] + " times");
ud = (ud + prod[iProd]) % MOD;
}
else if(s.charAt(i) == 'E') {
//System.out.println("east " + prod[iProd] + " times");
lr = (lr + prod[iProd]) % MOD;
}
else if(s.charAt(i) == 'W') {
//System.out.println("west " + prod[iProd] + " times");
lr = (lr - prod[iProd]) % MOD;
}
else if(s.charAt(i) == '(') {
//continue;
//this is taken care in the next to next else block
}
else if(s.charAt(i) == ')') {
iProd--;
//System.out.println("times have decreased to " + prod[iProd]);
}
else {
String sx = s.substring(i, i+1);
int ix = Integer.parseInt(sx);
iProd++;
prod[iProd] = prod[iProd - 1]*(long)ix;
//System.out.println("times have increased to " + prod[iProd]);
}
}
long a = (MOD + 1 + lr) % MOD;
long b = (MOD + 1 + ud) % MOD;
System.out.print("Case #" + tt + ": ");
if(a == (long)0)System.out.print(MOD + " ");
else System.out.print(a + " ");
if(b == (long)0)System.out.print(MOD + " ");
else System.out.print(b + " ");
System.out.println();
}
}