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;
}

UVA Problem 12854 – Automated Checking Machine Solution

UVA Problem 12854 – Automated Checking Machine Solution:


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

Solving Technique:

For This problem I tried many times but best I could get was rank 116 and run time 0.009 s ( 9 ms ). This is really bad for a very simple code.

The logic is simple we are given two strings ( or, 10 integers ) . If the character in the same index / position of the two strings are different we print Y. Otherwise if the characters are same we print N.

Since I know that there will 5 and 5 total of 10 characters each time. So I used integer to take input.

Now I used XOR between to integers because applying XOR to two integers if they are equal then we get 0. Otherwise we get a 1. XOR table,

a b a^b
0 0  0
0 1  1
1 0  1
1 1  0

So that is what I need if they are same i get 0 and else I get 1. Using this logic I check all 10 values. Then use ternary operation to print Y or, N.

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:

1 1 1 1 1
0 0 0 0 0
1 1 0 1 0
0 0 1 0 1
1 0 0 1 0
1 0 1 1 0

Output:

Y
Y
N

Code:

/*
 * @author Quickgrid ( Asif Ahmed )
 * @link https://quickgrid.wordpress.com
 * Problem: UVA 12854 - Automated Checking Machine
 */

#include<stdio.h>
int main(){
    register unsigned int a,b,c,d,e,f,g,h,i,j;
	while (scanf("%u%u%u%u%u%u%u%u%u%u", &a, &b, &c, &d, &e, &f, &g, &h, &i, &j) == 10){
        (a ^ f && b ^ g && c ^ h && d ^ i && e ^ j) ? printf("Y\n"): printf("N\n");
	}
	return 0;
}

UVA Problem 488 – Triangle Wave Solution

UVA Problem 488 – Triangle Wave Solution:


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

Solving Technique:

This is one of the easiest problems but requires some thinking. But getting accepted in not enough we also need to optimize the code to improve timing, memory, ranking.

Here I have solved this problem in two ways. The first took 0.042 s ( 42 ms ) and ranked 254 at the time of writing. The second solution took 0.122 s ( 122 ms ). Both codes take a different approach. The first one is a little bit cheating. Since running the second code I have found out that amplitude input is never greater than 9. Why? because running the second code it outputs garbage for any amplitude greater than 9 and it got accepted.

I will explain the first code logic here. The second ( slower ) code is commented for easier understanding. Please refer below for the solutions.

Since I know amplitude is never greater than 9 so I created constant string array in C. Also possible in C++ by using

const std::string a[] = {"1", "22", .....}

Now just print the predefined string array amplitude times and print in reverse order amplitude minus one times. That’s it. Please note, you should not print a blank line after last wave form. It quite easy,

if(times || n){
    printf("\n");
}

This works because both times and n variables are decremented and for the last loop when they become 0, the if condition does not execute. So for last waveform there is no newline.

Time to write a better solution and get that 1st rank :).

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:

2

4
5

3
2

Output:

1
22
333
4444
333
22
1

1
22
333
4444
333
22
1

1
22
333
4444
333
22
1

1
22
333
4444
333
22
1

1
22
333
4444
333
22
1

1
22
333
22
1

1
22
333
22
1

Code ( Faster ):

/*
 * Author: Quickgrid ( Asif Ahmed )
 * Site: https://quickgrid.wordpress.com
 * Problem: UVA 488 ( Triangle Wave )
 */
#include<stdio.h>

/* For this problem amplitude does not exceed 9, so you can cheat <span class="wp-smiley wp-emoji wp-emoji-smile" title=":)">:)</span> */
const char *a[] = {"", "1", "22", "333", "4444", "55555", "666666", "7777777", "88888888", "999999999"};

int main(){
    register unsigned n, i, j, k;
    scanf("%u", &n);

    while(n--){
        unsigned amp, times;
        scanf("%u%u", &amp, &times);

        while(times--){
            for(i = 1; i < amp; ++i)
                /* Just print the predefined strings */
                printf("%s\n", a[i]);

            for(k = i; k; --k)
                printf("%s\n", a[k]);

            if(times || n)
                printf("\n");
        }
    }
    return 0;
}

Another Solution ( Slower ):

This code also solves the problem but relatively slower. It took 0.122 s for my run. Still have look to learn something new. Please refer to this code for a better explanation of C array of strings.

/*
 * Author: Quickgrid ( Asif Ahmed )
 * Site: https://quickgrid.wordpress.com
 * Problem: UVA 488 ( Triangle Wave )
 */

#include<stdio.h>
#include<string.h>

int main(){
    register unsigned n, i, j, k;
    scanf("%u", &n);

    while(n--){
        unsigned amp, times;
        scanf("%u%u", &amp, &times);

        while(times--){
            /* Allocate memory for string array, allocates memory for string count */
            char **a = new char*[amp + 1];

            for(i = 1; i <= amp; ++i){
                /* Allocate memory for the string */
                a[i] = new char[i + 1];
                /* Reset the array */
                memset(a[i], 0, sizeof a[i]);

                for(j = 0; j < i; ++j)
                    /* Populate the string with numbers */
                    a[i][j] = i + 48;

                /* Print the generated strings */
                printf("%s\n", a[i]);
            }

            for(k = amp - 1; k > 0; --k)
                /* Print the rest in reverse */
                printf("%s\n", a[k]);

            if(times || n)
                /* For the last loop do not print new line */
                printf("\n");
        }
    }
    return 0;
}

Another using C++:

This one is also accepted but slower. Here instead of printing i keep adding all things to one string and only print when program is terminated. Although this works and will work but it is not a very good idea.

/*
 * Author: Quickgrid ( Asif Ahmed )
 * Site: https://quickgrid.wordpress.com
 * Problem: UVA 488 ( Triangle Wave )
 */
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const string a[] = {"", "1", "22", "333", "4444", "55555", "666666", "7777777", "88888888", "999999999"};

int main(){
    register unsigned n, i, j, k;
    string output, total = "";
    scanf("%u", &n);

    while(n--){
        unsigned amp, times;
        scanf("%u%u", &amp, &times);
        output = "";

        while(times--){
            for(i = 1; i < amp; ++i)
                output += a[i] + "\n";
            for(k = i; k; --k)
                output += a[k] + "\n";
            if(times || n)
                output += "\n";
        }
        total += output;
    }

    cout << total;
    return 0;
}