夢追い人

"It takes a dreamer to make a dream come true."―Vincent Willem van Gogh

SRM151 Div.2 Easy PrefixCode

なんだかんだ言って、

解きましたのです。
あと約三時間ある。長いなぁ・・・

Prefix Codeを調べよう。

今回はあえて問題文載せないでおきましょうか。

Prefix codeかどうか判定し、そうなら'Yes'、だめならどこでダメだったか、例えばi番目がだめなら'No, i'といった感じで返す問題。
Prefix codeはWikiみればわかります。

で解答。

class PrefixCode {
   public:
   string isOne(vector <string> words)
  {
	string tmp; vector<int> count(words.size(), 0); bool b = true;
	for (int i=0; i<words.size(); i++) {
		tmp = words[i];
		for (int j=0; j<words.size(); j++) {
			if (i == j) {
				continue;
			} else if (tmp.length() > words[j].length()) {
				continue;
			} else {
				for (int k=0; k<tmp.length(); k++) {
					if (tmp[k] != words[j][k]) b = false;
				}
				if (!b) {
					b = true;
					continue;
				}
				count[i]++;
			}
		}
	}
	int p = -1;
	for (int i=0; i<words.size(); i++) {
		if (count[i] != 0) {
			p = i;
			break;
		}
	}
	stringstream ss;
	if (p != -1) {
		ss << p;
		tmp = "No, "+ss.str();
	} else {
		tmp = "Yes";
	}
	return tmp;
  }
};

いつもこんな感じでやってますが、一応言っておくと僕のコードは最良とは限らないんで・・・

そこは宜しくお願いします。


じゃ、次は日付変更線超えたあとで!