UVA Problem 12646 – Zero or One Solution

UVA Problem 12646 – Zero or One Solution:


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

Solving Technique:

The input is all possible combination of 3 bits. If A, B, C are thought as bits then possible binary combination are,

000 \rightarrow * \\  001 \rightarrow C \\  010 \rightarrow B \\  011 \rightarrow A \\  ---- \\  100 \rightarrow A \\  101 \rightarrow B \\  110 \rightarrow C \\  111\rightarrow * \\

This problem can be solved in multiple ways such as using string, checking equality, performing xor equality etc. My code below is exactly same as equality checking but it perform xor operation between them.

XOR Table,

\begin{tabular}{l*{6}{c}r}  X & Y & X XOR Y \\ \hline  0 & 0 & 0 \\  0 & 1 & 1 \\  1 & 0 & 1 \\  1 & 1 & 0 \\  \end{tabular}
 

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. Please compile with c++ compiler as some of my codes are in c and some in c++.


More Inputs of This Problem on uDebug.


Input:

1 1 0
0 0 0
1 0 0

 


Output:

C
*
A

Code:

/**
 * Author:    Asif Ahmed
 * Site:      https://quickgrid.wordpress.com
 * Problem:   UVA 12646 - zero or one
 * Technique: XOR Bits to check equality.
 */

#include<stdio.h>


int main(){

    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);


    int A, B, C;
    while( scanf("%d%d%d", &A, &B, &C) == 3 ){

        if( !( A ^ B ) && !( A ^ C ) )
            putchar('*');
        else{
            if( A ^ B ){
                if( B ^ C )
                    putchar('B');
                else
                    putchar('A');
            }
            else
                putchar('C');
        }
        putchar('\n');
    }


    return 0;
}

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