Saturday, June 4, 2011

SRM148

Div 1, Level 1
Div 1, Level 2
Div 1, Level 3
Div 2, Level 1
Div 2, Level 2
Div 2, Level 3

Tutorials:

Division One - Level Three:

Solution

Source Code:

Division One - Level Two:

Solution

Same as Div2, Lvl 3.

Source Code:

Division One - Level One:

Solution

Source Code:

Division Two - Level Three:

Solution

Source Code:

//2009/07/27 10:28:51
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>

using namespace std;

class MNS
{
public:
    int combos(vector <int> numbers)
    {
        int score = 0;
        set<vector<int> > v;
        sort(numbers.begin(), numbers.end());

        if(checkMagic(numbers))
        {
            score++;
            v.insert(numbers);
        }

        while(next_permutation(numbers.begin(), numbers.end()))
        {
            if(checkMagic(numbers))
            {
                int sz = v.size();
                v.insert(numbers);
                if(sz != v.size())
                    score++;
            }
        }
        return score;
    }
private:
    bool checkMagic(vector<int> nn)
    {
        set<int> s;
        s.insert(nn[0] + nn[1] + nn[2]);
        s.insert(nn[3] + nn[4] + nn[5]);
        s.insert(nn[6] + nn[7] + nn[8]);

        s.insert(nn[0] + nn[3] + nn[6]);
        s.insert(nn[1] + nn[4] + nn[7]);
        s.insert(nn[2] + nn[5] + nn[8]);

        return (s.size() == 1);
    }
};

Division Two - Level Two:

Solution

Source Code:

//2009/07/27 10:29:03
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

class CeyKaps
{
public:
    string decipher(string typed, vector <string> switches)
    {
        string p;
        p.clear();
        for(int i=0; i<typed.size(); i++)\
        {
            char cc = typed[i];
            for(int j=0; j<switches.size(); j++)
            {
                if(switches[j][0] == cc)
                    cc = switches[j][2];
                else if(switches[j][2] == cc)
                    cc = switches[j][0];
            }
            p += cc;
        }
        return p;
    }
};
//Partly from editorials of Topcoder.

Division Two - Level One:

Solution

Source Code:

//2009/07/26 17:36:59
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>

using namespace std;

class DivisorDigits
{
public:
    int howMany(int number)
    {
        int res = 0;
        int mod = 10;
        int div = 1;
        while(div <= number)
        {
            if((number%mod) / div != 0 && number % ((number%mod)/div) == 0)
                res++;
            mod *= 10;
            div *= 10;
        }
        return res;
    }
};

No comments :