[C++] Palindrome
ยท
๐Ÿ“ Computer Science/โœ Algorithm
Palindrome1. ๊ฐœ๋…Palindrome์€ ๊ฑฐ๊พธ๋กœ ์ฝ์–ด๋„ ์ œ๋Œ€๋กœ ์ฝ๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ๋ฌธ์žฅ์„ ๋งํ•œ๋‹ค.  2. ๋ฌธ์ œhttps://leetcode.com/problems/palindrome-partitioning/description/#include #include using namespace std;class Solution {public: vector> answer; vector sub; vector> partition(string s) { dfs(s, 0); return answer; } void dfs(string s, int index) { if (index == s.length()) { answer.push_bac..
[C++] ์ด๋ถ„ ๊ทธ๋ž˜ํ”„
ยท
๐Ÿ“ Computer Science/โœ Algorithm
์ด๋ถ„ ๊ทธ๋ž˜ํ”„ 1. ๊ฐœ๋… ์ด๋ถ„ ๊ทธ๋ž˜ํ”„๋ž€ ์ธ์ ‘ํ•œ ์ •์ ๋ผ๋ฆฌ ์„œ๋กœ ๋‹ค๋ฅธ ์ƒ‰์œผ๋กœ ์น ํ•˜์—ฌ ๋ชจ๋“  ์ •์ ์„ ๋‘ ๊ทธ๋ฃน์œผ๋กœ ๋‚˜๋ˆ„๊ณ , ์„œ๋กœ ๋‹ค๋ฅธ ๊ทธ๋ฃน์˜ ์ •์ ์„ ๊ฐ„์„ ์œผ๋กœ ์—ฐ๊ฒฐํ•œ ๊ทธ๋ž˜ํ”„๋ฅผ ๋งํ•œ๋‹ค. 2. ์ด๋ถ„ ๋งค์นญ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ด๋ถ„ ๋งค์นญ์ด๋ž€ ์ด๋ถ„ ๊ทธ๋ž˜ํ”„์—์„œ ํ•œ์ชฝ ๊ทธ๋ฃน(A)๊ณผ ๋‹ค๋ฅธ ํ•œ์ชฝ ๊ทธ๋ฃน(1)์ด ๋งค์นญ์„ ํ–ˆ์„ ๋•Œ ์ตœ๋Œ€ ์œ ๋Ÿ‰์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ๋งํ•œ๋‹ค. 3. ๋ฌธ์ œ https://www.acmicpc.net/problem/1707 1707๋ฒˆ: ์ด๋ถ„ ๊ทธ๋ž˜ํ”„ ์ž…๋ ฅ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š”๋ฐ, ์ฒซ์งธ ์ค„์— ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ K๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ์ฒซ์งธ ์ค„์—๋Š” ๊ทธ๋ž˜ํ”„์˜ ์ •์ ์˜ ๊ฐœ์ˆ˜ V์™€ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ E๊ฐ€ ๋นˆ ์นธ์„ ์‚ฌ์ด์— www.acmicpc.net #include #include #include #include us..
[C++] ํŽธ์ง‘ ๊ฑฐ๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜(Edit Distance Algorithm)
ยท
๐Ÿ“ Computer Science/โœ Algorithm
ํŽธ์ง‘ ๊ฑฐ๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 1. ๊ฐœ๋… ํŽธ์ง‘ ๊ฑฐ๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜(Edit Distance Algorithm)์€ ๋‘ ๊ฐœ์˜ ๋ฌธ์ž์—ด A, B๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋‘ ๋ฌธ์ž์—ด์ด ์–ผ๋งˆ๋‚˜ ์œ ์‚ฌํ•œ ์ง€๋ฅผ ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ์ฆ‰, ๋ฌธ์ž์—ด A๊ฐ€ ๋ฌธ์ž์—ด B์™€ ๊ฐ™์•„์ง€๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ช‡ ๋ฒˆ์˜ ์—ฐ์‚ฐ์„ ์ง„ํ–‰ํ•ด์•ผ ํ•˜๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๊ธฐ์„œ์˜ ์—ฐ์‚ฐ์ด๋ž€ ์‚ฝ์ž…(Insertion), ์‚ฝ์ž…(Deletion), ๋Œ€์ฒด(Replacement)๋ฅผ ๋งํ•œ๋‹ค. ๋ฌธ์ž์—ด ๊ฐ„์˜ ์œ ์‚ฌ๋„ ์ธก์ •์ฒ˜๋Ÿผ ๊ธฐ๋ณธ์ ์œผ๋กœ๋Š” ๋‘ ๋ฐ์ดํ„ฐ ์‚ฌ์ด์˜ ์œ ์‚ฌ๋„๋ฅผ ์•Œ์•„๋‚ด๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ํŠนํžˆ ํ”„๋กœ๊ทธ๋žจ์˜ ํ‘œ์ ˆ ์—ฌ๋ถ€, ์ฒ ์ž ์˜ค๋ฅ˜ ๊ฒ€์‚ฌ ๋“ฑ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 2. ๊ธฐ๋ณธ ์›๋ฆฌ 3. ๋ฌธ์ œ https://leetcode.com/problems/edit-distance/description/ Edit D..
์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ •๋ฆฌ
ยท
๐Ÿ“ Computer Science/โœ Algorithm
์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ •๋ฆฌ 1. ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ์‚ฌ์ดํŠธ BOJ solved.ac: BOJ ๋ฌธ์ œ๋“ค์˜ ๋‚œ์ด๋„ ๋ฐ ํ‹ฐ์–ด ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” ์‚ฌ์ดํŠธ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ฆฌํŠธ์ฝ”๋“œ ๊ธฐ์ถœ ๋ฌธ์ œ ์‚ผ์„ฑ: SWEA, BOJ ์‚ผ์„ฑ ๊ธฐ์ถœ ๋ชจ์Œ ์นด์นด์˜ค: ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 2. ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ๊ฐ€์ด๋“œ ์‚ฌ์ดํŠธ BOJ ๊ธธ๋ผ์žก์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณต๋ถ€, ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ•˜๋‚˜์š”?
[C++] ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ(Linked List)
ยท
๐Ÿ“ Computer Science/โœ Algorithm
์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ(Linked List) ๋ฌธ์ œ https://leetcode.com/problems/add-two-numbers/ Add Two Numbers - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com class Solution { public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode* root = new ListNode(); ListNode* node = root; int num = 0..
[C++] ํ‰ํ–‰ ๋ถ„ํ• , ๋ชจ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜
ยท
๐Ÿ“ Computer Science/โœ Algorithm
ํ‰ํ–‰ ๋ถ„ํ• , ๋ชจ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ‰ํ–‰ ๋ถ„ํ• (SQRT Decomposition)์€ ๋ฐ์ดํ„ฐ๊ฐ€ N๊ฐœ์ด๋ฉด √N๊ฐœ๋งŒํผ ๋‚˜๋ˆ ์„œ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ๊ฐ ๊ทธ๋ฃน์€ ๋Œ€ํ‘ฏ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ๋งŒ์•ฝ ์ฃผ์–ด์ง€๋Š” ์ฟผ๋ฆฌ๊ฐ€ ๊ตฌ๊ฐ„์˜ ๋ง์…ˆ์ด๋ผ๋ฉด ๊ทธ๋ฃน์˜ ํ•ฉ์ด ๋Œ€ํ‘ฏ๊ฐ’์ด ๋œ๋‹ค. ๋ชจ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์–ด๋–ค ๊ตฌ๊ฐ„ [s, e]์— ์†ํ•˜๋Š” ์›์†Œ๋“ค์„ ์ด์šฉํ•˜์—ฌ ์–ด๋–ค ๊ฐ’์„ ๊ณ„์‚ฐํ•˜๋Š” ์ฟผ๋ฆฌ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ์—…๋ฐ์ดํŠธ๊ฐ€ ์—†์–ด ์ฟผ๋ฆฌ์˜ ์ˆœ์„œ๋ฅผ ๋ณ€๊ฒฝํ•ด๋„ ๋ฌด๋ฐฉํ•˜๋‹ค. ๊ทธ๋ž˜์„œ ์ฟผ๋ฆฌ ์ˆœ์„œ๋ฅผ ์žฌ๋ฐฐ์น˜ํ•˜์—ฌ ํšจ์œจ์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•œ๋‹ค. ๋ฐฐ์—ด์„ k(√N)๊ฐœ๋กœ ์ด๋ฃจ์–ด์ง„ ๊ทธ๋ฃน์œผ๋กœ ๋‚˜๋ˆ ์ฃผ๊ณ , ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•  ๋•Œ ๋‘ ์กฐ๊ฑด ์ค‘ ํ•˜๋‚˜๋ฅผ ๋งŒ์กฑํ•˜๋Š” ๊ฒฝ์šฐ ๋จผ์ € ์ฒ˜๋ฆฌํ•œ๋‹ค. [s1/k] < [s2/k] [s1/k] = [s2/k] and e1 < e2 ๋ฌธ์ œ https://www.acmicpc.net/problem/1..