๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/Note12

[C++, ํ…œํ”Œ๋ฆฟ] ๋ฐฐ์—ด ๋Œ๋ฆฌ๊ธฐ์™€ ๋‹ฌํŒฝ์ด ๋ฐฐ์—ด 1. ๋ฐฐ์—ด ์ƒํ•˜ ๋ฐ˜์ „ 1 6 2 9 8 4 → 4 2 9 3 1 8 7 2 6 9 8 2 → 9 2 3 6 1 5 1 8 3 4 2 9 → 7 4 6 2 3 1 7 4 6 2 3 1 → 1 8 3 4 2 9 9 2 3 6 1 5 → 7 2 6 9 8 2 4 2 9 3 1 8 → 1 6 2 9 8 4 void cmd1(){ // ๋ฐฐ์—ด ์ƒํ•˜ ๋ฐ˜์ „ int temp[101][101] = {0, }; for(int i=0; i 2022. 9. 22.
๋น„ํŠธ๋งˆ์Šคํฌ(BitMask) ๋น„ํŠธ๋งˆ์Šคํฌ(BitMask) ๋น„ํŠธ๋Š” ์ปดํ“จํ„ฐ์—์„œ ๋‹ค๋ฃจ๋Š” ์ตœ์†Œ ๋‹จ์œ„์ด๋ฉฐ, ์ •์ˆ˜๋ฅผ ์ด์ง„์ˆ˜(0, 1)๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋น„ํŠธ์˜ ํŠน์„ฑ์„ ์ด์šฉํ•œ ์—ฐ์‚ฐ์„ ํ†ตํ•ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด ๊ธฐ๋ฒ•์„ ๋น„ํŠธ๋งˆ์Šคํฌ(BitMask)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด์ง„์ˆ˜๋Š” 0 ๋˜๋Š” 1์„ ์ด์šฉํ•˜๋ฏ€๋กœ ํ•˜๋‚˜์˜ ๋น„ํŠธ(bit)๊ฐ€ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๋Š” ๋‘ ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ๋ณดํ†ต ์–ด๋–ค ๋น„ํŠธ๊ฐ€ 1์ด๋ฉด ์ „๊ตฌ๊ฐ€ "์ผœ์ ธ ์žˆ๋‹ค", 0์ด๋ฉด "๊บผ์ ธ ์žˆ๋‹ค"๋ผ๊ณ  ๋งํ•˜๋ฉฐ, ์ „๊ตฌ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์•„๋ž˜์ฒ˜๋Ÿผ ๋‘ ๊ฐ€์ง€ ๊ฒฝ์šฐ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ์˜ˆ์‹œ๋Š” ๋ชจ๋‘ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์˜์ž์— ์•‰์œผ๋ฉด 1, ์„œ์žˆ์œผ๋ฉด 0 ๋‚จ์ž๋ฉด 1, ์—ฌ์ž๋ฉด 0 ์ •๋‹ต์ด๋ฉด 1, ์˜ค๋‹ต์ด๋ฉด 0 ์‚ฌ์šฉ ์ด์œ  ๋น„ํŠธ๋งˆ์Šคํฌ ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ํฐ ์ด์œ ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ˆ˜ํ–‰ ์‹œ๊ฐ„์ด ๋น ๋ฅด๋‹ค. ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด ์ ๋‹ค. ๋น„ํŠธ๋งˆ์Šคํฌ ์—ฐ์‚ฐ์€ bit ์—ฐ์‚ฐ์ด๊ธฐ.. 2022. 9. 16.
[C++, ํ…œํ”Œ๋ฆฟ] ์œ ๋‹ˆ์˜จ ํŒŒ์ธ๋“œ ์œ ๋‹ˆ์˜จ ํŒŒ์ธ๋“œ #include #define MAX 100001 using namespace std; int N, M, ans; int parent[MAX]; int find_parent(int a){ if(a == parent[a]) return a; return parent[a] = find_parent(parent[a]); } void make_parent(int a, int b){ a = find_parent(a); b = find_parent(b); if(a 2022. 9. 2.
[C++, ํ…œํ”Œ๋ฆฟ] map find index Get index of element in C++ map Get index of element in C++ map I have a std::map called myMap in my C++ application, and I want to get an element using either myMap.find(key) or myMap[key]. However, I would also like to get the index of that element in the map... stackoverflow.com int k = distance(mymap.begin(), mymap.find(mykey)); 2022. 8. 23.
[C++, ํ…œํ”Œ๋ฆฟ] ๋‹ค์ต์ŠคํŠธ๋ผ ๋‹ค์ต์ŠคํŠธ๋ผ #include #include #include #define INF 1E9 using namespace std; // ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜(V), ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜(E), ์‹œ์ž‘ ๋…ธ๋“œ ๋ฒˆํ˜ธ(K) // ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๋Š” ์ตœ๋Œ€ 20000๊ฐœ๋ผ๊ณ  ๊ฐ€์ • int V, E, K; // ๊ฐ ๋…ธ๋“œ์— ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ๋…ธ๋“œ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋‹ด๋Š” ๋ฐฐ์—ด vectorgraph[20001]; // ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ํ…Œ์ด๋ธ” ๋งŒ๋“ค๊ธฐ int d[20001]; void dijkstra(int start){ priority_queuepq; // ์‹œ์ž‘ ๋…ธ๋“œ๋กœ ๊ฐ€๊ธฐ ์œ„ํ•œ ์ตœ๋‹จ ๊ฒฝ๋กœ๋Š” 0์œผ๋กœ ์„ค์ •ํ•˜์—ฌ, ํ์— ์‚ฝ์ž… pq.push({0, start}); d[start] = 0; while(!pq.empty()){ int dist = pq.top().first; //.. 2022. 5. 28.
[C++, ํ…œํ”Œ๋ฆฟ] ํ”Œ๋กœ์ด๋“œ-์™€์ƒฌ ํ”Œ๋กœ์ด๋“œ-์™€์ƒฌ #include #define INF 1e9 // ๋ฌดํ•œ์„ ์˜๋ฏธํ•˜๋Š” ๊ฐ’์œผ๋กœ 10์–ต์„ ์„ค์ • using namespace std; // ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜(N), ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜(M) // ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๋Š” ์ตœ๋Œ€ 500๊ฐœ๋ผ๊ณ  ๊ฐ€์ • int n, m; // 2์ฐจ์› ๋ฐฐ์—ด(๊ทธ๋ž˜ํ”„ ํ‘œํ˜„)๋ฅผ ๋งŒ๋“ค๊ธฐ int graph[501][501]; int main(void) { cin >> n >> m; // ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ํ…Œ์ด๋ธ”์„ ๋ชจ๋‘ ๋ฌดํ•œ์œผ๋กœ ์ดˆ๊ธฐํ™” for (int i = 0; i > b >> c; graph[a][b] = c; } // ์ ํ™”.. 2022. 5. 13.
[C++, ์œ ์šฉํ•œ ๋ฌธ๋ฒ•] upper_bound, lower_bound lower_bound ์šฉ๋„ : ์ฐพ์œผ๋ ค๋Š” key ๊ฐ’๋ณด๋‹ค ๊ฐ™๊ฑฐ๋‚˜ ํฐ ์ˆซ์ž๊ฐ€ ๋ฐฐ์—ด ๋ช‡ ๋ฒˆ์งธ์—์„œ ์ฒ˜์Œ ๋“ฑ์žฅํ•˜๋Š”์ง€ ์ฐพ๊ธฐ ์œ„ํ•จ ์‚ฌ์šฉ ์กฐ๊ฑด : ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•  ๋ฐฐ์—ด ํ˜น์€ ๋ฒกํ„ฐ๋Š” ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ๋˜์–ด ์žˆ์–ด์•ผ ํ•จ lower_bound์˜ ๋ฐ˜ํ™˜ํ˜•์€ Iterator ์ด๋ฏ€๋กœ ์‹ค์ œ๋กœ ๋ช‡ ๋ฒˆ์งธ ์ธ๋ฑ์Šค์ธ์ง€ ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด, lower_bound ๊ฐ’์—์„œ ๋ฐฐ์—ด ์ฒซ ๋ฒˆ์งธ ์ฃผ์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฐฐ์—ด์˜ ์ด๋ฆ„์„ ๋นผ ์ค˜์•ผํ•จ. 1. array #include #include using namespace std; int main() { int arr[6] = { 1,2,3,4,5,6 }; cout 2022. 5. 11.
[C++, ํ…œํ”Œ๋ฆฟ] ์†Œ์ˆ˜ ๊ตฌํ•˜๊ธฐ(์—๋ผํ† ์ŠคํŠธ๋„ค์Šค์˜ ์ฒด) ์†Œ์ˆ˜ ๊ตฌํ•˜๊ธฐ(์—๋ผํ† ์ŠคํŠธ๋„ค์Šค์˜ ์ฒด) #include #include #include using namespace std; int main() { int N = 10000; // 0 ~10000๊นŒ์ง€์˜ ์†Œ์ˆ˜ ๊ตฌํ•˜๊ธฐ vectorprime(N + 1, true); // ์ตœ์ข…์ ์œผ๋กœ true์ผ ๊ฒฝ์šฐ ์†Œ์ˆ˜ prime[0] = prime[1] = false; for(int i=2; i 2022. 4. 11.
[C++, ์œ ์šฉํ•œ ๋ฌธ๋ฒ•] ๋ฌธ์ž์—ด ๋Œ€์†Œ๋ฌธ์ž ๋ณ€ํ™˜ ๋ฌธ์ž์—ด ๋Œ€์†Œ๋ฌธ์ž ๋ณ€ํ™˜ #include #include using namespace std; int main(){ string s = "ABCD"; string s2 = "abcd"; transform(s.begin(), s.end(), s.begin(), ::tolower); // ๋Œ€๋ฌธ์ž -> ์†Œ๋ฌธ์ž transform(s2.begin(), s2.end(), s2.begin(), ::toupper); // ์†Œ๋ฌธ์ž -> ๋Œ€๋ฌธ์ž } 2022. 4. 10.