Saturday, December 13, 2014

TCHS SRM 56 250 pt - Strange Comparator solution

TCHS SRM 56 250 pt - Strange Comparator: http://community.topcoder.com/stat?c=problem_statement&pm=10010&rd=13525

TCHS SRM 56 250 pt - Strange Comparator editorial:  http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm56

TCHS SRM 56 250 pt - Strange Comparator solution:


#include <iostream>
#include <vector>

class StrangeComparator {
 public: std::vector <std::string> compareString(std::vector <std::string> a, std::vector <std::string> b) {
  int cnt=0, f=0;
  std::vector <std::string> v;
  for(int i=0; i<a.size(); i++, cnt=0, f=0) {
   if(a[i].length()!=b[i].length()) f=1;
   if(f==0) for(int j=0; j<a[i].length(); j++) if(a[i][j]!=b[i][j]) cnt++;   
   if(cnt>1 || f==1) v.push_back("No");
   else v.push_back("Yes");
  }
  return v;
 }
};



TCHS SRM 55 250 pt - Very Interesting Movie solution

TCHS SRM 55 250 pt - Very Interesting Movie: http://community.topcoder.com/stat?c=problem_statement&pm=9955&rd=13524

TCHS SRM 55 250 pt - Very Interesting Movie editorial:  http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm55

TCHS SRM 55 250 pt - Very Interesting Movie solution:


#include <iostream>
#include <vector>

class VeryInterestingMovie {
 public: int maximumPupils(std::vector <std::string> s) {
  int a=0, f=0;
  for(int i=0; i<s.size(); i++, f=0) for(int j=0; j<s[i].length(); j++) {
   if(s[i][j]=='Y' && f==0) a++, f=1;
   else if((s[i][j]=='Y' && f==1) || s[i][j]=='N') f=0;
  }
  return a;
 }
};

TCHS SRM 54 250 pt - Problem Setter solution

TCHS SRM 54 250 pt - Problem Setter: http://community.topcoder.com/stat?c=problem_statement&pm=9946&rd=13523

TCHS SRM 54 250 pt - Problem Setter editorial:  http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm54

TCHS SRM 54 250 pt - Problem Setter solution: 


#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>

class ProblemSetter {
 public: std::vector <int> chooseProblems(std::vector <int> d) {
  double dif, d1, d2, mi=1005;
  std::vector <int> v;
  std::sort(d.begin(), d.end());
  for(int i=0; i<d.size()-2; i++) {
   for(int j=d.size()-1; j>i+1; j--) {
    for(int k=i+1; k<d.size()-1; k++) {
     d1=d[k]-d[i], d2=d[j]-d[k];
     dif=std::abs(d1-d2);
     if(dif<mi) {
      mi=dif, v.clear();
      v.push_back(d[i]), v.push_back(d[k]), v.push_back(d[j]);
     }
    }
   }
  }
  return v;   
 }
};

for sorting vectors in C++: http://www.cplusplus.com/reference/algorithm/sort/

for clearing or removing elements from vector in C++: http://www.cplusplus.com/reference/vector/vector/clear/

for absolute value of a number in C++: http://www.cplusplus.com/reference/cmath/abs/

Friday, December 12, 2014

TCHS SRM 53 250 pt - DNAConstruction solution

TCHS SRM 53 250 pt - DNAConstruction: http://community.topcoder.com/stat?c=problem_statement&pm=9818&rd=13486

TCHS SRM 53 250 pt - DNAConstruction editorial:  http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm53

TCHS SRM 53 250 pt - DNAConstruction solution: 

 
#include <iostream>
#include <cstring>
#include <algorithm>

class DNAConstruction {
 public: int maxLength(std::string n) {
  int a=0, t=0, c=0, g=0, tot=0;
  for(int i=0; i<n.length(); i++) {
   if(n[i]=='A') a++;
   else if(n[i]=='T') t++;
   else if(n[i]=='C') c++;
   else if(n[i]=='G') g++;
  }
  tot=std::min(a, t) + std::min(c, g);
  return tot;
 }
};

for proper use of "min" and "max" in c++:

1 - http://www.cplusplus.com/reference/algorithm/max/

2 - http://stackoverflow.com/questions/1632145/use-of-min-and-max-functions-in-c


and for iterating through string in c++: http://stackoverflow.com/questions/1315041/how-can-i-iterate-through-a-string-and-also-know-the-index-current-position

TCHS SRM 52 250 pt - Tournament Judging solution

TCHS SRM 52 250 pt - Tournament Judging: http://community.topcoder.com/stat?c=problem_statement&pm=9822&rd=13485

TCHS SRM 52 250 pt - Tournament Judging editorial:  http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm52

TCHS SRM 52 250 pt - Tournament Judging solution: 

#include <iostream>
#include <cmath>
#include <vector>

class TournamentJudging {
 public: int getPoints(std::vector <int> rs, std::vector <int> cf) {
  int tot=0;
  double db;
  for(int i=0; i<rs.size(); i++) {
   db=(double)rs[i]/cf[i]; tot+=round(db);
  }
  return tot;
 }
};


for "round"ing to the nearest number in C++: http://www.cplusplus.com/reference/cmath/round/

TCHS SRM 51 250 pt - Missing Digits solution

TCHS SRM 51 250 pt - Missing Digits: http://community.topcoder.com/stat?c=problem_statement&pm=7920&rd=13484

TCHS SRM 51 250 pt - Missing Digits editorial:  http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm51

TCHS SRM 51 250 pt - Missing Digits solution:


#include <iostream>
#include <sstream>
#include <vector>

class MissingDigits {
 public: std::string isAllowed(std::vector <int> na, int r) {
  std::ostringstream ss;
  std::string s1, s2;
  ss<<r, s1=ss.str();
  for(int i=0; i<na.size(); i++) {
   std::ostringstream ss2;
   ss2<<i<<na[i], s2=ss2.str();
   if(s1.find(s2)!=std::string::npos) return "NO";
  }  
 return "YES";
 }
};
 
 
for "ostringstream" or to convert int to string in C++, see:  
http://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c
 
 
and for "find" substring in a string in C++, see: 
http://www.cplusplus.com/reference/string/string/find/

TCHS SRM 50 250 pt - Level one - Funny Birds solution

TCHS SRM 50 250 pt - Level one - Funny Birds: http://community.topcoder.com/stat?c=problem_statement&pm=9769&rd=13483

TCHS SRM 50 250 pt - Level one - Funny Birds editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm50

TCHS SRM 50 250 pt - Level one - Funny Birds solution: 


#include <iostream>

class FunnyBirds{
    public: int gameTime(int n) {
        int a=1, cnt=0;
        while(n) {
            while((a*(a+1)/2)<=n) a++;
            a--, n-=a*(a+1)/2, cnt+=a, a=1;
        }
        return cnt;
    }
};

Friday, August 22, 2014

TC SRM 630 div2 250 pt - Double Letter solution

TC SRM 630 div2 250 pt - Double Letter solution: 


#include <iostream>
#include <string>

class DoubleLetter {
    public: std::string ableToSolve(std::string s) {
        int flag=0, cnt=0;
        while(!flag) {
            for(int i=1; i<s.length(); i++) if(s[i-1]==s[i]) s.erase(i-1, 2);
            cnt++;
            if(!s.length()) flag=1;
            if(s.length()%2 || cnt==100) break;
        }
        if(flag) return "Possible";
        else return "Impossible";
    }
};

Monday, August 18, 2014

Hackerrank Ad Infinitum August 2014 - Stepping Stones Game solution

Hackerrank Ad Infinitum August 2014 - Stepping Stones Game: https://www.hackerrank.com/contests/infinitum-aug14/challenges/stepping-stones-game

Hackerrank Ad Infinitum August 2014 - Stepping Stones Game editorial: https://www.hackerrank.com/contests/infinitum-aug14/challenges/stepping-stones-game/editorial

Hackerrank Ad Infinitum August 2014 - Stepping Stones Game solution: 


#include <iostream>
#include <cstdio>
#include <cmath>

int main() {
    int t;
    long long int n, a;
    scanf("%d", &t);
    while(t--) {
        scanf("%lld", &n);
        if(floor(sqrt(1+8*n))==sqrt(1+8*n)) a=(sqrt(1+8*n)-1)/2, printf("Go On Bob %lld\n", a);
        else printf("Better Luck Next Time\n");
    }
    return 0;
}

note: The formular for the sum of the first 
n natural numbers is
n(n+1)2
A number x appears in this sequence, iff
n(n+1)2=xn2+n2x=0
has an non-negative integer solution. Since, this is a quadratic equation, you can simply compute the positive solution:
n=1+8x12
This is an integer number, iff 1+8x is a square and 21+8x1. You can see, that the latter is always true, if the former is true. So the answer is:
x occurs in this sequence, if 1+8x is a perfect square.

ref: http://math.stackexchange.com/questions/466649/how-do-i-test-if-a-number-x-is-a-sum-of-consecutive-natural-numbers

Hackerrank Ad Infinitum August 2014 - Reverse Game solution

Hackerrank Ad Infinitum August 2014 - Reverse Game: https://www.hackerrank.com/contests/infinitum-aug14/challenges/reverse-game

Hackerrank Ad Infinitum August 2014 - Reverse Game editorial: https://www.hackerrank.com/contests/infinitum-aug14/challenges/reverse-game/editorial

Hackerrank Ad Infinitum August 2014 - Reverse Game solution: 


#include <iostream>
#include <cstdio>

int main() {
    int n, k, t, cnt, a;
    scanf("%d", &t);
    while(t--) {
        scanf("%d%d", &n, &k);
        if(k<(n-1)/2) {
            cnt=0, a=2;
            while(cnt<k) a+=2, cnt++;
        }
        else {
            cnt=n-1, a=1;
            while(cnt>k) a+=2, cnt--;
        }
        printf("%d\n", a-1);
    }
    return 0;
}

Friday, August 15, 2014

Hackerrank Weekly - Week 8 - Counter Game solution

Hackerrank Weekly - Week 8 - Counter Game: https://www.hackerrank.com/contests/w8/challenges/counter-game

Hackerrank Weekly - Week 8 - Counter Game editorial: https://www.hackerrank.com/contests/w8/challenges/counter-game/editorial

Hackerrank Weekly - Week 8 - Counter Game solution: 


#include <iostream>
#include <cstdio>

int main() {
    long long unsigned int n, tmp;
    int cnt, t;
    scanf("%d", &t);
    while(t--) {
        scanf("%llu", &n), tmp=n;
        cnt=0;
        while(tmp) tmp&=tmp-1, cnt++;
        cnt--;
        while((n&1)==0) n>>=1, cnt++;
        if(cnt%2==0) printf("Richard\n");
        else printf("Louise\n");
    }
    return 0;
}

and my stackoverflow question to the difference between ampersand and equal to signs in c++  & and == operators: http://stackoverflow.com/questions/25256749/difference-between-n1-and-n1