Hatena::Grouptopcoder

TopCoderの問題を解く

解いた問題の一覧表

2009-07-05

ProblemWriting

| 15:12

問題文

497.42 -> 967.77 / 1000

以下の状態遷移図を基にプログラムすればいい。

f:id:caligue:20090720171507p:image

enum STATES { S0, S1, S2, S3 };

class ProblemWriting {
public:
    string myCheckData(string dotForm) {
        const int len = dotForm.length();
        if (len < 1 || 25 < len) {
            return "dotForm must contain between 1 and 25 characters, inclusive.";
        }
        vector<char> ops;
        ops.push_back('+'); ops.push_back('-');
        ops.push_back('*'); ops.push_back('/');
        int state = S0;
        for (int i = 0; i < len; i++) {
            const char c = dotForm[i];
            bool isError = false;
            switch (state) {
            case S0:
                if (isdigit(c)) 
                    state = S1;
                else 
                    isError = true;
                break;
            case S1:
            case S2:
                if (c == '.') 
                    state = S2;
                else if (find(ops.begin(),ops.end(),c) != ops.end()) 
                    state = S3;
                else 
                    isError = true;
                break;
            case S3:
                if (c == '.') 
                    state = S3;
                else if (isdigit(c)) 
                    state = S1;
                else 
                    isError = true;
                break;
            }
            if (isError) {
                ostringstream oss;
                oss << "dotForm is not in dot notation, check character "
                    << i << ".";
                return oss.str();
            }
        }
        if (state != S1) {
            ostringstream oss;
            oss << "dotForm is not in dot notation, check character "
                << len << ".";
            return oss.str();
        }
        return "";
    }
};