TJU Problem 2501 Solution

Score, a relatively simple problem in the sense that it’s mainly tracing through a string… I solved this one in Java just because it seemed a little more challenging, even though once you use .toCharArray() the difference between java and C++ goes out the window… I tried keeping it simple, after all it was a problem in a programming competition and I didn’t want to spend too much time on it… so here’s the way I solved it.

It’s not necessarily the pretties code you’ll ever see, but it works.

Enjoy!

/*
*  TJU Problem 2501: Score
*  http://acm.tju.edu.cn/toj/showp2501.html
*/

import java.util.Scanner;

//gotta rename it main.java to submit...
public class p2501 {

    public static void main (String args[]){
        Scanner scn = new Scanner(System.in);
        final int cases = Integer.parseInt(scn.nextLine());
        for(int i = 0; i <= cases; i++){
            int finalAnswer = 0;
            int counter = 0;
            final char[] string = scn.nextLine().toCharArray();
            if(string[0] == 'O'){
                counter++;
                finalAnswer= finalAnswer + counter;
            }
            for(int j = 1; j< string.length; j++){
                if(string[j] == 'O' && string[j-1] == string[j])
                    counter++;
                else if(string[j] == 'O')
                    counter = 1;
                else
                    counter = 0;
                finalAnswer= finalAnswer + counter;
            }
            System.out.println(finalAnswer);
        }
    }
}
									

Leave a Reply

Your email address will not be published. Required fields are marked *