Monday, 30 November 2015

The Love-Letter Mystery Hacker Rank Problem Solution.

Problem Statement
James found a love letter his friend Harry has written for his girlfriend. James is a prankster, so he decides to meddle with the letter. He changes all the words in the letter intopalindromes.
To do this, he follows two rules:
  1. He can reduce the value of a letter, e.g. he can change d to c, but he cannot change c to d.
  2. In order to form a palindrome, if he has to repeatedly reduce the value of a letter, he can do it until the letter becomes a. Once a letter has been changed to a, it can no longer be changed.
Each reduction in the value of any letter is counted as a single operation. Find the minimum number of operations required to convert a given string into a palindrome.
Input Format
The first line contains an integer T, i.e., the number of test cases.
The next T lines will contain a string each. The strings do not contain any spaces.
Constraints
1T10
1 length of string 104
All characters are lower case English letters.
Output Format
A single line containing the number of minimum operations corresponding to each test case.
Sample Input
4
abc
abcba
abcd
cba
Sample Output
2
0
4
2
Explanation
  1. For the first test case, abc -> abb -> aba.
  2. For the second test case, abcba is already a palindromic string.
  3. For the third test case, abcd -> abcc -> abcb -> abca = abca -> abba.
  4. For the fourth test case, cba -> bba -> aba.
Solution:

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
string str;
int main(){

    int T;
    cin >> T;
    for(int t  = 0; t < T; t++){
        cin >> str;
        long long cnt = 0;
        /// if string size is odd you don't need to calculate middle value
        /// abc or cba calculate a and c u will find than minimum answer
        for(int i = 0,j = str.size()-1; i < str.size()/2 ;i++,j--)cnt+=abs(str[i]-str[j]);
        cout << cnt << endl;
    }
    return 0;
}

Taum and B'day Hacker Rank Problem Solution.

Problem Statement
Taum is planning to celebrate the birthday of his friend, Diksha. There are two types of gifts that Diksha wants from Taum: one is black and the other is white. To make her happy, Taum has to buy B number of black gifts and W number of white gifts.
  • The cost of each black gift is X units.
  • The cost of every white gift is Y units.
  • The cost of converting each black gift into white gift or vice versa is Z units.
Help Taum by deducing the minimum amount he needs to spend on Diksha's gifts.
Input Format
The first line will contain an integer T which will be the number of test cases.
There will be T pairs of lines. The first line of each test case will contain the values of integersB and W. Another line of each test case will contain the values of integers XY, and Z.
Constraints
1T10
0X,Y,Z,B,W109
Output Format
T lines, each containing an integer: the minimum amount of units Taum needs to spend on gifts.
Sample Input
5
10 10
1 1 1
5 9
2 3 4
3 6
9 1 1
7 7
4 2 1
3 3
1 9 2
Sample Output
20
37
12
35
12
Explanation
  • Sample Case #01:
    There is no benefit to converting the white gifts into black or the black gifts into white, so Taum will have to buy each gift for 1 unit. So cost of buying all gifts will be: 101+101=20.
  • Sample Case #02:
    Again, we can't decrease the cost of black or white gifts by converting colors. We will buy gifts at their original price. So cost of buying all gifts will be: 52+93=10+27=37.
  • Sample Case #03:
    We will buy white gifts at their original price, 1. For black gifts, we will first buy white one and color them to black, so that their cost will be reduced to 1+1=2. So cost of buying all gifts will be: 32+61=12.
  • Sample Case #04:
    Similarly, we will buy white gifts at their original price, 2. For black gifts, we will first buy white one and color them to black, so that their cost will be reduced to 2+1=3. So cost of buying all gifts will be: 73+72=35.
  • Sample Case #05: We will buy black gifts at their original price, 1. For white gifts, we will first black gifts worth 1 unit and color them to white with another 2 units, so cost for white gifts is reduced to 3 units. So cost of buying all gifts will be: 31+33=3+9=12.

Solution:

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    unsigned long long T,b,w,x,y,z,ans;
    cin >> T;
    for(int i = 1; i <= T; i++){
        cin >> b >> w;
        cin >> x >> y >> z;
        ans = (b * min(x,y+z)) + (w * min(y,x+z));
        cout << ans << endl;
    }
    return 0;
}