Google Code Jam Moons and Umbrellas Runtime Error

I keep getting a runtime error in the Moons and Umbrellas problem in the google code jam
Here is my code:
indent preformatted text by 4 spaces
import java.util.Scanner;

public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);

    int n = in.nextInt();
    
    for(int i = 0; i < n; i ++) {
        int x = in.nextInt();
        int y = in.nextInt();
        String line = in.next();
        char[] arr = line.toCharArray();
        int sum = 0;
        for(int j = 0; j < arr.length; j ++) {
            if(arr[j] == 'C') {
                if(j + 1 < arr.length) {
                    if(arr[j + 1] == 'J') {
                        sum += x;
                    }
                }
            } else if(arr[j] == 'J') {
                if(j + 1 < arr.length) {
                    if(arr[j + 1] == 'C') {
                        sum += y;
                    }
                }
            } else {
                String p = String.valueOf(arr);
                if(!p.contains("C") && !p.contains("J")) {
                    for(int m = 0; m < arr.length; m ++) {
                    	arr[m] = 'C';
                    }
                }
                if(j > 0 && j < arr.length - 1 && arr[j - 1] != '?' && arr[j + 1] != '?') {
                    if(arr[j - 1] == arr[j + 1]) {
                        arr[j] = arr[j - 1];
                    } else {
                        arr[j] = 'C';
                    }
                    j -= 2;
                } else if(j == 0 && arr[1] != '?') {
                    arr[0] = arr[1];
                } else if(j == arr.length - 1) {
                    arr[arr.length - 1] = arr[arr.length - 2];
                } else if(arr[j + 1] != '?') {
                    arr[j] = arr[j + 1];
                }else if(j > 0 && arr[j - 1] != '?') {
                    arr[j] = arr[j - 1];
                } else if(arr[j + 1] == '?') {
                    int start = j;
                    int end = j;
                    while(arr[end] == '?') {
                        end ++;
                    }
                    for(int m = start; m < end; m ++) {
                        arr[m] = arr[end];
                    }
                }
            }
        }
        System.out.println("Case #" + (i + 1) + ": " + sum);
    }
    in.close();
}

}

The code passes all the sample test cases and also works fine when I use it on Eclipse.

Can you help me with reversort engineering a little?

Why you all are asking the help for a question from ongoing contest?

I have never asked like this anytime but this question is just giving headache. I have spent half a day on this but not able to figure out what is wrong. I don’t want to ask but I am also at the same time depressed about this question.

We can discuss.

Please help me a little bit with a hint or something with reversort engineering question. I have almost all cases on my system and it is working fine but then also getting WA verdict. What can be wrong here?

aren’t you accessing an invalid position when the string is just “?” ?

Well I put that there because we know at that point that the character is “?” and if the character after it isn’t a “?”, I wanted to change it to the “J” or “C” that was in front of it.

T = int(input())
if(T>=1 and T<=100):
pass
else:
raise(“Test Case Out of Bound”)

RES = []
for i in range(T):
temp = list(input().split())
X = int(temp[0])
Y = int(temp[1])
S = list(temp[2])
if(X>=1 and X<=100):
pass
else:
raise(“X value Out of Bound”)
if(Y>=1 and Y<=100):
pass
else:
raise(“Y Value Out of Bound”)
if(len(S)>=1 and len(S)<=1000):
for j in S:
if(j==‘C’ or j==‘J’ or j==’?’):
pass
else:
raise(“S contains invalid characters”)
else:
raise(“S Value Out of Bound”)
if(X>0 and Y>0):
if(S[0] == ‘?’):
prev = 0
else:
prev = 1
for j in range(len(S)):

        if(S[j]=='?'):
            if(prev == 0 and S[j+1] == 'C'):
                for k in range(j+1):
                    S[k]='C'
                prev = 1
            elif(prev == 0 and S[j+1] == 'J'):
                for k in range(j+1):
                    S[k]='J'
                prev = 1
            elif(prev == 1):
                S[j]=S[j-1]
S = ''.join([str(elem) for elem in S])
CJ = S.count('CJ')
JC = S.count('JC')
ans = (CJ * X) + (JC * Y)
RES.append(ans)

for i in range(len(RES)):
print(“Case #{}: {}”.format(i+1,RES[i]))


T = int(input())
if(T>=1 and T<=100):
    pass
else:
    raise("Test Case Out of Bound")

RES = []
for i in range(T):
    temp = list(input().split())
    X = int(temp[0])
    Y = int(temp[1])
    S = list(temp[2])
    if(X>=1 and X<=100):
        pass
    else:
        raise("X value Out of Bound")
    if(Y>=1 and Y<=100):
        pass
    else:
        raise("Y Value Out of Bound")
    if(len(S)>=1 and len(S)<=1000):
        for j in S:
            if(j=='C' or j=='J' or j=='?'):
                pass
            else:
                raise("S contains invalid characters")
    else:
        raise("S Value Out of Bound")
    if(X>0 and Y>0):
        if(S[0] == '?'):
            prev = 0
        else:
            prev = 1
        for j in range(len(S)):
            
            if(S[j]=='?'):
                if(prev == 0 and S[j+1] == 'C'):
                    for k in range(j+1):
                        S[k]='C'
                    prev = 1
                elif(prev == 0 and S[j+1] == 'J'):
                    for k in range(j+1):
                        S[k]='J'
                    prev = 1
                elif(prev == 1):
                    S[j]=S[j-1]
    S = ''.join([str(elem) for elem in S])
    CJ = S.count('CJ')
    JC = S.count('JC')
    ans = (CJ * X) + (JC * Y)
    RES.append(ans)

for i in range(len(RES)):
    print("Case #{}: {}".format(i+1,RES[i]))

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

void solve()
{
int n,x,y,cnt=0,ans=0;
string s;
cin>>x>>y>>s;
int i,j;
n=s.length();
for(i=0;i<n;i++)
{
if(s[i]==’?’)
cnt++;
}
if(cnt==n || cnt==n-1)
{
cout<<“0”;
return;
}
int k;
for(k=0;k<n-1;k++)
{
if(s[k]!=’?’)
break;
}
if(k==0)
k++;
i=k;
while(i<n-1)
{
if(s[i]==’?’)
{
if(s[i-1]!=s[i+1] && s[i+1]!=’?’)
{
if(s[i-1]==‘J’)
ans+=y;
else
ans+=x;
}
else if(s[i-1]!=s[i+1] && s[i+1]==’?’)
{
j=i+2;
while(s[j]==’?’ && j<n)
{
j++;
}
if(j==n)
{
break;
}
if(s[i-1]!=s[j])
{
if(s[i-1]==‘J’)
ans+=y;
else
ans+=x;
}
i=j;
}
}
i++;
}
for(i=0;i<n-1;i++)
{
if(s[i]==‘J’ && s[i+1]==‘C’)
ans+=y;
else if(s[i]==‘C’ && s[i+1]==‘J’)
ans+=x;
}
cout<<ans;

}

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
ll t;
cin>>t;
for(int j=1;j<=t; j++)
{
cout<<“Case #”<<j<<": ";
solve();
cout<<endl;
}
}