Hatena::Grouptopcoder

TopCoderの問題を解く

解いた問題の一覧表

2009-08-03

StringTrain

| 18:38

問題文

電車の連結。PrefixとSuffixの問題。

class StringTrain {
    public:
        string buildTrain(vector <string> cars) {
            string train = cars[0];
            for (int i = 1; i < cars.size(); i++) {
                const int len_train = train.length();
                const int len_car = cars[i].length();
                for (int j = len_car-1; j >= 1; j--) {
                    if (len_train <= j) continue;
                    if (train.substr(len_train-j,j) == cars[i].substr(0,j)) {
                        train = train + cars[i].substr(j);
                        break;
                    } 
                }
            }
            set<char> appeared;
            ostringstream oss;
            for (int i = train.length()-1; i >= 0; i--) {
                if (appeared.find(train[i]) == appeared.end()) {
                    oss << train[i];
                    appeared.insert(train[i]);
                }
            }
            string strAfterRemv(oss.str());
            reverse(strAfterRemv.begin(), strAfterRemv.end());
            ostringstream resoss;
            resoss << train.length() << " " << strAfterRemv;
            return resoss.str();
        }
};

DennysDennys2012/07/10 08:54There's a trefriic amount of knowledge in this article!

ptkxkwfiufxptkxkwfiufx2012/07/11 08:32rW3Q5y <a href="http://lvoddmvsamev.com/">lvoddmvsamev</a>

ssurmbuvpsmssurmbuvpsm2012/07/11 21:20GnFEME , [url=http://jcvsaeuskkgr.com/]jcvsaeuskkgr[/url], [link=http://iseogblgtggc.com/]iseogblgtggc[/link], http://nsaattulsfhk.com/

xtmpsysmfgnxtmpsysmfgn2012/07/12 13:058VKorp <a href="http://qspzckisagzz.com/">qspzckisagzz</a>

ueblizedjueblizedj2012/07/12 18:376GmZzo , [url=http://rtmucgtfbosq.com/]rtmucgtfbosq[/url], [link=http://dzmpwnhervlx.com/]dzmpwnhervlx[/link], http://trmherritlub.com/