## UVA Problem 444 ( Encoder and Decoder ) Solution

UVA Problem 444 ( Encoder and Decoder ) Solution:

Solving Technique:

Seems like a medium level problem ( at least for me ).

Here the input is either mix of characters (A-Z) or (a-z) or,

`!  ,  .  :  ;  ?`

These characters. Or, Integers only.

If the input is a decoded string such as, abc?.  We need to encode it. The encode method is to reverse the Ascii values. For example, If decoded string was input

```Input: abc?d
Just Convert to Ascii: 97989963100
Reverse the Converted Ascii: 00136998979 /*This is our output*/```

If the input was an encoded integer string, to decode it follow the procedure below,

```Input: 00136998979
Reverse the input: 97989963100
Convert to Character: abc?d /*This is our output*/```

Another important thing any characters from ‘d’ to ‘z’ has ASCII value of 100 or more. So In that case we need to reverse 3 characters to find its corresponding ASCII character ( in other words to decode properly ).

Also after encoding a string if there are 0’s in the front we should print them like in the example above.

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

Code Explanation:

The code below makes cin, cout as fast as or maybe even faster then scanf, printf. Reference code forces blog.

```std::ios_base::sync_with_stdio(false);
cin.tie(0);
```

It is also advised to use new line character instead of endl.

Input:

```abc
798999
Have a Nice Day !```

Output:

```998979
cba
332312179862310199501872379231018117927```

Code:

```/*
* @author Quickgrid ( Asif Ahmed )
*/

#include<iostream>
#include<stdio.h>

using namespace std;

int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);

int i, j, a;
string s;

while (getline(cin, s)){
/*
* Find out the length of string
*/
for (i = 0; s[i]; ++i);

/*
* Start processing the string/input backward
*/
for (j = i - 1; j >= 0;){
/*
* Check if the input is encoded integer. Also checking with s or the first character is enough since it can either be numbers or letters
*/
if (s >= '0' && s <= '9'){
/*
* Check if the character is of length 2 because No input below 100 Ascii value of (d) contains integer length 2
*/
if (s[j] != '1'){
/*
* Make the last digits 10s and 1st digit in 1s place. Ex: 79 = 7+(9*10) = 97
*/
a = (s[j-1] - '0') + (s[j] - '0') * 10;
/*
* Decrease the loop counter by two since we have processed two digits. Meaning the integer was 99 or below
*/
j -= 2;
}else{
/*
* Make the last digits 100s, middle 10s and 1st digit in 1s place. Ex: 101 = 1+(0*10)+(1*100) = 101
*/
a = s[j-2] - '0' + (s[j-1] - '0') * 10 + (s[j] - '0') * 100;
/*
* Decrease the loop counter by Three since we have processed Three digits. Meaning the integer was 100 or above
*/
j-=3;
}
/*
* Since the input was a number and we decoded it to a character now just print that character
*/
cout << (char)a;
}
/*
* If not encoded integer it is obviously a string
*/
else{
/*
* Store the ASCII integer value of the character
*/
a = s[j];

while (a){
/*
* Print the last integer digit of (a)
*/
cout << a % 10;
/*
*/
a /= 10;
}
/*
* Decrease the loop counter since One character is processed
*/
--j;
}
}
cout << endl;
}
return 0;
}
```

## UVA Problem 11428 ( Cubes ) Solution

UVA Problem 11428 ( Cubes ) Solution:

Solving Technique:

In this problem we are to find the value of “x” and “y”, given “N” from the formula below,

N = x– y3

If there are no solution just print “No solution” ( without quotes ).

One line that may be confusing is, “If there is more than one solution then output the one with smallest value of y”.

Looping from 1 to square root N is enough. Also an important statement is checking if “x” cube is greater than “N”. Otherwise we calculate we will get a negative result. Because if “x” cube is less than or equal “n”, then there is another “y” cube subtracted from it so it will become negative but we were given positive numbers.

``` for(x=1; x<=sqrt(n);){
if(x*x*x > n){
/*check for y code goes here*/
}
++x;
}
```

If “x” cube is greater than “N” then it may be a possible value of “x”. Now we need to calculate for “y” to make sure. In order to do that we loop “j” from 1 to x-1 ( We can’t loop till i otherwise x and y will be equal which result in 0. Also we loop from 1 because subtracting 0 doesn’t change anything ).

```/*reaches here only if x cube is greater than N*/

for(y=1; y<x;){
if(x*x*x - y*y*y == n){
works = 1;        /* set flag for breaking outer loop and printing x y */
break;            /* break inner loop since x cube minus y cube is equal to N, means we found the answer */
}
++y;
}
if(works) break;          /* break the outer loop since we found the answer we should loop anymore */
```

Now i check if “x” cube minus “y” cube is equal to N. In that case i use a flag set it as true ( 1 ) and break the loop. It is important that if the flag is true we exit the outer loop otherwise it’ll keep looping and will give us wrong answer.

That’s it now just print value of x y or, No solution.

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

Input:

```7
37
12
0```

Output:

```2 1
4 3
No solution```

### Code Without Optimization:

```/*
* Author: Quickgrid ( Asif Ahmed )
* Site: https://quickgrid.wordpress.com
*/

#include<stdio.h>
#include<math.h>

int main(){
int n;

while (scanf("%d", &n) && n){
int works = 0, x, y;

for (x = 1; x <= sqrt(n);){
if (x*x*x > n){
for (y = 1; y < x;){
if (x*x*x - y*y*y == n){
works = 1;
break;
}
++y;
}
if (works) break;
}
++x;
}

if (works){
printf("%d %d\n", x, y);
}else{
printf("No solution\n");
}
}
return 0;
}
```

### With Some Optimization:

```/*
* @author Quickgrid ( Asif Ahmed )
*/

#include<stdio.h>
#include<math.h>

int main(){
register unsigned x, y;
unsigned n, k;

while (scanf("%u", &n) && n){
unsigned works = 0;

for (x = 1, k = sqrt(n); x <= k;){
unsigned xcube = x * x * x;

if (xcube > n){
for (y = 1; y < x;){
if (xcube - y * y * y == n){
works = 1;
break;
}
++y;
}

if (works)
break;
}
++x;
}

if (works)
printf("%u %u\n", x, y);
else
printf("No solution\n");

}
return 0;
}
```

## UVA Problem 10235 ( Simply Emirp ) Solution

UVA Problem 10235 ( Simply Emirp ) Solution:

Solving Technique:

Rank 115, Run time 0.012 s ( 12 ms ).

I have solved this problem in two ways first one is optimized primality test and second one is Sieve of Eratosthenes.

Problem is simple with a is simple we need to check if it’s prime or not. If it is then is it emrip or just prime.

There is one critical example that wasn’t given, its given below. If a prime is reversed and the reversed number is also prime then it is emrip. Ex, 17 is prime and reverse of 17 is 71 is also prime, then 17 is emrip.

If the number is emrip we should print emrip instead prime.

Critical example: 383 is prime but not emrip. If the number is prime and reversed number is equal to original number then it is not emrip.

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

Code Explanation:

I’ve written a custom check prime function to check for primes since it’ll be called twice. If the number is prime then I reversed the number and checked if that number is also prime. Lastly I checked if the reversed number is prime and also reversed number not equal to original.

Critical Input:

```999
481184
373
998001
998857
753257
823455
999999
850058
78887
999983```

Output:

```999 is not prime.
481184 is not prime.
373 is prime.
998001 is not prime.
998857 is emirp.
753257 is prime.
823455 is not prime.
999999 is not prime.
850058 is not prime.
78887 is prime.
999983 is emirp.```

### Code:

```/*
* @author  Quickgrid ( Asif Ahmed )
* Problem: UVA 10235 ( Simply Emrip optimized primality )
*/

#include<stdio.h>
#include<math.h>

unsigned int checkPrime(unsigned int n){
if (n == 2)
return 1;

if (n % 2 == 0 || n < 2)
return 0;

register unsigned int i;
unsigned int len = sqrt(n);

for (i = 3; i <= len; i += 2){
if (n % i == 0)
return 0;
}

return 1;
}

int main(){
register unsigned int n;
unsigned int p, prime, mod;

while (scanf("%u", &n) == 1){

/**
* check if number is prime
*/
p = checkPrime(n);

if (p){
/**
* Reverse prime
*/
prime = n;
mod = 1;

while (prime / mod){
mod *= 10;
}
mod /= 10;

unsigned int reversePrime = 0;
while (prime){
/**
* Here reversePrime is the reversed prime
*/
reversePrime += mod * (prime % 10);
prime /= 10;
mod /= 10;
}
/**
* Reverse prime end
*/

if (checkPrime(reversePrime) && reversePrime != n){  /* check if reverse is prime but also check the reversed number does not match the original */
printf("%u is emirp.\n", n);
}else{
printf("%u is prime.\n", n);
}
}else{
printf("%u is not prime.\n", n);
}
}
return 0;
}
```

### Simply Emrip with Sieve of Eratosthenes:

```/*
* @author  Quickgrid ( Asif Ahmed )
* Problem: UVA 10235 ( Simply Emrip Sieve of Eratosthenes)
*/

#include<stdio.h>
#include<math.h>

#define N 1000000

/**
* Since value can be up to 1000000
*/
unsigned int *primes = new unsigned int[N];

unsigned int *SieveOfEratosthenes(unsigned int n){
register unsigned int i = 2;

for (; i <= n; ++i)
primes[i] = 1;

primes = primes = 0;
unsigned int len = sqrt(n);

for (i = 2; i <= len; ++i){
if (primes[i]){
for (unsigned int k = i * i; k <= n; k += i)
primes[k] = 0;
}
}
return primes;
}

int main(){
register unsigned int n;
unsigned int prime, mod;

/**
* Pre calculate sieve for every value
*/
primes = SieveOfEratosthenes(N);

while (scanf("%u", &n) == 1){
if (primes[n]){                  /* Since sieve is calculated and stored on table just do a look up */

prime = n;
mod = 1;

while (prime / mod){
mod *= 10;
}
mod /= 10;

unsigned int reversePrime = 0;
while (prime){
reversePrime += mod * (prime % 10);
prime /= 10;
mod /= 10;
}

/**
* Again access table to see if it is a prime
*/
if (primes[reversePrime] && reversePrime != n)
printf("%u is emirp.\n", n);
else
printf("%u is prime.\n", n);
}else
printf("%u is not prime.\n", n);
}
return 0;
}
```

## UVA Problem 575 ( Skew Binary ) Solution

UVA Problem 575 ( Skew Binary ) Solution:

Solving Technique:

This one is very easy. We need to un-skew or convert to decimal the given input using the given formula.

Inputs are string/ character array because the inputs won’t fit on another data type. Plus it is easier with string.

It is already said after converting our inputs to it will fit in integer data type. Also unsigned can be used here since none of the input, output or calculation result will be zero.

We just need to calculate the formula and print the result.

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

Code Explanation:

Here taking an array of size 32 ( 31+1 for safety  ) will be enough. Code is commented for more look below.

Input:

```10120
200000000000000000000000000000
10
1000000000000000000000000000000
11
100
11111000001110000101101102000
0```

Output:

```44
2147483646
3
2147483647
4
7
1041110737```

Code:

```/*
* @author  Quickgrid ( Asif Ahmed )
*/

#include<stdio.h>

/**
* My custom recursive function to calculate power of two
*/
unsigned int twoPow(unsigned int n){
if(n == 0)
return 1;

/**
* this is our recursive counter we need to decrease it otherwise it will go to infinite loop
*/
n--;

/**
* simply keep multiplying 2 given times to get power of two
*/
return 2 * twoPow(n);
}

int main(){
char s;
unsigned int i,j,sum;   /* here i used unsigned since none of our input or outputs are negative */

while (gets(s)){
/*
* check if its 0 then exit, Maybe even deleting the NULL check will work for this problem
*/
if(s == '0' && s == '\0')
return 0;

sum = 0;

/**
* get the string length
*/
for(i = 0; s[i]; ++i);

for(j = 0; s[j]; ++j)
/**
* convert character to integer, then multiply 2 to the power n, then subtract 1 and add to sum
*/
sum = sum + (s[j] - 48) * (twoPow(i - j) - 1);

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

## UVA Problem 12614 ( Earn For Future ) Solution

UVA Problem 12614 ( Earn For Future ) Solution:

Solving Technique:

Like many other programming problem this tries to tangle us with its complex language and useless data to through us off the right path. I have provided three working solutions with same logic but with some optimizations.

This program simply requires us to find the max number among given inputs.

At first glance it may seem like we need to bitwise & ( and ) all given value for the result. But trying that doesn’t yield the right answer.

If you need to know what this problem is about:

The problem says bitwise operation will be performed on card numbers. But we do not need to perform bitwise operations. It is not our concern. But the problem also says that he have picked N cards. Now he needs to choose more cards to maximize his gain. The problem say, ” Please tell him the maximum amount he can win from these set of cards”. This means we obviously need to pick the biggest number because we want to maximize the amount ( performing bitwise & ( and ) with the biggest number instead of a smaller gives us a bigger number ).

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

Code Explanation:

The first line of input is the number of test cases ( n ). The next line of input the number of inputs (I used m in my code) to come. Following m lines inputs.

Here we do not need an array to store the variable. We can just calculate as the values are being given. For every test case I use the first card number as the max number, then I compare other inputs against it.

Input:

```6
2
0 1
2
3 5
3
0 1 2
4
0 0 9 1
5
0 99 1 9 5
6
8 0 7 5 8 5```

Output:

```Case 1: 1
Case 2: 5
Case 3: 2
Case 4: 9
Case 5: 99
Case 6: 8```

### Using some assumption:

```/*
* @author Quickgrid ( Asif Ahmed )
* Problem UVA 12614 ( Earn For Future )
*/

#include<stdio.h>

int main(){
register unsigned n, i;
/*
* Since we are told we won't get any negative numbers
*/
unsigned m, a, max, c = 1;

scanf("%u", &n);
while (n--){
/*
* Assuming m is at least 1, so i scan the first one as max
*/
scanf("%u%u", &m, &max);
/*
* Already one input taken, so decrease counter
*/
m -= 1;
while (m--){
scanf("%u", &a);
if (a > max)
max = a;
}
printf("Case %u: %u\n", c++, max);
}
return 0;
}
```

### Another one with custom scanning and some bit-wise tricks:

```/*
* Author: Quickgrid ( Asif Ahmed )
* Site: https://quickgrid.wordpress.com
* Problem: UVA 12614 ( Earn For Future )
*/

#include<stdio.h>

void Rfastscan(register unsigned &x){
register int c;
x = 0;
c = getchar();
for(;(c > 47 && c < 58); c = getchar())
x = (x << 1) + (x << 3) + c - 48;
}

void fastscan(int &x){
unsigned neg = 0;
register int c;
x = 0;
c = getchar();

if(c == '-'){
neg = 1;
c = getchar();
}

for(;(c > 47 && c < 58); c = getchar())
x = (x<<1) + (x<<3) +c -48;
if(neg)
x = ~x+1;   /* 2's complement */
}

int main(){
register unsigned n, m, i, c = 1;
int a, max;

Rfastscan(n);
while(n--){
Rfastscan(m);
fastscan(max);

for(i=1; i<m; ++i){
fastscan(a);
if(a > max)
max = a;
}
printf("Case %u: %u\n", c++, max);
}
return 0;
}
```

## UVA Problem 12541 ( Birthdates ) Solution

UVA Problem 12541 ( Birthdates ) Solution:

Solving Technique:

Rank 363, Run time 9 ms ( 0.009 s ).

This problem seems easy at first glance but requires some thinking. There will be only two output.

First one will be the name of the youngest person ( The person whose was born after everyone ). Meaning his/her birth year, month, date is greater than others. Ex, a person born on 1991 is younger than person born on 1990.

Second one will be the name of the oldest person ( The person whose was born before everyone ). Meaning his/her birth year, month, date is less than others. Ex, a person born on 1990 is older than person born on 1991.

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

Code Explanation:

Since the name doesn’t exceed 15 letter so an array of size 16 ( i use 1 more for safety ) will suffice. Next there are 4 inputs each time. One is name string, the rest are integers such as date, month and year respectively.

Since the inputs are all related I used a struct to keep all data in a structure named person.

Now I use two more struct variable named young and old. By default I set both of them to first value of our structure. Now I compare if year is greater I set young to that struct variable in that loop. If less than I set old to the struct variable in that loop. If both of these are not true then I follow the same logic for month and date.

In the and the young and old struct variable get set to youngest and oldest struct variable based on logic.

Since I only need to print the youngest and oldest person name so, I just access our young and old structure with name property using dot operator and print them each on a new line.

Input:

```10
Machel 31 12 4999
Amar 27 9 1991
Kara 18 9 5001
Tom 29 1 1991
Priyanka 1 12 5001
Melissa 25 10 1991
Ping 16 10 5001
Amidala 10 1 1991
Xena 17 10 5001```

Output:

```Priyanka
Amidala```

Code:

```/*
* Author: Quickgrid ( Asif Ahmed )
* Site: https://quickgrid.wordpress.com
* Problem: UVA 12541 ( Birth dates  )
*/

#include<stdio.h>

struct person{
char name;
unsigned int date, month, year;
};

int main(){
register unsigned int n, i = 0;

scanf("%u", &n);

struct person p[n], maxp, minp;

for(; i<n; ++i){
scanf("%s%u%u%u", &p[i].name, &p[i].date, &p[i].month, &p[i].year);
}

maxp = p;    /*younger person*/
minp = p;    /*older person*/

for(i=0; i<n; ++i){
if(p[i].year > maxp.year)
maxp = p[i];

else if(p[i].year < minp.year)
minp = p[i];

else{
if(p[i].month > maxp.month && p[i].year >= maxp.year)
maxp = p[i];

else if(p[i].month < minp.month && p[i].year <= minp.year)
minp = p[i];

else{
if(p[i].date > maxp.date && p[i].month >= maxp.month && p[i].year >= maxp.year)
maxp = p[i];
else if(p[i].date < minp.date && p[i].month <= minp.month && p[i].year <= minp.year)
minp = p[i];
}
}

}

printf("%s\n%s\n", maxp.name, minp.name);

return 0;
}
```

## UVA Problem 12289 ( One Two Three ) Solution

UVA Problem 12289 ( One Two Three ) Solution:

Solving Technique:

Very very easy problem. It requires us to only print 1 or 2 or 3 based on input.

There are only 3 types of string (one, two, three) input and among them only one character is changed for each input. Also Their length is always the same.

So the very basic idea that comes to mind is any input with length of five must be three ( since length doesn’t change ). Now if that is not true it can either be one or two.

Code Explanation:

I took a different approach for my code. Instead of including another header file and calling a function to get the string length, I simply checked if the third position in the array ( array starts from zero ) is not a NULL character. If there is no NULL character in that position then I simply outputted 3. Why? because the string three does not contain a NULL character in position 3.

Now above is not true then I check if the string is one or two. If i only check for sub-strings ( part of strings ) in one and find any two characters of ‘o’, ‘n’, ‘e’ then it is definitely one. Then print 1. Else simply print 2.

Input:

```3
owe
too
theee```

Output:

```1
2
3```

Code:

```/**
* @author  Quickgrid ( Asif Ahmed )
*/

#include<stdio.h>

#define O 111
#define N 110
#define E 101

static char s;

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

while (n--){
scanf("%s", s);
if (s){
printf("3\n");
}else{
unsigned s0 = s;
unsigned s1 = s;
unsigned s2 = s;
if ((s0 == O && s1 == N) || (s1 == N && s2 == E) || (s0 == O && s2 == E))
printf("1\n");
else
printf("2\n");
}
}
return 0;
}
```

## UVA Problem 10324 ( Zeros and Ones ) Solution

UVA Problem 10324 ( Zeros and Ones ) Solution:

Solving Technique:

Here I have provided naive solution for this problem which is very slow. This code can be solved much much faster. I’ll update to it later.

This One was painful. Got Runtime Error a few times before solving this. Still my best time for this so far is 1.899 sec. My main problem was that it said the problem could end with both an EOF or a new line character. The main problem was new line. So I used getchar() ( found it here one of the best sites for contest programming ) and checked for EOF and string length in the string input while condition.

The problem is quite descriptive. It says our string or character array size can be 1000000The string will contain only ‘0’ and ‘1’ characters. Basically we are to find if the characters between the given interval min(i,j)  and max(i,j)Meaning check for the characters from the minimum number between i or j to the maximum number of i or j.

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

Code Explanation:

Here the input format is first take a string or character array ( of size 1000000 ) and next take an integer which is the number if inputs to come. Now loop that many times and take two integers ( i and j ) every loop.

I used a flag named works which is set to 1 by default and use this to check condition if the characters match or not.

Now i loop thorough the given interval ( i and jto check if the character in the given interval are same.

In the loop i check every character of the interval string with the first character of that given interval. Since all character need to be same to output is this code is valid.

Now in the loop if any character doesn’t match I set the flag ( works ) to 0, print No since all characters don’t match and break from loop immediately.

Next thing I check if flag is 1 then I print Yes.

Lastly the getchar()

Input:

```0000011111
3
0 5
4 2
5 9
01010101010101010101010101111111111111111111111111111111111110000000000000000
5
4 4
25 60
1 3
62 76
24 62
1
1
0 0```

Output:

```Case 1:
No
Yes
Yes
Case 2:
Yes
Yes
No
Yes
No
Case 3:
Yes```

Code Naive Solution ( Slow ):

```/**
* Author:  Quickgrid ( Asif Ahmed )
* Site:    https://quickgrid.wordpress.com
* Problem: UVA 10324 - Zeros and Ones
*/

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

#define N 1000000

static char s[N];

int main(){

int i;
int works, c = 1;
int n, a, b;

while(gets(s)){

scanf("%d", &n);
printf("Case %d:\n", c++);

while( n-- ){

works = 1;

scanf("%d%d",&a,&b);

if(a == b){
printf("Yes\n");
continue;
}

if(a > b){
for(i = b; i <= a; ++i){
if(s[i] != s[a]){
works = 0;
printf("No\n");
break;
}
}
}else{
for(i = a; i <= b; ++i){
if(s[i] != s[a]){
works = 0;
printf("No\n");
break;
}
}
}

if(works)
printf("Yes\n");

}
getchar();
}
return 0;
}
```