UVA Problem 1585 – Score Solution

UVA Problem 1585 – Score Solution:


Click here to go to this problem in uva Online Judge.

Solving Technique:

Easy problem.  Run time 0.009 s ( 9 ms ), Rank 439.

We are given a string with only ‘O’ and ‘X’ characters. Also it is specified string length is not bigger than 80 character and there won’t be any space in between characters.

The way to calculate the score is,

OOXXOXXOOO
1200100123 /* 1+2+0+0+1+0+0+1+2+3 = 10 */

We count up until we encounter an ‘X’. Then we reset count to ZERO. Before reset we keep a sum of all previous counts. 

Important:  Be sure to add or print a new line after each output unless otherwise specified. The outputs should match exactly because sometimes even a space character causes the answer to be marked as wrong answer.


Input:

5 
OOXXOXXOOO 
OOXXOOXXOO 
OXOXOXOXOXOXOX 
OOOOOOOOOO 
OOOOXOOOOXOOOOX

Output:

10 
9 
7 
55 
30

Code:

/*
 * @author  Quickgrid ( Asif Ahmed )
 * @link    https://quickgrid.wordpress.com
 */

#include<stdio.h>
static char s[128];

int main(){
    register unsigned n, i;

    scanf("%u", &n);

    while (n--){
        scanf("%s", s);
        unsigned sum = 0, val = 0;

        for (i = 0; s[i]; ++i){

            /**
             * 79 is decimal ASCII 'O', Applying XOR Check to see if they are equal
             * If two numbers are same then val is set to 0, otherwise increment val
             */
            (s[i] ^ 79) ? val = 0: ++val;

            sum += val;
        }

        printf("%u\n", sum);
    }
    return 0;
}

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s