SRM 159 DIV2 Level One - 路上駐車できるとこ

ロスで路上駐車の切符切られたことあるww


交通違反って何であんなに凹むんだ!?


さて、ルールに基づいて、路上駐車できるところを探していく。

  • 私道の前は停めちゃダメ。
  • バス停の前と、5,10メートル先は停めちゃダメ。
  • 脇道の前と、5メートル先、5メートル後は停めちゃダメ。

というルールらしい。


文字列で道路の状況が与えられるので、駐車できるところの数を答える。


別のバッファを用意しておいてダメな所にマークを付けていく作戦で。

#include <iostream>

#include <string>
#include <vector>
using namespace std;

#define foreach(bind, item) \
    for (typeof ((item).begin()) bind = (item).begin(), __ie = (item).end() ;\
         bind != __ie ; \
         bind++)

class StreetParking
{
private:
    static void put(int position, vector<int> *free)
    {
        if (position >= 0 && position < (*free).size())
            (*free)[position] = 0;
    }

public:
    static int freeParks(string street)
    {
        int count = 0;
        vector<int> free(street.length(), 1);

        for (int i = 0; i < street.length(); i++) {
            switch (street[i]) {
            case 'D':
                put(i, &free);
                break;
            case 'B':
                put(i, &free);
                put(i - 1, &free);
                put(i - 2, &free);
                break;
            case 'S':
                put(i + 1, &free);
                put(i, &free);
                put(i - 1, &free);
                break;
            }
        }

        foreach(i, free)
            count += *i;

        return count;
    }
};


駐車場を探そう!!