Hatena::Grouptopcoder

TopCoderの問題を解く

解いた問題の一覧表

2009-08-11

Twain

| 19:36

問題文

問題文の通りに文字列を変換する。やるだけの問題だが、書く必要があるコード量が多い。一回目にサブミットしたプログラムはバグがあり、システムテストは通らなかった。

184.88/500 - 00:42:14 (failed)

class Twain {
    public:
        string getNewSpelling(int year, string phrase) {
            vector<char> exceptions;
            exceptions.push_back('a'); exceptions.push_back('e');
            exceptions.push_back('i'); exceptions.push_back('o');
            exceptions.push_back('u');
            for (int y = 1; y <= year; y++) {
                string tmpStr;
                int len;
                switch (y) {
                    case 1:
                        if (phrase[0] == 'x') phrase[0] = 'z';
                        phrase = replaceCharsOfWord(phrase, " x", " z");
                        phrase = replaceCharsOfWord(phrase, "x", "ks");
                        break;
                    case 2:
                        phrase = replaceCharsOfWord(phrase, "y", "i");
                        break;
                    case 3:
                        phrase = replaceCharsOfWord(phrase, "ce", "se");
                        phrase = replaceCharsOfWord(phrase, "ci", "si");
                        break;
                    case 4:
                        do {
                            tmpStr = phrase;
                            phrase = replaceCharsOfWord(phrase, "ck", "k");
                        } while (tmpStr != phrase);
                        break;
                    case 5:
                        if (phrase.substr(0,3) == "sch") 
                            phrase = "sk" + phrase.substr(3);
                        phrase = replaceCharsOfWord(phrase, " sch", " sk");
                        phrase = replaceCharsOfWord(phrase, "chr", "kr");
                        for (char c = 'a'; c <= 'z'; c++) {
                            if (c == 'h') continue;
                            string pattern("c"+string(1,c));
                            string placement("k"+string(1,c));
                            phrase = replaceCharsOfWord(phrase, pattern, placement);
                        }
                        phrase = replaceCharsOfWord(phrase, "c ", "k ");
                        len = phrase.length();
                        if (phrase[len-1] == 'c') phrase[len-1] = 'k';
                        break;
                    case 6:
                        if (phrase.substr(0,2) == "kn")
                            phrase = "n" + phrase.substr(2);
                        phrase = replaceCharsOfWord(phrase, " kn", " n");
                        break;
                    case 7:
                        do {
                            tmpStr = phrase;
                            for (char c = 'a'; c <= 'z'; c++) {
                                if (find(exceptions.begin(),exceptions.end(),c) == exceptions.end()) {
                                    string pattern(2,c);
                                    string placement(1,c);
                                    phrase = replaceCharsOfWord(phrase, pattern, placement);
                                }
                            }
                        } while (tmpStr != phrase);
                        break;
                }
            }
            return phrase;
        }
    private:
        string replaceCharsOfWord(const string& phrase, 
                const string pattern, const string placement) {
            string result;
            string::size_type pos = 0;
            string::size_type pre_pos = 0;
            string::size_type pat_len = pattern.length();
            while ((pos = phrase.find(pattern,pos)) != string::npos) {
                result.append(phrase, pre_pos, pos-pre_pos);
                result.append(placement);
                pos += pat_len;
                pre_pos = pos;
            }
            result.append(phrase, pre_pos, phrase.length()-pre_pos);
            return result;
        }
};

OliviaOlivia2011/07/09 22:49Ppl like you get all the birans. I just get to say thanks for he answer.

cganpmvkaacganpmvkaa2011/07/10 00:21ozUmeq <a href="http://pjfcqeeeqmnr.com/">pjfcqeeeqmnr</a>

gkbfwfvcbgkbfwfvcb2011/07/10 21:09Bo1aDa , [url=http://xrkdnugczyzj.com/]xrkdnugczyzj[/url], [link=http://sbngsfqxhlpy.com/]sbngsfqxhlpy[/link], http://safjlfwoassi.com/

uxpnpunauxpnpuna2011/07/11 20:32crHIwv <a href="http://fodsgqzsvyim.com/">fodsgqzsvyim</a>

mhhaaumhhaau2011/07/12 21:58da56vU , [url=http://tqvvgrxdnlwt.com/]tqvvgrxdnlwt[/url], [link=http://ofcuaexemicz.com/]ofcuaexemicz[/link], http://shzifphpejdh.com/