Skip to content

[Week 6] DICTIONARY self review - Yunhyunjoย #184

@Yunhyunjo

Description

@Yunhyunjo

DICTIONARY self review

1. ํ•ด๊ฒฐ ์‹œ๋„ ๊ณผ์ •

์œ„์ƒ์ •๋ ฌ์„ ์œ„ํ•ด indegree์™€ ch๋ฅผ ์„ ์–ธํ•˜์—ฌ ์ €์žฅํ•˜๊ณ  ์ธ์ ‘๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด ์ •๋ ฌ์„ ์‹œ๋„ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

2. ์ž‘์„ฑํ•œ ์ฝ”๋“œ์™€ ์„ค๋ช…

while (c--) {
		string s, p;
		fill(indegree.begin(), indegree.end(), 0);
		fill(ch.begin(), ch.end(), 0);
		vector <vector <int>> v(26);
		cin >> n >> s;
		p = s;
		ch[p[0] - 97] = 1;
		for (int i = 1; i < n; i++) {
			cin >> s;
			if (p[0] != s[0]) {
				if (ch[s[0] - 97] == 0) ch[s[0] - 97] = 1;
				indegree[s[0] - 97]++;
				v[p[0] - 97].push_back(s[0] - 97);
			}
			else {
				int pp = 1;
				while (pp < p.size() && pp < s.size()) {
					if (p[pp] != s[pp]) {
						if (ch[p[pp] - 97] == 0) ch[p[pp] - 97] = 1;
						if (ch[s[pp] - 97] == 0) ch[s[pp] - 97] = 1;
						indegree[s[pp] - 97]++;
						v[p[pp] - 97].push_back(s[pp] - 97);
						break;
					}
					pp++;
				}
			}
			p = s;
		}
		cout << topologySort(v) << endl;
	}

๋จผ์ € ์ž…๋ ฅ๋œ ์ˆœ์„œ๋Œ€๋กœ ๋น„๊ตํ•˜์—ฌ ์ธ์ ‘๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค๊ณ  indegree๋„ ๊ตฌํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.
๋‘ ๋‹จ์–ด์˜ 0๋ฒˆ์งธ ๊ฐ’์ด ๊ฐ™์„ ๊ฒฝ์šฐ์—๋Š” ๋‹ค์Œ ์ธ๋ฑ์Šค์˜ ํ•ด๋‹นํ•˜๋Š” ๋ฌธ์ž๋ฅผ ๋น„๊ตํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋ ‡๊ฒŒ ๊ตฌํ•œ ์ธ์ ‘๋ฆฌ์ŠคํŠธ๋ฅผ topologySortํ•จ์ˆ˜์— ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋„˜๊ฒจ์ฃผ์–ด ์œ„์ƒ์ •๋ ฌ์„ ํ•ด ์ถœ๋ ฅํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

string topologySort(vector <vector <int>>& v) {
	string s = "";
	queue <int> q;
	for (int i = 0; i < 26; i++) {
		if (ch[i] == 1 && indegree[i] == 0) {
			q.push(i);
		}
	}

	while (!q.empty()) {
		int x = q.front();
		q.pop();
		char c = x + 97;
		s += c;
		for (int j = 0; j < v[x].size(); j++) {
			if (v[x][0] == 50) return "INVALID HYPOTHESIS";
			if (--indegree[v[x][j]] == 0) q.push(v[x][j]);
		}
		v[x].clear();
		v[x].push_back(50);
	}
	if(s == "") return "INVALID HYPOTHESIS";
	for (int i = 0; i < 26; i++) {
		if (ch[i] == 0) {
			char c = i + 97;
			s += c;
		}
	}
	return s;
}

queue๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ผ๋‹จ indegree๊ฐ’์ด 0์ธ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ๋‹ค์Œ bfs๋ฅผ ํ†ตํ•ด ์œ„์ƒ์ •๋ ฌ์„ ํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.
์ˆœํ™˜์„ ๋ง‰๊ธฐ์œ„ํ•ด ์ด๋ฏธ ์ •๋ ฌ์— ์‚ฌ์šฉํ•œ ์ •์  ๋ฆฌ์ŠคํŠธ๋Š” clearํ•ด์ค€ ๋‹ค์Œ ๊ฐ’ 50์„ ๋„ฃ์–ด์ฃผ์—ˆ๊ณ , for ๋ฌธ์„ ๋Œ๋•Œ 0๋ฒˆ์งธ ๊ฐ’์ด 50์ด๋ฉด ์ด๋ฏธ ์ •๋ ฌ์— ์‚ฌ์šฉํ•œ ์ •์ ์œผ๋กœ ์ˆœํ™˜๋œ ๊ฒƒ ์ด๋ฏ€๋กœ ๋ฐ”๋กœ INVALID HYPOTHESIS๋ฅผ ๋ฆฌํ„ดํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.
๋˜ indegree๊ฐ€ 0์ธ ๊ฒƒ์ด ์—†์–ด s๊ฐ€ ๋น„์–ด์žˆ์„ ๊ฒฝ์šฐ์—๋„ ์ˆœํ™˜์ด๋ž€ ๋œป์ด๋ฏ€๋กœ INVALID HYPOTHESIS๋ฅผ ๋ฆฌํ„ดํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

3. ๋ง‰ํžŒ ์  ๋ฐ ๊ฐœ์„  ์‚ฌํ•ญ

์˜ˆ์ œ ์ž…๋ ฅ์€ ์ •๋‹ต์ด ์ œ๋Œ€๋กœ ๋‚˜์™€์„œ ์ •ํ™•ํžˆ ์–ด๋””์„œ ํ‹€๋ฆฐ์ง€๋ฅผ ๋ชจ๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋˜‘๊ฐ™์€ ์ˆœ์„œ๊ฐ€ ์ค‘๋ณต์œผ๋กœ ๋‚˜์˜ฌ ๋•Œ ์–ด์ฐจํ”ผ topologySortํ•จ์ˆ˜์—์„œ ๊ฑธ๋Ÿฌ์ง€๋ฏ€๋กœ ์ธ์ ‘๋ฆฌ์ŠคํŠธ ๋งŒ๋“ค๋•Œ ์ฒ˜๋ฆฌ๋ฅผ ์•ˆํ•ด์คฌ๋Š”๋ฐ ์ฒ˜๋ฆฌํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋„ฃ์–ด์ค˜์•ผํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

Metadata

Metadata

Assignees

No one assigned

    Labels

    2๊ธฐ์Šคํ„ฐ๋”” 2๊ธฐWAWrong Answer

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions