Hatena::Grouptopcoder

TopCoderの問題を解く

解いた問題の一覧表

2009-08-20

ProgressBar

| 17:36

問題文, SRM 173

インストール作業の進捗具合を示すプログレス・バーを表示する。

240.19/250

class ProgressBar {
public:
    string showProgress(vector <int> taskTimes, int tasksCompleted) {
        int total = accumulate(taskTimes.begin(), taskTimes.end(), 0);
        int completed = accumulate(taskTimes.begin(), 
                taskTimes.begin()+tasksCompleted, 0);
        double percent = static_cast<double>(completed) / total;
        string progress(20, '.');
        for (int i = 0; i < floor(percent*20); i++)
            progress[i] = '#';
        return progress;
    }
};

Poetry

| 13:47

問題文, SRM 170

与えられた詩が、どのように音韻を踏んでいるのかを調べる。

377.34/1000

class Poetry {
public:
    string rhymeScheme(vector <string> poem) {
        char label = 'a';
        vector<vector<string> > substrings(poem.size());
        string result(poem.size(), ' ');
        for (int i = 0; i < poem.size(); i++) {
            if (poem[i].length() == 0) continue;
            string s = tolowerString(poem[i]);
            istringstream iss(s);
            string last(""), tmp;
            while (iss >> tmp) last = tmp;
            if (last == "")
                continue;
            const int len = last.length();
            bool isPreVowel = false;
            for (int j = len-1; j >= 0; j--) {
                const char c = last[j];
                bool isVowel = (j!=len-1 && j!=0 && c=='y')
                    || c=='a' || c=='e' || c=='i' || c=='o' || c=='u';
                if (isPreVowel && !isVowel) 
                    substrings[i].push_back(last.substr(j+1));
                isPreVowel = isVowel;
            }
            if (isPreVowel) substrings[i].push_back(last);
            for (int j = 0; j <= substrings[i].size(); j++) {
                if (j == substrings[i].size()) {
                    result[i] = label;
                    label++;
                    if (label > 'z') label = 'A';
                    break;
                }
                for (int k = 0; k < i; k++)  {
                    if (find(substrings[k].begin(), substrings[k].end(), substrings[i][j])
                            != substrings[k].end()) {
                        result[i] = result[k];
                        break;
                    }
                }
                if (result[i] != ' ') break;
            }

        }
        return result;
    }
private:
    string tolowerString(string s) {
        for (int i = 0; i < s.length(); i++)
            s[i] = tolower(s[i]);
        return s;
    }
};