Wednesday, July 30, 2014

TCHS SRM 47 Level one - 250 pt - Cards shuffle solution

TCHS SRM 47 Level one - 250 pt - Cards shuffle statement: http://community.topcoder.com/stat?c=problem_statement&pm=8295&rd=10803

TCHS SRM 47 Level one - 250 pt - Cards shuffle editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm47

TCHS SRM 47 Level one - 250 pt - Cards shuffle solution: 


#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

class CardsShuffle {
    public: string shuffle(string c, int f, int l, int t) {
        std::rotate(c.begin(), c.begin() + ((f-1)*t % l), c.begin() + l);
        return c;       
    }
};

my solution:
#include <iostream>
#include <cstring>

class CardsShuffle{
    public: std::string shuffle(std::string c, int f, int l, int t) {
        std::string s;
        while(t--) s=c.substr(f-1, l-(f-1)), s+=c.substr(0, f-1), s+=c.substr(l), c=s;
        return s;
    }
};

No comments:

Post a Comment