<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>자료구조 Archives - 어제와 내일의 나 그 사이의 이야기</title>
	<atom:link href="https://lycos7560.com/tag/%EC%9E%90%EB%A3%8C%EA%B5%AC%EC%A1%B0/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>생각의 흐름을 타고 다니며 만드는 블로그</description>
	<lastBuildDate>Mon, 13 Oct 2025 06:07:16 +0000</lastBuildDate>
	<language>ko-KR</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://lycos7560.com/wp-content/uploads/2022/11/cropped-cropped-cropped-log-1-150x150-1-80x80.png</url>
	<title>자료구조 Archives - 어제와 내일의 나 그 사이의 이야기</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>백준 14003번 (가장 긴 증가하는 부분 수열 5, C++)</title>
		<link>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-14003%eb%b2%88-%ea%b0%80%ec%9e%a5-%ea%b8%b4-%ec%a6%9d%ea%b0%80%ed%95%98%eb%8a%94-%eb%b6%80%eb%b6%84-%ec%88%98%ec%97%b4-5-c/40314/</link>
					<comments>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-14003%eb%b2%88-%ea%b0%80%ec%9e%a5-%ea%b8%b4-%ec%a6%9d%ea%b0%80%ed%95%98%eb%8a%94-%eb%b6%80%eb%b6%84-%ec%88%98%ec%97%b4-5-c/40314/#comments</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Mon, 13 Oct 2025 06:06:17 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[14003]]></category>
		<category><![CDATA[14003번]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Algorithm Analysis]]></category>
		<category><![CDATA[Algorithm Explanation]]></category>
		<category><![CDATA[Algorithm Practice]]></category>
		<category><![CDATA[Algorithm Study]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[Binary Search LIS]]></category>
		<category><![CDATA[binary_search]]></category>
		<category><![CDATA[BOJ 14003]]></category>
		<category><![CDATA[BOJ Solution]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[C++ Algorithm]]></category>
		<category><![CDATA[C++ STL]]></category>
		<category><![CDATA[Coding Interview]]></category>
		<category><![CDATA[Coding Lecture]]></category>
		<category><![CDATA[Coding Test]]></category>
		<category><![CDATA[Competitive Coding]]></category>
		<category><![CDATA[Competitive Programming]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Data Structure]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[DP]]></category>
		<category><![CDATA[DP Table]]></category>
		<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[Efficiency]]></category>
		<category><![CDATA[Example Walkthrough]]></category>
		<category><![CDATA[Fast IO]]></category>
		<category><![CDATA[Greedy]]></category>
		<category><![CDATA[Greedy Algorithm]]></category>
		<category><![CDATA[Implementation]]></category>
		<category><![CDATA[Increasing Sequence]]></category>
		<category><![CDATA[Index Mapping]]></category>
		<category><![CDATA[Index Tracking]]></category>
		<category><![CDATA[Input Output]]></category>
		<category><![CDATA[LIS]]></category>
		<category><![CDATA[LIS Diagram]]></category>
		<category><![CDATA[LIS Example]]></category>
		<category><![CDATA[LIS Flow]]></category>
		<category><![CDATA[LIS Indexing]]></category>
		<category><![CDATA[LIS Pattern]]></category>
		<category><![CDATA[LIS Reconstruction]]></category>
		<category><![CDATA[LIS Reconstruction Algorithm]]></category>
		<category><![CDATA[LIS step by step]]></category>
		<category><![CDATA[LIS Study]]></category>
		<category><![CDATA[LIS Table]]></category>
		<category><![CDATA[LIS Tail]]></category>
		<category><![CDATA[LIS Track]]></category>
		<category><![CDATA[LIS Tutorial]]></category>
		<category><![CDATA[LIS using Binary Search]]></category>
		<category><![CDATA[LIS Visualization]]></category>
		<category><![CDATA[LIS 강의]]></category>
		<category><![CDATA[LIS 구현]]></category>
		<category><![CDATA[LIS 단계별]]></category>
		<category><![CDATA[LIS 문제]]></category>
		<category><![CDATA[LIS 문제풀이]]></category>
		<category><![CDATA[LIS 복원]]></category>
		<category><![CDATA[LIS 복원 알고리즘]]></category>
		<category><![CDATA[LIS 알고리즘]]></category>
		<category><![CDATA[LIS 예제]]></category>
		<category><![CDATA[LIS 추적]]></category>
		<category><![CDATA[LIS 코드]]></category>
		<category><![CDATA[LIS 패턴]]></category>
		<category><![CDATA[Logic Explanation]]></category>
		<category><![CDATA[Longest Increasing Subsequence]]></category>
		<category><![CDATA[Longest Subsequence]]></category>
		<category><![CDATA[lower_bound]]></category>
		<category><![CDATA[Memory Limit]]></category>
		<category><![CDATA[Memory Optimization]]></category>
		<category><![CDATA[O(N log N)]]></category>
		<category><![CDATA[O(N^2)]]></category>
		<category><![CDATA[O(NlogN)]]></category>
		<category><![CDATA[Optimal Algorithm]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Prev Index]]></category>
		<category><![CDATA[Problem Explanation]]></category>
		<category><![CDATA[problem solving]]></category>
		<category><![CDATA[Programming Algorithm]]></category>
		<category><![CDATA[Programming Study]]></category>
		<category><![CDATA[Sequence Analysis]]></category>
		<category><![CDATA[Sequence Optimization]]></category>
		<category><![CDATA[Sequence Problem]]></category>
		<category><![CDATA[Sequence Reconstruction]]></category>
		<category><![CDATA[Sequence Trace]]></category>
		<category><![CDATA[STL]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[Subsequence]]></category>
		<category><![CDATA[Tail Array]]></category>
		<category><![CDATA[Theory]]></category>
		<category><![CDATA[Time Complexity]]></category>
		<category><![CDATA[Time Limit]]></category>
		<category><![CDATA[Traceback]]></category>
		<category><![CDATA[upper_bound]]></category>
		<category><![CDATA[vector]]></category>
		<category><![CDATA[가장 긴 증가하는 부분 수열]]></category>
		<category><![CDATA[가장 긴 증가하는 부분 수열 5]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[동적 계획법]]></category>
		<category><![CDATA[디버깅]]></category>
		<category><![CDATA[로직 해설]]></category>
		<category><![CDATA[메모리 최적화]]></category>
		<category><![CDATA[문제 해설]]></category>
		<category><![CDATA[배열]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 14003]]></category>
		<category><![CDATA[백준 14003번]]></category>
		<category><![CDATA[백준 문제풀이]]></category>
		<category><![CDATA[벡터]]></category>
		<category><![CDATA[수열 문제]]></category>
		<category><![CDATA[수열 분석]]></category>
		<category><![CDATA[수열 추적]]></category>
		<category><![CDATA[시간 복잡도]]></category>
		<category><![CDATA[시간 제한]]></category>
		<category><![CDATA[시간 최적화]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[알고리즘 분석]]></category>
		<category><![CDATA[알고리즘 설명]]></category>
		<category><![CDATA[알고리즘 연습]]></category>
		<category><![CDATA[예제 해설]]></category>
		<category><![CDATA[이론]]></category>
		<category><![CDATA[이분 탐색]]></category>
		<category><![CDATA[인덱스 기반 LIS]]></category>
		<category><![CDATA[입출력]]></category>
		<category><![CDATA[자료구조]]></category>
		<category><![CDATA[증가 수열]]></category>
		<category><![CDATA[최장 부분 수열]]></category>
		<category><![CDATA[최적화]]></category>
		<category><![CDATA[최적화 알고리즘]]></category>
		<category><![CDATA[추적]]></category>
		<category><![CDATA[코딩 강의]]></category>
		<category><![CDATA[코딩 공부]]></category>
		<category><![CDATA[코딩 인터뷰]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[탐욕 알고리즘]]></category>
		<category><![CDATA[효율성]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=40314</guid>

					<description><![CDATA[<p>가장 긴 증가하는 부분 수열 5 https://www.acmicpc.net/problem/14003 시간 제한 메모리 제한 제출 정답 맞힌 사람 정답 비율 3 초 512 MB 45574 15974 11322 34.533% 문제 수열 A가 주어졌을 때, 가장 긴 증가하는&#160;부분&#160;수열을 구하는 프로그램을 작성하시오. 예를 들어, 수열 A = {10, 20, 10, 30, 20, 50} 인 경우에 가장 긴 증가하는 부분&#160;수열은&#160;A = {10,&#160;20, [&#8230;]</p>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-14003%eb%b2%88-%ea%b0%80%ec%9e%a5-%ea%b8%b4-%ec%a6%9d%ea%b0%80%ed%95%98%eb%8a%94-%eb%b6%80%eb%b6%84-%ec%88%98%ec%97%b4-5-c/40314/">백준 14003번 (가장 긴 증가하는 부분 수열 5, C++)</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-87ca5ab3      "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							목차						</div>
																						<div class="uagb-toc__list-wrap ">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#가장-긴-증가하는-부분-수열-5" class="uagb-toc-link__trigger">가장 긴 증가하는 부분 수열 5</a><ul class="uagb-toc__list"><li class="uagb-toc__list"><a href="#문제" class="uagb-toc-link__trigger">문제</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#입력" class="uagb-toc-link__trigger">입력</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#출력" class="uagb-toc-link__trigger">출력</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#예제-입력-1" class="uagb-toc-link__trigger">예제 입력 1</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#예제-출력-1" class="uagb-toc-link__trigger">예제 출력 1</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#출처" class="uagb-toc-link__trigger">출처</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#비슷한-문제" class="uagb-toc-link__trigger">비슷한 문제</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#알고리즘-분류" class="uagb-toc-link__trigger">알고리즘 분류</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#1차-시도-로직-그대로-구현" class="uagb-toc-link__trigger">1차 시도 &#8211; 로직 그대로 구현</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#2차-시도-이분-탐색-인덱스-추적을-이용한-lis-복원-on-log-n" class="uagb-toc-link__trigger">2차 시도 &#8211; 이분 탐색 + 인덱스 추적을 이용한 LIS 복원 / O(N log N)</a></ul></ol>					</div>
									</div>
				</div>
			


<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">가장 긴 증가하는 부분 수열 5 <img decoding="async" width="35" height="45" class="wp-image-37909" style="width: 35px;" src="https://lycos7560.com/wp-content/uploads/2024/03/Platinum_5.jpg" alt="" srcset="https://lycos7560.com/wp-content/uploads/2024/03/Platinum_5.jpg 400w, https://lycos7560.com/wp-content/uploads/2024/03/Platinum_5-234x300.jpg 234w" sizes="(max-width: 35px) 100vw, 35px" /></h2>



<p><a href="https://www.acmicpc.net/problem/14003" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/14003</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>시간 제한</th><th>메모리 제한</th><th>제출</th><th>정답</th><th>맞힌 사람</th><th>정답 비율</th></tr></thead><tbody><tr><td>3 초</td><td>512 MB</td><td>45574</td><td>15974</td><td>11322</td><td>34.533%</td></tr></tbody></table></figure>



<h3 class="wp-block-heading">문제</h3>



<p>수열 A가 주어졌을 때, 가장 긴 증가하는&nbsp;부분&nbsp;수열을 구하는 프로그램을 작성하시오.</p>



<p>예를 들어, 수열 A = {10, 20, 10, 30, 20, 50} 인 경우에 가장 긴 증가하는 부분&nbsp;수열은&nbsp;A = {<strong>10</strong>,&nbsp;<strong>20</strong>, 10,&nbsp;<strong>30</strong>, 20,&nbsp;<strong>50</strong>} 이고, 길이는 4이다.</p>



<div style="height:15px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">입력</h3>



<p>첫째 줄에 수열 A의 크기 N (1 ≤ N ≤ 1,000,000)이 주어진다.</p>



<p>둘째 줄에는 수열 A를 이루고 있는 A<sub>i</sub>가 주어진다. (-1,000,000,000&nbsp;≤ A<sub>i</sub>&nbsp;≤ 1,000,000,000)</p>



<div style="height:15px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">출력</h3>



<p>첫째 줄에 수열 A의 가장 긴 증가하는 부분 수열의 길이를 출력한다.</p>



<p>둘째 줄에는 정답이 될 수 있는 가장 긴 증가하는 부분 수열을 출력한다.</p>



<div style="height:15px" aria-hidden="true" class="wp-block-spacer"></div>



<div class="wp-block-uagb-container uagb-block-aa78ee06 alignfull uagb-is-root-container"><div class="uagb-container-inner-blocks-wrap">
<div class="wp-block-uagb-container uagb-block-70c24c24">
<h3 class="wp-block-heading">예제 입력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">6
10 20 10 30 20 50</pre>
</div>



<div class="wp-block-uagb-container uagb-block-2c88bb50">
<h3 class="wp-block-heading">예제 출력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">4
10 20 30 50</pre>
</div>
</div></div>



<div style="height:15px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">출처</h3>



<ul class="wp-block-list">
<li>문제를 만든 사람: <a href="https://www.acmicpc.net/user/baekjoon">baekjoon</a></li>



<li>데이터를 추가한 사람: <a href="https://www.acmicpc.net/user/harinboy">harinboy</a>, <a href="https://www.acmicpc.net/user/jh05013">jh05013</a>, <a href="https://www.acmicpc.net/user/kkw564">kkw564</a>, <a href="https://www.acmicpc.net/user/ldw0318">ldw0318</a>, <a href="https://www.acmicpc.net/user/minho6428">minho6428</a>, <a href="https://www.acmicpc.net/user/sohnryang">sohnryang</a>, <a href="https://www.acmicpc.net/user/surung9898">surung9898</a>, <a href="https://www.acmicpc.net/user/yeo2507">yeo2507</a></li>



<li>빠진 조건을 찾은 사람: <a href="https://www.acmicpc.net/user/Acka">Acka</a></li>
</ul>



<div style="height:15px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">비슷한 문제</h3>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/11053" target="_blank" rel="noreferrer noopener">11053번. 가장 긴 증가하는 부분 수열</a></li>



<li><a href="https://www.acmicpc.net/problem/11054" target="_blank" rel="noreferrer noopener">11054번. 가장 긴 바이토닉 부분 수열</a></li>



<li><a href="https://www.acmicpc.net/problem/11055" target="_blank" rel="noreferrer noopener">11055번. 가장 큰 증가하는 부분 수열</a></li>



<li><a href="https://www.acmicpc.net/problem/11722" target="_blank" rel="noreferrer noopener">11722번. 가장 긴 감소하는 부분 수열</a></li>



<li><a href="https://www.acmicpc.net/problem/12015" target="_blank" rel="noreferrer noopener">12015번. 가장 긴 증가하는 부분 수열 2</a></li>



<li><a href="https://www.acmicpc.net/problem/12738" target="_blank" rel="noreferrer noopener">12738번. 가장 긴 증가하는 부분 수열 3</a></li>



<li><a href="https://www.acmicpc.net/problem/14002" target="_blank" rel="noreferrer noopener">14002번. 가장 긴 증가하는 부분 수열 4</a></li>
</ul>



<div style="height:15px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">알고리즘 분류</h3>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/12" target="_blank" rel="noreferrer noopener">이분 탐색</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/235" target="_blank" rel="noreferrer noopener">역추적</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/43" target="_blank" rel="noreferrer noopener">가장 긴 증가하는 부분 수열 문제</a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--60);margin-bottom:var(--wp--preset--spacing--60)"/>



<h3 class="wp-block-heading">1차 시도 &#8211; 로직 그대로 구현</h3>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="1043" height="115" src="https://lycos7560.com/wp-content/uploads/2025/10/image-4.png" alt="" class="wp-image-40317" srcset="https://lycos7560.com/wp-content/uploads/2025/10/image-4.png 1043w, https://lycos7560.com/wp-content/uploads/2025/10/image-4-300x33.png 300w, https://lycos7560.com/wp-content/uploads/2025/10/image-4-768x85.png 768w" sizes="(max-width: 1043px) 100vw, 1043px" /></figure>



<p>한 번의 입력(<code>num</code>)에 의해 생성된 새로운 시퀀스들을 임시로 저장</p>



<p>어떤 수열에도 붙을 수 없으면 독립된 시퀀스 시작</p>



<p>나중에 불필요한 시퀀스(같은 길이인데 꼬리가 큰 것)는 제거</p>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;vector>

using namespace std;

int n; // 수열의 개수
vector&lt;vector&lt;int>> Sequences;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> n;
    if (n &lt;= 0) return 0;

    int num;
    cin >> num;

    // 첫 번째 수로 초기 시퀀스 하나 생성
    vector&lt;int> sequence;
    sequence.push_back(num);
    Sequences.push_back(sequence);

    for (int i = 1; i &lt; n; i++)
    {
        cin >> num;
        int length = Sequences.size();

        bool canExtend = false;
        vector&lt;vector&lt;int>> newSequences;

        // 기존 시퀀스들을 모두 검사
        for (int j = 0; j &lt; length; j++)
        {
            int last = Sequences[j].back();

            // 확장 가능한 경우 : 새로운 시퀀스 생성
            if (last &lt; num)
            {
                canExtend = true;
                vector&lt;int> extendedSeq = Sequences[j];
                extendedSeq.push_back(num);
                newSequences.push_back(extendedSeq);
            }
        }

        // 어떤 시퀀스에도 붙지 못하면 새로운 시퀀스 시작
        if (!canExtend)
        {
            vector&lt;int> newSeq;
            newSeq.push_back(num);
            newSequences.push_back(newSeq);
        }

        // 새로 생긴 시퀀스들을 기존에 합침
        for (auto&amp; seq : newSequences)
            Sequences.push_back(seq);

        // 최적 시퀀스 길이 및 꼬리값 계산
        int maxSeqSize = 0;
        int bestIndex = 0;
        int bestTail = 1000000001;

        for (int j = 0; j &lt; (int)Sequences.size(); j++)
        {
            int sz = Sequences[j].size();
            int tail = Sequences[j].back();
            if (sz > maxSeqSize || (sz == maxSeqSize &amp;&amp; tail &lt; bestTail))
            {
                maxSeqSize = sz;
                bestTail = tail;
                bestIndex = j;
            }
        }

        // 가지치기: 같은 길이인데 꼬리값이 큰 시퀀스 제거
        for (int j = 0; j &lt; (int)Sequences.size();)
        {
            int sz = Sequences[j].size();
            int tail = Sequences[j].back();

            if (sz == maxSeqSize &amp;&amp; tail > bestTail)
            {
                Sequences.erase(Sequences.begin() + j);
            }
            else
            {
                j++;
            }
        }
    }

	// 결과 중 가장 긴 시퀀스 찾기
    int maxSize = 0;
    int bestIdx = 0;
    for (int i = 0; i &lt; (int)Sequences.size(); i++)
    {
        if (Sequences[i].size() > maxSize)
        {
            maxSize = Sequences[i].size();
            bestIdx = i;
        }
    }

    cout &lt;&lt; maxSize &lt;&lt; "\n";
    for (int x : Sequences[bestIdx])
        cout &lt;&lt; x &lt;&lt; " ";

    return 0;
}
</pre>



<div style="height:25px" aria-hidden="true" class="wp-block-spacer"></div>



<p>브루트포스 방식으로 모든 가능한 증가 수열을 시도하는 방법은 시간 초과 및 메모리 초과로 인하여 통과할 수 없음.</p>



<figure class="wp-block-image size-full"><img decoding="async" width="604" height="509" src="https://lycos7560.com/wp-content/uploads/2025/10/image-3.png" alt="" class="wp-image-40318" srcset="https://lycos7560.com/wp-content/uploads/2025/10/image-3.png 604w, https://lycos7560.com/wp-content/uploads/2025/10/image-3-300x253.png 300w" sizes="(max-width: 604px) 100vw, 604px" /></figure>



<p><strong>매 반복마다</strong> <code>Sequences</code>의 크기가 <strong>기하급수적으로 늘어나는 구조</strong></p>



<p>n개의 원소가 들어오면 2ⁿ에 비례하는 시퀀스로 통과하기 힘들다. (O(2ⁿ))</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>단계</th><th>가능한 Sequences 수</th></tr></thead><tbody><tr><td>1</td><td>[ [1] ]</td></tr><tr><td>2</td><td>[ [1], [2], [1,2] ]</td></tr><tr><td>3</td><td>[ [1], [2], [3], [1,2], [1,3], [2,3], [1,2,3] ]</td></tr><tr><td>4</td><td>거의 모든 가능한 증가 부분 수열이 등장함 (약 15개)</td></tr></tbody></table></figure>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--50);margin-bottom:var(--wp--preset--spacing--50)"/>



<h3 class="wp-block-heading">2차 시도 &#8211; <strong>이분 탐색 + 인덱스 추적</strong>을 이용한 LIS 복원 / O(N log N)</h3>



<figure class="wp-block-image size-full"><img decoding="async" width="1042" height="39" src="https://lycos7560.com/wp-content/uploads/2025/10/image-7.png" alt="" class="wp-image-40322" srcset="https://lycos7560.com/wp-content/uploads/2025/10/image-7.png 1042w, https://lycos7560.com/wp-content/uploads/2025/10/image-7-300x11.png 300w, https://lycos7560.com/wp-content/uploads/2025/10/image-7-768x29.png 768w" sizes="(max-width: 1042px) 100vw, 1042px" /></figure>



<p>최장 증가 수열 (LIS, Longest Increasing Subsequence)</p>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;vector>
#include &lt;algorithm>

using namespace std;

// (LIS, Longest Increasing Subsequence)

int main()
{
    ios::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cout.tie(NULL);

    int N;
    std::cin >> N;

    vector&lt;int> input(N);
    for (int i = 0; i &lt; N; i++)
        std::cin >> input[i];

    vector&lt;int> tail;  // LIS를 구성하는 인덱스들
    vector&lt;int> prev_idx(N, -1);  //각 요소가 LIS에 포함될 때, 그 직전 요소의 인덱스를 저장


    for (int i = 0; i &lt; N; i++)
    {
        // 이분 탐색으로 input[i]가 tail 배열에서 들어가야 할 위치 찾기
        int lo = 0;
        int hi = (int)tail.size() - 1;
        int pos = (int)tail.size(); // 기본값은 맨 뒤

        while (lo &lt;= hi)
        {
            int mid = (lo + hi) / 2;
            // input[i]보다 크거나 같은 첫 번째 tail 원소 위치(pos)를 찾
            if (input[tail[mid]] &lt; input[i])
            {
                lo = mid + 1;
            }
            else
            {
                pos = mid;
                hi = mid - 1;
            }
        }

        // 이전 인덱스 설정
        if (pos > 0)
        {
            prev_idx[i] = tail[pos - 1];
        }
        else
        {
            prev_idx[i] = -1;  // 첫 번째 요소
        }

        // tail 업데이트
        if (pos == (int)tail.size())
        {
            tail.push_back(i);
        }
        else
        {
            tail[pos] = i;
        }
    }

    // 결과: LIS 길이
    int lis_len = (int)tail.size();
    std::cout &lt;&lt; lis_len &lt;&lt; "\n";

    // 실제 LIS 수열 재구성
    vector&lt;int> lis;
    for (int idx = tail.back(); idx != -1; idx = prev_idx[idx])
    {
        lis.push_back(input[idx]);
    }
    reverse(lis.begin(), lis.end());

    for (int v : lis)
        std::cout &lt;&lt; v &lt;&lt; " ";
    std::cout &lt;&lt; "\n";

    return 0;
}</pre>



<div style="height:15px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="651" height="324" src="https://lycos7560.com/wp-content/uploads/2025/10/image-5.png" alt="" class="wp-image-40319" srcset="https://lycos7560.com/wp-content/uploads/2025/10/image-5.png 651w, https://lycos7560.com/wp-content/uploads/2025/10/image-5-300x149.png 300w" sizes="(max-width: 651px) 100vw, 651px" /></figure>



<p><code>input[i]</code>가 들어가야 할 위치(<code>pos</code>)를 찾음</p>



<p>즉, <code>tail</code> 배열 내에서 <strong>input[i]보다 작지만 가장 큰 값 다음 위치</strong>를 찾는다.</p>



<figure class="wp-block-image size-full"><img decoding="async" width="513" height="466" src="https://lycos7560.com/wp-content/uploads/2025/10/image-6.png" alt="" class="wp-image-40320" srcset="https://lycos7560.com/wp-content/uploads/2025/10/image-6.png 513w, https://lycos7560.com/wp-content/uploads/2025/10/image-6-300x273.png 300w" sizes="(max-width: 513px) 100vw, 513px" /></figure>



<p><code>prev_idx[i]</code>는 <code>input[i]</code> 바로 앞에 올 LIS 원소의 인덱스입니다.</p>



<p><code>pos > 0</code>이면 → 현재 원소가 &#8220;길이 pos+1&#8243;짜리 LIS의 마지막이 되므로 &#8220;그 바로 이전 원소&#8221;는 <code>tail[pos - 1]</code></p>



<p><code>pos == 0</code>이면 → 수열의 첫 번째 원소이므로 이전이 없음</p>



<p><code>pos == tail.size()</code> → <code>input[i]</code>가 지금 까지의 모든 수보다 크므로 LIS 길이 1 증가</p>



<p>그렇지 않으면 → 기존 LIS 후보 중 &#8220;끝값이 더 큰 것&#8221;을 <code>input[i]</code>로 <strong>치환</strong> (더 작은 끝값 유지)</p>



<h4 class="wp-block-heading">예시 1</h4>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">input = [5, 2, 8, 6, 3, 6, 9, 7]</pre>



<div style="height:15px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-table"><table><thead><tr><th class="has-text-align-left" data-align="left">단계</th><th class="has-text-align-left" data-align="left">i</th><th class="has-text-align-left" data-align="left">input[i]</th><th class="has-text-align-left" data-align="left">tail (인덱스)</th><th class="has-text-align-left" data-align="left">tail (값으로 표현)</th><th class="has-text-align-left" data-align="left">prev_idx</th><th class="has-text-align-left" data-align="left">설명</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">0</td><td class="has-text-align-left" data-align="left">0</td><td class="has-text-align-left" data-align="left">5</td><td class="has-text-align-left" data-align="left">[0]</td><td class="has-text-align-left" data-align="left">[5]</td><td class="has-text-align-left" data-align="left">[-1, ?, ?, ?, ?, ?, ?, ?]</td><td class="has-text-align-left" data-align="left">첫 번째 원소 → 새로운 LIS 시작</td></tr><tr><td class="has-text-align-left" data-align="left">1</td><td class="has-text-align-left" data-align="left">1</td><td class="has-text-align-left" data-align="left">2</td><td class="has-text-align-left" data-align="left">[1]</td><td class="has-text-align-left" data-align="left">[2]</td><td class="has-text-align-left" data-align="left">[-1, -1, ?, ?, ?, ?, ?, ?]</td><td class="has-text-align-left" data-align="left">2 &lt; 5 → 기존 tail[0] 교체 (더 작은 끝값 유지)</td></tr><tr><td class="has-text-align-left" data-align="left">2</td><td class="has-text-align-left" data-align="left">2</td><td class="has-text-align-left" data-align="left">8</td><td class="has-text-align-left" data-align="left">[1, 2]</td><td class="has-text-align-left" data-align="left">[2, 8]</td><td class="has-text-align-left" data-align="left">[-1, -1, 1, ?, ?, ?, ?, ?]</td><td class="has-text-align-left" data-align="left">8 &gt; 2 → 길이 2짜리 LIS 형성</td></tr><tr><td class="has-text-align-left" data-align="left">3</td><td class="has-text-align-left" data-align="left">3</td><td class="has-text-align-left" data-align="left">6</td><td class="has-text-align-left" data-align="left">[1, 3]</td><td class="has-text-align-left" data-align="left">[2, 6]</td><td class="has-text-align-left" data-align="left">[-1, -1, 1, 1, ?, ?, ?, ?]</td><td class="has-text-align-left" data-align="left">6은 2보다 크지만 8보다 작음 → 8을 6으로 교체</td></tr><tr><td class="has-text-align-left" data-align="left">4</td><td class="has-text-align-left" data-align="left">4</td><td class="has-text-align-left" data-align="left">3</td><td class="has-text-align-left" data-align="left">[1, 4]</td><td class="has-text-align-left" data-align="left">[2, 3]</td><td class="has-text-align-left" data-align="left">[-1, -1, 1, 1, 1, ?, ?, ?]</td><td class="has-text-align-left" data-align="left">3은 2보다 크고 6보다 작음 → 6을 3으로 교체</td></tr><tr><td class="has-text-align-left" data-align="left">5</td><td class="has-text-align-left" data-align="left">5</td><td class="has-text-align-left" data-align="left">6</td><td class="has-text-align-left" data-align="left">[1, 4, 5]</td><td class="has-text-align-left" data-align="left">[2, 3, 6]</td><td class="has-text-align-left" data-align="left">[-1, -1, 1, 1, 1, 4, ?, ?]</td><td class="has-text-align-left" data-align="left">6은 3보다 크므로 길이 3짜리 LIS 생성</td></tr><tr><td class="has-text-align-left" data-align="left">6</td><td class="has-text-align-left" data-align="left">6</td><td class="has-text-align-left" data-align="left">9</td><td class="has-text-align-left" data-align="left">[1, 4, 5, 6]</td><td class="has-text-align-left" data-align="left">[2, 3, 6, 9]</td><td class="has-text-align-left" data-align="left">[-1, -1, 1, 1, 1, 4, 5, ?]</td><td class="has-text-align-left" data-align="left">9 &gt; 6 → 길이 4짜리 LIS 생성</td></tr><tr><td class="has-text-align-left" data-align="left">7</td><td class="has-text-align-left" data-align="left">7</td><td class="has-text-align-left" data-align="left">7</td><td class="has-text-align-left" data-align="left">[1, 4, 5, 7]</td><td class="has-text-align-left" data-align="left">[2, 3, 6, 7]</td><td class="has-text-align-left" data-align="left">[-1, -1, 1, 1, 1, 4, 5, 5]</td><td class="has-text-align-left" data-align="left">7은 6보다 크지만 9보다 작음 → 9를 7로 교체</td></tr></tbody></table></figure>



<div style="height:15px" aria-hidden="true" class="wp-block-spacer"></div>



<h4 class="wp-block-heading">예시 2</h4>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">이분 탐색을 이용한 LIS 구성 로직

input = [3, 5, 2, 7, 4, 1, 8]

--------------------------------

i=0 (값: 3)
tail: [] (빈 배열)
이분탐색: tail.size() = 0 → while문 스킵
pos = 0 (기본값)

prev_idx[0] = -1 (pos=0이므로)
tail 업데이트: pos == tail.size() → push_back(0)
결과: tail = [0] (값: [3])

--------------------------------

i=1 (값: 5)
tail: [0] (값: [3])
이분탐색:
  lo=0, hi=0, mid=0
  input[tail[0]] = 3 &lt; 5 → lo = mid+1 = 1
  lo=1 > hi=0 → 종료
pos = tail.size() = 1

prev_idx[1] = tail[0] = 0 (5 앞에 3)
tail 업데이트: push_back(1)
결과: tail = [0,1] (값: [3,5])

--------------------------------

i=2 (값: 2)
tail: [0,1] (값: [3,5])
이분탐색:
  lo=0, hi=1, mid=0
  input[tail[0]] = 3 >= 2 → pos=0, hi=-1
  lo=0 > hi=-1 → 종료
pos = 0

prev_idx[2] = -1 (pos=0이므로)
tail 업데이트: tail[0] = 2
결과: tail = [2,1] (값: [2,5])

--------------------------------

i=3 (값: 7)
tail: [2,1] (값: [2,5])
이분탐색:
  lo=0, hi=1, mid=0: input[tail[0]]=2 &lt; 7 → lo=1
  lo=1, hi=1, mid=1: input[tail[1]]=5 &lt; 7 → lo=2
  lo=2 > hi=1 → 종료
pos = tail.size() = 2

prev_idx[3] = tail[1] = 1 (7 앞에 5)
tail 업데이트: push_back(3)
결과: tail = [2,1,3] (값: [2,5,7])

--------------------------------

i=4 (값: 4)
tail: [2,1,3] (값: [2,5,7])
이분탐색:
  lo=0, hi=2, mid=1: input[tail[1]]=5 >= 4 → pos=1, hi=0
  lo=0, hi=0, mid=0: input[tail[0]]=2 &lt; 4 → lo=1
  lo=1 > hi=0 → 종료
pos = 1

prev_idx[4] = tail[0] = 2 (4 앞에 2)
tail 업데이트: tail[1] = 4
결과: tail = [2,4,3] (값: [2,4,7])

--------------------------------

i=5 (값: 1)
tail: [2,4,3] (값: [2,4,7])
이분탐색:
  lo=0, hi=2, mid=1: input[tail[1]]=4 >= 1 → pos=1, hi=0
  lo=0, hi=0, mid=0: input[tail[0]]=2 >= 1 → pos=0, hi=-1
  lo=0 > hi=-1 → 종료
pos = 0

prev_idx[5] = -1
tail 업데이트: tail[0] = 5
결과: tail = [5,4,3] (값: [1,4,7])

--------------------------------

i=6 (값: 8)

tail: [5,4,3] (값: [1,4,7])
이분탐색:
  lo=0, hi=2, mid=1: input[tail[1]]=4 &lt; 8 → lo=2
  lo=2, hi=2, mid=2: input[tail[2]]=7 &lt; 8 → lo=3
  lo=3 > hi=2 → 종료
pos = tail.size() = 3

prev_idx[6] = tail[2] = 3 (8 앞에 7)
tail 업데이트: push_back(6)
결과: tail = [5,4,3,6] (값: [1,4,7,8])

</pre>



<p></p>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-14003%eb%b2%88-%ea%b0%80%ec%9e%a5-%ea%b8%b4-%ec%a6%9d%ea%b0%80%ed%95%98%eb%8a%94-%eb%b6%80%eb%b6%84-%ec%88%98%ec%97%b4-5-c/40314/">백준 14003번 (가장 긴 증가하는 부분 수열 5, C++)</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-14003%eb%b2%88-%ea%b0%80%ec%9e%a5-%ea%b8%b4-%ec%a6%9d%ea%b0%80%ed%95%98%eb%8a%94-%eb%b6%80%eb%b6%84-%ec%88%98%ec%97%b4-5-c/40314/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>백준 9935번 (문자열 폭발, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-9935%eb%b2%88-%eb%ac%b8%ec%9e%90%ec%97%b4-%ed%8f%ad%eb%b0%9c-c-baekjoon/39929/</link>
					<comments>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-9935%eb%b2%88-%eb%ac%b8%ec%9e%90%ec%97%b4-%ed%8f%ad%eb%b0%9c-c-baekjoon/39929/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Thu, 13 Feb 2025 10:37:52 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[9935]]></category>
		<category><![CDATA[9935번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[FRULA]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[문자열]]></category>
		<category><![CDATA[문자열 폭발]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 9935번]]></category>
		<category><![CDATA[스택]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[자료 구조]]></category>
		<category><![CDATA[자료구조]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=39929</guid>

					<description><![CDATA[<p>문자열 폭발 https://www.acmicpc.net/problem/9935 시간 제한 메모리 제한 제출 정답 맞힌 사람 정답 비율 2 초 (추가 시간 없음) 128 MB 97727 27262 19242 27.023% 문제 상근이는 문자열에 폭발 문자열을 심어 놓았다. 폭발 문자열이 폭발하면 그 문자는 문자열에서 사라지며, 남은 문자열은 합쳐지게 된다. 폭발은 다음과 같은 과정으로 진행된다. 상근이는 모든 폭발이 끝난 후에 어떤 문자열이 남는지 [&#8230;]</p>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-9935%eb%b2%88-%eb%ac%b8%ec%9e%90%ec%97%b4-%ed%8f%ad%eb%b0%9c-c-baekjoon/39929/">백준 9935번 (문자열 폭발, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">문자열 폭발 <img decoding="async" width="30" height="38" class="wp-image-37904" style="width: 30px;" src="https://lycos7560.com/wp-content/uploads/2024/03/Gold_4.jpg" alt="" srcset="https://lycos7560.com/wp-content/uploads/2024/03/Gold_4.jpg 400w, https://lycos7560.com/wp-content/uploads/2024/03/Gold_4-234x300.jpg 234w" sizes="(max-width: 30px) 100vw, 30px" /></h2>



<p><a href="https://www.acmicpc.net/problem/9935" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/9935</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">2 초 (추가 시간 없음)</td><td class="has-text-align-left" data-align="left">128 MB</td><td class="has-text-align-left" data-align="left">97727</td><td class="has-text-align-left" data-align="left">27262</td><td class="has-text-align-left" data-align="left">19242</td><td class="has-text-align-left" data-align="left">27.023%</td></tr></tbody></table></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">문제</h3>



<p>상근이는 문자열에 폭발 문자열을 심어 놓았다. </p>



<p>폭발 문자열이 폭발하면 그 문자는 문자열에서 사라지며, 남은 문자열은 합쳐지게 된다.</p>



<p>폭발은 다음과 같은 과정으로 진행된다.</p>



<ul class="wp-block-list">
<li>문자열이 폭발 문자열을 포함하고 있는 경우에, 모든 폭발 문자열이 폭발하게 된다. <br>남은 문자열을 순서대로 이어 붙여 새로운 문자열을 만든다.</li>



<li>새로 생긴 문자열에 폭발 문자열이 포함되어 있을 수도 있다.</li>



<li>폭발은 폭발 문자열이 문자열에 없을 때까지 계속된다.</li>
</ul>



<p>상근이는 모든 폭발이 끝난 후에 어떤 문자열이 남는지 구해보려고 한다. </p>



<p>남아있는 문자가 없는 경우가 있다. 이때는 &#8220;FRULA&#8221;를 출력한다.</p>



<p>폭발 문자열은 같은 문자를 두 개 이상 포함하지 않는다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">입력</h3>



<p>첫째 줄에 문자열이 주어진다. 문자열의 길이는 1보다 크거나 같고, 1,000,000보다 작거나 같다.</p>



<p>둘째 줄에 폭발 문자열이 주어진다. 길이는 1보다 크거나 같고, 36보다 작거나 같다.</p>



<p>두 문자열은 모두 알파벳 소문자와 대문자, 숫자 0, 1, &#8230;, 9로만 이루어져 있다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">출력</h3>



<p>첫째 줄에 모든 폭발이 끝난 후 남은 문자열을 출력한다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">예제 입력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">mirkovC4nizCC44
C4</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">예제 출력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">mirkovniz</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">예제 입력 2</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">12ab112ab2ab
12ab</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">예제 출력 2</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">FRULA</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">출처</h3>



<p><a href="https://www.acmicpc.net/category/45">Contest</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/17">Croatian Open Competition in Informatics</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/272">COCI 2013/2014</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/detail/1230">Contest #5</a>&nbsp;3번</p>



<ul class="wp-block-list">
<li>문제를 번역한 사람: <a href="https://www.acmicpc.net/user/baekjoon">baekjoon</a></li>



<li>문제의 오타를 찾은 사람: <a href="https://www.acmicpc.net/user/chan4928">chan4928</a></li>



<li>시간 제한을 수정한 사람: <a href="https://www.acmicpc.net/user/jh05013">jh05013</a></li>



<li>데이터를 추가한 사람: <a href="https://www.acmicpc.net/user/limequeen">limequeen</a>, <a href="https://www.acmicpc.net/user/mrseos">mrseos</a></li>
</ul>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">알고리즘 분류</h3>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/175" target="_blank" rel="noreferrer noopener">자료 구조</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/158" target="_blank" rel="noreferrer noopener">문자열</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/71" target="_blank" rel="noreferrer noopener">스택</a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<h3 class="wp-block-heading">통과된 코드</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;string>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    string S, C;
    cin >> S >> C;

    string Res; 

    for (char ch : S)
    {
        Res.push_back(ch);

        // Res의 마지막 부분이 c와 같은지 확인
        if (Res.size() >= C.size() &amp;&amp; Res.substr(Res.size() - C.size()) == C)
        {
            // 문자열에서 폭발 문자열 제거
            Res.erase(Res.size() - C.size()); 
        }
    }

    cout &lt;&lt; (Res.empty() ? "FRULA" : Res);

    return 0;
}
</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<p><strong>예시 </strong><br><code>ch = 'm'</code>: <code>Res = "m"</code><br><code>ch = 'i'</code>: <code>Res = "mi"</code><br><code>ch = 'r'</code>: <code>Res = "mir"</code><br><code>ch = 'k'</code>: <code>Res = "mirk"</code><br><code>ch = 'o'</code>: <code>Res = "mirko"</code><br><code>ch = 'v'</code>: <code>Res = "mirkov"</code><br><code>ch = 'C'</code>: <code>Res = "mirkovC"</code><br><code>ch = '4'</code>: <code>Res = "mirkovC4"</code><br><code>ch = 'n'</code>: <code>Res = "mirkovn"</code><br><code>ch = 'i'</code>: <code>Res = "mirkovni"</code><br><code>ch = 'z'</code>: <code>Res = "mirkoviz"</code><br><code>ch = 'C'</code>: <code>Res = "mirkovnizC"</code><br><code>ch = 'C'</code>: <code>Res = "mirkovnizCC"</code><br><code>ch = '4'</code>: <code>Res = "mirkovnizCC4"</code><br><code>ch = '4'</code>: <code>Res = "mirkovnizC4"</code><br><em><strong>Res = &#8220;mirkovniz&#8221;</strong></em></p>



<p></p>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-9935%eb%b2%88-%eb%ac%b8%ec%9e%90%ec%97%b4-%ed%8f%ad%eb%b0%9c-c-baekjoon/39929/">백준 9935번 (문자열 폭발, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-9935%eb%b2%88-%eb%ac%b8%ec%9e%90%ec%97%b4-%ed%8f%ad%eb%b0%9c-c-baekjoon/39929/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 2161번 (카드1, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2161%eb%b2%88-%ec%b9%b4%eb%93%9c1-c-baekjoon/35524/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2161%eb%b2%88-%ec%b9%b4%eb%93%9c1-c-baekjoon/35524/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Wed, 21 Jun 2023 03:23:46 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[2161]]></category>
		<category><![CDATA[2161번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Queue]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[구현]]></category>
		<category><![CDATA[기본]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 2161]]></category>
		<category><![CDATA[백준 2161번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[자료 구조]]></category>
		<category><![CDATA[자료구조]]></category>
		<category><![CDATA[카드1]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[큐]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=35524</guid>

					<description><![CDATA[<p>백준 2161번 '카드1' 문제에 대한 글입니다. Queue를 이용한 구현문제입니다. (This is an article about the question of "Card 1" in Baekjoon Number 2161.<br />
It is an implementation problem using Queue.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2161%eb%b2%88-%ec%b9%b4%eb%93%9c1-c-baekjoon/35524/">백준 2161번 (카드1, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>


				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-dbc07632      "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							목차 테이블						</div>
																						<div class="uagb-toc__list-wrap ">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#카드1" class="uagb-toc-link__trigger">카드1</a><ul class="uagb-toc__list"><li class="uagb-toc__list"><a href="#통과된-코드" class="uagb-toc-link__trigger">통과된 코드</a></ul></ol>					</div>
									</div>
				</div>
			


<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">카드1</h1>



<p class="has-medium-font-size"><a href="https://www.acmicpc.net/problem/2161" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/2161</a></p>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">2 초</td><td class="has-text-align-left" data-align="left">128 MB</td><td class="has-text-align-left" data-align="left">12937</td><td class="has-text-align-left" data-align="left">8539</td><td class="has-text-align-left" data-align="left">7532</td><td class="has-text-align-left" data-align="left">68.698%</td></tr></tbody></table></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">문제</h3>



<p>N장의 카드가 있다. 각각의 카드는 차례로 1부터 N까지의 번호가 붙어 있으며, </p>



<p>1번 카드가 제일 위에, N번 카드가 제일 아래인 상태로 순서대로 카드가 놓여 있다.</p>



<p>이제 다음과 같은 동작을 카드가 한 장 남을 때까지 반복하게 된다. </p>



<p>우선, 제일 위에 있는 카드를 바닥에 버린다. </p>



<p>그 다음, 제일 위에 있는 카드를 제일 아래에 있는 카드 밑으로 옮긴다.</p>



<p>예를 들어 N=4인 경우를 생각해 보자. </p>



<p>카드는 제일 위에서부터 1234 의 순서로 놓여있다. </p>



<p>1을 버리면 234가 남는다. 여기서 2를 제일 아래로 옮기면 342가 된다. </p>



<p>3을 버리면 42가 되고, 4를 밑으로 옮기면 24가 된다. </p>



<p>마지막으로 2를 버리고 나면, 버린 카드들은 순서대로 1 3 2가 되고, 남는 카드는 4가 된다.</p>



<p>N이 주어졌을 때, 버린 카드들을 순서대로 출력하고, </p>



<p>마지막에 남게 되는 카드를 출력하는 프로그램을 작성하시오.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">입력</h3>



<p>첫째 줄에 정수 N(1 ≤ N ≤ 1,000)이 주어진다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">출력</h3>



<p>첫째 줄에 버리는 카드들을 순서대로 출력한다. </p>



<p>제일 마지막에는 남게 되는 카드의 번호를 출력한다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 입력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">7</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 출력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">1 3 5 7 4 2 6</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">알고리즘 분류</h3>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/102" target="_blank" rel="noreferrer noopener">구현</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/175" target="_blank" rel="noreferrer noopener">자료 구조</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/72" target="_blank" rel="noreferrer noopener">큐</a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<h2 class="wp-block-heading has-large-font-size">통과된 코드</h2>



<p>큐를 이용하여 상황을 구현하는 단순한 문제입니다.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;queue>
using namespace std;
queue&lt;int> _MyQueue;
int _N;
int main()
{
	cin >> _N;
	for (int i = 1; i &lt;= _N; i++)
		_MyQueue.push(i);
	while (true) {
		cout &lt;&lt; _MyQueue.front() &lt;&lt; " ";
		_MyQueue.pop();
		if (_MyQueue.empty())
			break;
		_MyQueue.push(_MyQueue.front());
		_MyQueue.pop();
	}
	return 0;
}</pre>



<div style="height:41px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1205" height="95" src="https://lycos7560.com/wp-content/uploads/2023/06/image-36.png" alt="" class="wp-image-35526" srcset="https://lycos7560.com/wp-content/uploads/2023/06/image-36.png 1205w, https://lycos7560.com/wp-content/uploads/2023/06/image-36-300x24.png 300w, https://lycos7560.com/wp-content/uploads/2023/06/image-36-768x61.png 768w" sizes="(max-width: 1205px) 100vw, 1205px" /></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5698326622209671" data-ad-slot="4245812909"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2161%eb%b2%88-%ec%b9%b4%eb%93%9c1-c-baekjoon/35524/">백준 2161번 (카드1, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2161%eb%b2%88-%ec%b9%b4%eb%93%9c1-c-baekjoon/35524/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>알고리즘 – 트라이(Trie) 자료구조</title>
		<link>https://lycos7560.com/etc/%ec%95%8c%ea%b3%a0%eb%a6%ac%ec%a6%98-%ed%8a%b8%eb%9d%bc%ec%9d%b4trie-%ec%9e%90%eb%a3%8c%ea%b5%ac%ec%a1%b0/33961/</link>
					<comments>https://lycos7560.com/etc/%ec%95%8c%ea%b3%a0%eb%a6%ac%ec%a6%98-%ed%8a%b8%eb%9d%bc%ec%9d%b4trie-%ec%9e%90%eb%a3%8c%ea%b5%ac%ec%a1%b0/33961/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Tue, 14 Mar 2023 02:37:19 +0000</pubDate>
				<category><![CDATA[기타]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[leaf node]]></category>
		<category><![CDATA[retrieval]]></category>
		<category><![CDATA[root node]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[Time Complexity]]></category>
		<category><![CDATA[tree]]></category>
		<category><![CDATA[Trie]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[구조]]></category>
		<category><![CDATA[기본]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[데이터]]></category>
		<category><![CDATA[시간 복잡도]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[자료]]></category>
		<category><![CDATA[자료 구조]]></category>
		<category><![CDATA[자료구조]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[트라이]]></category>
		<category><![CDATA[트라이(Trie)]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=33961</guid>

					<description><![CDATA[<p>트라이(Trie) 자료구조에 대한 글입니다.<br />
트라이(Trie)의 자료구조의 장점 및 단점, c++ 구현에 대하여 설명합니다. (This is an article about the Trie data structure.<br />
Explain the advantages and disadvantages of the data structure of Trie and the implementation of c++.)</p>
<p>The post <a href="https://lycos7560.com/etc/%ec%95%8c%ea%b3%a0%eb%a6%ac%ec%a6%98-%ed%8a%b8%eb%9d%bc%ec%9d%b4trie-%ec%9e%90%eb%a3%8c%ea%b5%ac%ec%a1%b0/33961/">알고리즘 – 트라이(Trie) 자료구조</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>


				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-df0292cc      "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							목차 테이블						</div>
																						<div class="uagb-toc__list-wrap ">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#트라이trie" class="uagb-toc-link__trigger">트라이(Trie)?</a><ul class="uagb-toc__list"><li class="uagb-toc__list"><a href="#트라이trie의-이점" class="uagb-toc-link__trigger">트라이(Trie)의 이점</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#트라이trie의-잠재적인-단점" class="uagb-toc-link__trigger">트라이(Trie)의 잠재적인 단점</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#트라이trie의-시간-복잡도" class="uagb-toc-link__trigger">트라이(Trie)의 시간 복잡도</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#트라이trie-구현-c" class="uagb-toc-link__trigger">트라이(Trie) 구현 (C++)</a></ul></ol>					</div>
									</div>
				</div>
			


<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">트라이(Trie)?</h1>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide"/>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size">Trie data structure는 문자열을 효율적으로 저장하고 검색하는 데 사용되는 Tree와 유사한 데이터 구조입니다.</p>



<p class="has-medium-font-size">&#8220;Trie&#8221;라는 이름은 &#8220;트리&#8221;와 비슷하게 발음되는 &#8220;retrieval,&#8221;이라는 단어에서 유래되었습니다.</p>



<p class="has-medium-font-size">검색할 때 볼 수 있는&nbsp;<strong>자동완성 기능, 사전 검색 등 문자열을 탐색하는데 특화</strong>되어있는 자료구조입니다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="883" height="823" src="https://lycos7560.com/wp-content/uploads/2023/03/image-53.png" alt="" class="wp-image-33963" srcset="https://lycos7560.com/wp-content/uploads/2023/03/image-53.png 883w, https://lycos7560.com/wp-content/uploads/2023/03/image-53-300x280.png 300w, https://lycos7560.com/wp-content/uploads/2023/03/image-53-768x716.png 768w" sizes="(max-width: 883px) 100vw, 883px" /><figcaption class="wp-element-caption">https://ko.wikipedia.org/wiki/%ED%8A%B8%EB%9D%BC%EC%9D%B4_(%EC%BB%B4%ED%93%A8%ED%8C%85)</figcaption></figure>



<p class="has-medium-font-size">&#8220;A&#8221;, &#8220;to&#8221;, &#8220;tea&#8221;, &#8220;ted&#8221;, &#8220;ten&#8221;, &#8220;i&#8221;, &#8220;in&#8221;, &#8220;inn&#8221;를 키로 둔 트라이. </p>



<p class="has-medium-font-size">이 예제에는 모든 자식 노드가 알파벳 순으로 왼쪽에서 오른쪽으로 정렬되어 있지는 않다. (루트 노드와 &#8216;t&#8217; 노드)</p>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size">Trie에서 각 node는 문자열의 단일 문자를 나타내고 node에서 나오는 가장자리는 문자열의 다음 문자를 나타냅니다. </p>



<p class="has-medium-font-size">Trie의 각 문자열은 root node에서 leaf node까지의 경로로 표시됩니다. </p>



<p class="has-medium-font-size">root node에는 연결된 문자가 없으며 각 leaf node는 데이터 세트의 전체 문자열을 나타냅니다.</p>



<div style="height:30px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size">Trie는 특히 공통 접두사를 공유하는 문자열을 검색할 때 문자열을 효율적으로 저장하고 검색하는 데 자주 사용됩니다. </p>



<p class="has-medium-font-size">예를 들어 Trie를 사용하여 단어 사전을 저장할 수 있습니다. </p>



<p class="has-medium-font-size">여기서 Trie의 각 node는 단어의 접두사를 나타냅니다. </p>



<p class="has-medium-font-size">Trie를 root node에서 leaf node로 이동하면  leaf node와 관련된 전체 단어를 빠르게 검색할 수 있습니다.</p>



<div style="height:150px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">트라이(Trie)의 이점</h2>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:0;margin-bottom:0"/>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">1. 효율적인 접두사 검색</h3>



<p class="has-medium-font-size">Trie는 특히 접두사 검색 쿼리에 효율적입니다. </p>



<p class="has-medium-font-size">root node에서 접두사를 나타내는 node로 Trie를 순회하면 해당 접두사로 시작하는 데이터 세트의 모든 문자열을 검색할 수 있습니다. </p>



<p class="has-medium-font-size">이는 자동 완성, 맞춤법 검사기 및 검색 엔진과 같은 응용 프로그램에서 유용합니다.</p>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">2. 공간 효율성</h3>



<p class="has-medium-font-size">Trie는 해시 테이블과 같은 다른 데이터 구조에 비해 매우 공간 효율적일 수 있습니다. </p>



<p class="has-medium-font-size">문자열에 공통 접두사의 중복 저장을 방지하여 특히 문자열 수가 많은 데이터 세트의 경우 공간을 크게 절약할 수 있습니다.</p>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">3. 빠른 삽입 및 삭제</h3>



<p class="has-medium-font-size">Trie는 문자열의 빠른 삽입 및 삭제를 지원하며 최악의 경우 O(m)의 시간 복잡도(m 문자열의 길이)입니다. </p>



<p class="has-medium-font-size">따라서 데이터 세트가 동적이며 자주 업데이트해야 하는 애플리케이션에서 유용합니다.</p>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">4. 예측 텍스트 입력</h3>



<p class="has-medium-font-size">Trie는 자동 완성 시스템을 구현하는 데 사용할 수 있습니다. </p>



<p class="has-medium-font-size">여기서 시스템은 사용자 입력을 기반으로 다음 단어나 구문을 제안합니다. </p>



<p class="has-medium-font-size">자주 사용되는 단어와 구문의 Trie를 저장함으로써 </p>



<p class="has-medium-font-size">시스템은 사용자의 현재 입력을 기반으로 가장 가능성이 높은 다음 단어를 제안할 수 있습니다.</p>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">5. 낮은 메모리 오버헤드</h3>



<p class="has-medium-font-size">Trie는 특히 ASCII 또는 유니코드와 같은 작은 문자 세트의 경우 메모리 오버헤드가 낮습니다. </p>



<p class="has-medium-font-size">따라서 임베디드 시스템 및 기타 메모리 제약 환경에서 사용하기에 적합합니다.</p>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size">전반적으로 Trie 데이터 구조는 문자열의 데이터 세트를 효율적으로 저장하고 처리하기 위한 강력한 도구입니다. </p>



<p class="has-medium-font-size">특히 기본 사용 사례가 접두사를 검색하거나 시퀀스의 다음 단어를 예측하는 경우에 그렇습니다.</p>



<div style="height:150px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">트라이(Trie)의 잠재적인 단점</h2>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:0;margin-bottom:0"/>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">1. 메모리 요구 사항</h3>



<p class="has-medium-font-size">Trie는 특히 문자열이 길거나 문자 집합이 큰 경우 </p>



<p class="has-medium-font-size">큰 문자열 데이터 세트를 저장하기 위해 상당한 양의 메모리를 요구할 수 있습니다. </p>



<p class="has-medium-font-size">이는 메모리 사용량이 중요한 환경이나 응용 프로그램에서의 사용을 제한할 수 있습니다.</p>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">2. 접두사가 아닌 쿼리에 대한 느린 검색 시간</h3>



<p class="has-medium-font-size">Trie는 접두사 검색 쿼리에 효율적이지만 접두사가 아닌 쿼리에 대한 다른 데이터 구조보다 느릴 수 있습니다. </p>



<p class="has-medium-font-size">알고리즘이 쿼리와 일치하는 모든 문자열을 찾기 위해 각 노드의 전체 하위 트리를 탐색해야 하므로 </p>



<p class="has-medium-font-size">해시 테이블과 같은 다른 데이터 구조에 비해 검색 시간이 느려질 수 있기 때문입니다.</p>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">3. 복잡성</h3>



<p class="has-medium-font-size">Trie 데이터 구조의 구현 및 유지 관리는 </p>



<p class="has-medium-font-size">엄청나게 큰 데이터 세트의 경우 또는 문자 집합이 매우 복잡한 경우에</p>



<p class="has-medium-font-size">다른 데이터 구조보다 더 복잡할 수 있습니다. </p>



<p class="has-medium-font-size">이로 인해 개발 시간이 길어지고 개발 비용이 높아질 수 있습니다.</p>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">4. 희소 데이터 집합에 대한 비효율적인 메모리 사용</h3>



<p class="has-medium-font-size">데이터 집합에 희소 문자열이 많이 포함되어 있거나 공통 접두사가 거의 없는 경우 </p>



<p class="has-medium-font-size">Trie는 가장 메모리 효율적인 데이터 구조가 아닐 수 있습니다. </p>



<p class="has-medium-font-size">이는 Trie가 많은 빈 노드를 저장해야 하므로 메모리가 낭비될 수 있기 때문입니다.</p>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size">전반적으로 Trie 데이터 구조는 효율적인 문자열 처리를 위한 강력한 도구이지만 </p>



<p class="has-medium-font-size">응용 프로그램의 특정 요구 사항에 따라 항상 최선의 선택이 아닐 수도 있습니다.</p>



<div style="height:150px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">트라이(Trie)의 시간 복잡도</h2>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:0;margin-bottom:0"/>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size">일반적으로 Trie의 생성 시간 복잡도는 O(M x L), 탐색 시간 복잡도는 O(L)이다.</p>



<p class="has-medium-font-size">제일 긴 문자열의 길이를 L이라 하고, 총 문자열들의 수를 M이라 할 때 시간 복잡도는 위와 같다.</p>



<p class="has-medium-font-size">생성 시간 복잡도는 모든 문자열 M 개를 넣어야 하고, </p>



<p class="has-medium-font-size">M 개에 대해서 트라이에 넣는 건 가장 긴 문자열 길이인 L 만큼 걸리므로 O(MxL)의 시간 복잡도를 가진다. (삽입은 O(L)이다.)</p>



<p class="has-medium-font-size">탐색 시간 복잡도는 트리를 제일 깊게 탐색하는 경우는 가장 긴 문자열 길이인 L까지 깊게 들어가는 것이므로 O(L)의 시간 복잡도를 가진다.</p>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-default" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">트라이(Trie) 구현 (C++)</h2>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;unordered_map>

using namespace std;

// TrieNode클래스를 사용하여 Trie의 각 노드를 표현
class TrieNode {

public:
    unordered_map&lt;char, TrieNode*> children;
    bool isEndOfWord;

    TrieNode() { 
        isEndOfWord = false;
    }
    
};

// Trie 클래스
class Trie {

    // Trie의 루트 노드에 대한 포인터인 Trie개인 멤버 변수
    private: 
        TrieNode* root; // 루트 노드

// 생성자새 인스턴스를 만들고 Trie를 초기화합
    public:
        Trie() {
            root = new TrieNode();
        }


    // 문자열을 받아 Trie에 추가합니다. 
    // 루트 노드에서 시작하여 Trie 아래로 이동하여 단어의 각 문자에 필요한 새 노드를 만듭니다.
    void insert(string word) {
        TrieNode* curr = root; // 시작
        for (char c : word) {
            if (curr->children.find(c) == curr->children.end()) {
                // Trie에 없다면 새 노드 생성
                curr->children[c] = new TrieNode();
            }
            curr = curr->children[c]; // Trie에 이미 존재하는 경우 기존 노드로 이동
        }
        // 단어의 끝을 나타내는 isEndOfWord bool값
        curr->isEndOfWord = true;
    }

    // 단어를 검색합니다.
    bool search(string word) {
        TrieNode* curr = root;
        for (char c : word) {
            if (curr->children.find(c) == curr->children.end()) {
                return false;
            }
            curr = curr->children[c];
        }
        return curr->isEndOfWord;
    }

    // prefix(접두사)로 시작하는 단어가 Trie에 있는지 확인
    bool startsWith(string prefix) {
        TrieNode* curr = root;
        for (char c : prefix) {
            if (curr->children.find(c) == curr->children.end()) {
                return false;
            }
            curr = curr->children[c];
        }
        return true;
    }
};

int main() {

    Trie trie;
    trie.insert("hello");
    trie.insert("world");
    trie.insert("hello world");

    cout &lt;&lt; trie.search("hello") &lt;&lt; "\n";      // output: 1 (true)
    cout &lt;&lt; trie.search("hello world") &lt;&lt; "\n"; // output: 1 (true)
    cout &lt;&lt; trie.search("hi") &lt;&lt; "\n";         // output: 0 (false)

    cout &lt;&lt; trie.startsWith("hell") &lt;&lt; "\n";    // output: 1 (true)
    cout &lt;&lt; trie.startsWith("heaven") &lt;&lt; "\n";  // output: 0 (false)

    return 0;
}</pre>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<figure class="wp-block-embed is-type-wp-embed is-provider-어제와-내일의-나-그-사이의-이야기 wp-block-embed-어제와-내일의-나-그-사이의-이야기"><div class="wp-block-embed__wrapper">
<blockquote class="wp-embedded-content" data-secret="UKL9lnwUUR"><a href="https://lycos7560.com/cpp/programmers/programmers-17685-3%ec%b0%a8-%ec%9e%90%eb%8f%99%ec%99%84%ec%84%b1-2018-kakao-blind-recruitment/33995/">Programmers 17685 [3차] 자동완성 [2018 KAKAO BLIND RECRUITMENT]</a></blockquote><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  title="&#8220;Programmers 17685 [3차] 자동완성 [2018 KAKAO BLIND RECRUITMENT]&#8221; &#8212; 어제와 내일의 나 그 사이의 이야기" src="https://lycos7560.com/cpp/programmers/programmers-17685-3%ec%b0%a8-%ec%9e%90%eb%8f%99%ec%99%84%ec%84%b1-2018-kakao-blind-recruitment/33995/embed/#?secret=0L39VdZa1Y#?secret=UKL9lnwUUR" data-secret="UKL9lnwUUR" width="600" height="338" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
</div></figure>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;string>
#include &lt;vector>
#include &lt;unordered_map>

using namespace std;

// TrieNode클래스를 사용하여 Trie의 각 노드를 표현
class TrieNode {
public:
    unordered_map&lt;char, TrieNode*> children;
    int num; // 연관된 단어의 개수를 저장
    TrieNode() {
        num = 0;
    }
};

// Trie 클래스
class Trie {

private: // Trie의 루트 노드에 대한 포인터인 Trie개인 멤버 변수
    TrieNode* root; // 루트 노드


public:
    Trie() { // 생성자새 인스턴스를 만들고 Trie를 초기화
        root = new TrieNode();
    }

    // 문자열을 받아 Trie에 추가합니다. 
    // 루트 노드에서 시작하여 Trie 아래로 이동하여 단어의 각 문자에 필요한 새 노드를 만듭니다.
    void insert(string word) {
        TrieNode* curr = root; // 시작
        for (char c : word) {
            if (curr->children.find(c) == curr->children.end()) {
                // Trie에 없다면 새 노드 생성
                curr->children[c] = new TrieNode();
            }

            // Trie에 이미 존재하는 경우 기존 노드로 이동
            curr = curr->children[c];
            curr->num++; // 연관된 단어의 개수 추가
        }
    }

    // 단어를 검색합니다.
    // 연관된 단어의 개수가 1이라면 글자수 반환
    int search(string word) {
        TrieNode* curr = root;
        int cnt = 0;
        for (char c : word) {
            curr = curr->children[c];
            cnt++;
            if (curr->num == 1) {
                // 연관된 단어의 개수가 1이라면 
                // 자동완성
                return cnt;
            }
        }
        return 9999; // 문제에서 나올 수 없는 경우 
    }

    // prefix(접두사)로 시작하는 단어가 Trie에 있는지 확인
    int startsWith(string prefix) {
        TrieNode* curr = root;
        for (char c : prefix) {
            if (curr->children.find(c) == curr->children.end()) {
                return false;
            }
            curr = curr->children[c];
        }
        return curr->children.size();
    }

};

int solution(vector&lt;string> words) {
    int answer = 0;
    Trie trie;
    for (auto&amp; it : words) {
        trie.insert(it);
    }

    for (auto&amp; it : words) {
        if (trie.startsWith(it) >= 1) {
            // 접두사로 시작하는 단어가 있다면 
            // 단어 전체를 입력해야한다.
            answer += it.length();
            continue;
        }

        // 자동완성 확인
        answer += trie.search(it);
    }

    return answer;
}</pre>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--70);margin-bottom:var(--wp--preset--spacing--70)"/>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<p><a href="https://www.acmicpc.net/problemset?sort=ac_desc&amp;algo=79">https://www.acmicpc.net/problemset?sort=ac_desc&amp;algo=79</a> &lt;- 트라이 연습 문제 모음</p>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5698326622209671" data-ad-slot="4245812909"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/etc/%ec%95%8c%ea%b3%a0%eb%a6%ac%ec%a6%98-%ed%8a%b8%eb%9d%bc%ec%9d%b4trie-%ec%9e%90%eb%a3%8c%ea%b5%ac%ec%a1%b0/33961/">알고리즘 – 트라이(Trie) 자료구조</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/etc/%ec%95%8c%ea%b3%a0%eb%a6%ac%ec%a6%98-%ed%8a%b8%eb%9d%bc%ec%9d%b4trie-%ec%9e%90%eb%a3%8c%ea%b5%ac%ec%a1%b0/33961/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 1141번 (접두사, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1141%eb%b2%88-%ec%a0%91%eb%91%90%ec%82%ac-c-baekjoon/34016/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1141%eb%b2%88-%ec%a0%91%eb%91%90%ec%82%ac-c-baekjoon/34016/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Sat, 11 Mar 2023 18:56:40 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[1141]]></category>
		<category><![CDATA[1141번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[node]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[Trie]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[기본]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[문자열]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 1141]]></category>
		<category><![CDATA[백준 1141번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[자료 구조]]></category>
		<category><![CDATA[자료구조]]></category>
		<category><![CDATA[접두사]]></category>
		<category><![CDATA[정렬]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[트라이]]></category>
		<category><![CDATA[트라이(Trie)]]></category>
		<category><![CDATA[트리]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=34016</guid>

					<description><![CDATA[<p>백준 1141번 '접두사' 문제에 대한 글입니다. 'Trie' 자료구조를 이용하여 해결하였습니다. (This is an article about the 'prefix' problem in Baekjoon 1141. We solved it using the 'Trie' data structure.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1141%eb%b2%88-%ec%a0%91%eb%91%90%ec%82%ac-c-baekjoon/34016/">백준 1141번 (접두사, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>


				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-9d3b3ff9      "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							목차 테이블						</div>
																						<div class="uagb-toc__list-wrap ">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#접두사" class="uagb-toc-link__trigger">접두사</a><li class="uagb-toc__list"><a href="#통과된-코드" class="uagb-toc-link__trigger">통과된 코드</a></ol>					</div>
									</div>
				</div>
			


<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">접두사</h1>



<p class="has-medium-font-size"><a href="https://www.acmicpc.net/problem/1141">https://www.acmicpc.net/problem/1141</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure id="problem-info" class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">2 초</td><td class="has-text-align-left" data-align="left">128 MB</td><td class="has-text-align-left" data-align="left">2191</td><td class="has-text-align-left" data-align="left">1055</td><td class="has-text-align-left" data-align="left">877</td><td class="has-text-align-left" data-align="left">50.460%</td></tr></tbody></table></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">문제</h2>



<p class="has-medium-font-size">접두사X 집합이란 집합의 어떤 한 단어가, 다른 단어의 접두어가 되지 않는 집합이다. </p>



<p class="has-medium-font-size">예를 들어, {hello}, {hello, goodbye, giant, hi}, 비어있는 집합은 모두 접두사X 집합이다. </p>



<p class="has-medium-font-size">하지만, {hello, hell}, {giant, gig, g}는 접두사X 집합이 아니다.</p>



<p class="has-medium-font-size">단어 N개로 이루어진 집합이 주어질 때, 접두사X 집합인 부분집합의 최대 크기를 출력하시오.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">입력</h2>



<p class="has-medium-font-size">첫째 줄에 단어의 개수 N이 주어진다. N은 50보다 작거나 같은 자연수이다. </p>



<p class="has-medium-font-size">둘째 줄부터 N개의 줄에는 단어가 주어진다. </p>



<p class="has-medium-font-size">단어는 알파벳 소문자로만 이루어져 있고, 길이는 최대 50이다. </p>



<p class="has-medium-font-size">집합에는 같은 단어가 두 번 이상 있을 수 있다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출력</h2>



<p class="has-medium-font-size">첫째 줄에 문제의 정답을 출력한다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">6
hello
hi
h
run
rerun
running</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">4</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 2</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">6
a
b
cba
cbc
cbb
ccc</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 2</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">6</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 3</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">6
a
ab
abc
abcd
abcde
abcdef</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 3</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">1</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 4</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">3
topcoder
topcoder
topcoding</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 4</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">2</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출처</h2>



<ul class="wp-block-list">
<li>문제를 번역한 사람: <a href="https://www.acmicpc.net/user/baekjoon" target="_blank" rel="noreferrer noopener">baekjoon</a></li>
</ul>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">알고리즘 분류</h2>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/175" target="_blank" rel="noreferrer noopener">자료 구조</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/158" target="_blank" rel="noreferrer noopener">문자열</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/97" target="_blank" rel="noreferrer noopener">정렬</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/120" target="_blank" rel="noreferrer noopener">트리</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/79" target="_blank" rel="noreferrer noopener">트라이</a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<h1 class="wp-block-heading">통과된 코드</h1>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;set>
#include &lt;unordered_map>

using namespace std;

// TrieNode클래스를 사용하여 Trie의 각 노드를 표현
class TrieNode {

public:
    unordered_map&lt;char, TrieNode*> children;
    bool isEndOfWord;

    TrieNode() {
        isEndOfWord = false;
    }

};

// Trie 클래스
class Trie {

    // Trie의 루트 노드에 대한 포인터인 Trie개인 멤버 변수
private:
    TrieNode* root; // 루트 노드

// 생성자새 인스턴스를 만들고 Trie를 초기화합
public:
    Trie() {
        root = new TrieNode();
    }

    // 문자열을 받아 Trie에 추가합니다. 
    // 루트 노드에서 시작하여 Trie 아래로 이동하여 단어의 각 문자에 필요한 새 노드를 만듭니다.
    void insert(string word) {
        TrieNode* curr = root; // 시작
        for (char c : word) {
            if (curr->children.find(c) == curr->children.end()) 
                curr->children[c] = new TrieNode(); // Trie에 없다면 새 노드 생성
            
            curr = curr->children[c]; // Trie에 이미 존재하는 경우 기존 노드로 이동
        }
        // 단어의 끝을 나타내는 isEndOfWord bool값
        curr->isEndOfWord = true;
    }

    // prefix(접두사)로 시작하는 단어가 Trie에 있는지 확인
    int startsWith(string prefix) {
        TrieNode* curr = root;
        for (char c : prefix) {
            if (curr->children.find(c) == curr->children.end()) 
                return curr->children.size();
            
            curr = curr->children[c];
        }
        return curr->children.size();
    }
};

int main() {

    Trie trie;
    int N;
    set&lt;string> myV;
    string str;

    cin >> N;

    for (int i = 0; i &lt; N; i++) {
        cin >> str;
        myV.insert(str); // set으로 중복 제거
        trie.insert(str);
    }

    int res = myV.size();

    for (auto &amp;it : myV) 
        if (trie.startsWith(it) >= 1 ) res--;
    
    cout &lt;&lt; res;

    return 0;
}
</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://lycos7560.com/wp-content/uploads/2023/03/image-56.png" alt="" class="wp-image-34018" width="1088" height="98" srcset="https://lycos7560.com/wp-content/uploads/2023/03/image-56.png 1036w, https://lycos7560.com/wp-content/uploads/2023/03/image-56-300x27.png 300w, https://lycos7560.com/wp-content/uploads/2023/03/image-56-768x69.png 768w" sizes="(max-width: 1088px) 100vw, 1088px" /></figure>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5698326622209671" data-ad-slot="4245812909"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1141%eb%b2%88-%ec%a0%91%eb%91%90%ec%82%ac-c-baekjoon/34016/">백준 1141번 (접두사, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1141%eb%b2%88-%ec%a0%91%eb%91%90%ec%82%ac-c-baekjoon/34016/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 14426번 (접두사 찾기, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-14426%eb%b2%88-%ec%a0%91%eb%91%90%ec%82%ac-%ec%b0%be%ea%b8%b0-c-baekjoon/34046/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-14426%eb%b2%88-%ec%a0%91%eb%91%90%ec%82%ac-%ec%b0%be%ea%b8%b0-c-baekjoon/34046/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Sat, 11 Mar 2023 00:09:48 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[14426]]></category>
		<category><![CDATA[14426번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[node]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[Trie]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[기본]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[문자열]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 14426]]></category>
		<category><![CDATA[백준 14426번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[자료 구조]]></category>
		<category><![CDATA[자료구조]]></category>
		<category><![CDATA[접두사]]></category>
		<category><![CDATA[접두사 찾기]]></category>
		<category><![CDATA[정렬]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[트라이]]></category>
		<category><![CDATA[트라이(Trie)]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=34046</guid>

					<description><![CDATA[<p>백준 14426번 '접두사 찾기' 문제에 대한 글입니다. '트라이' 자료구조를 이용하여 해결하였습니다. (This is an article on the 'find prefix' problem in Baekjun 14426. We solved it using the 'Trie' data structure.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-14426%eb%b2%88-%ec%a0%91%eb%91%90%ec%82%ac-%ec%b0%be%ea%b8%b0-c-baekjoon/34046/">백준 14426번 (접두사 찾기, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>


				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-2709ce82      "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							목차 테이블						</div>
																						<div class="uagb-toc__list-wrap ">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#접두사-찾기" class="uagb-toc-link__trigger">접두사 찾기</a><li class="uagb-toc__list"><a href="#통과된-코드" class="uagb-toc-link__trigger">통과된 코드</a></ol>					</div>
									</div>
				</div>
			


<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">접두사 찾기</h1>



<p class="has-medium-font-size"><a href="https://www.acmicpc.net/problem/14426" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/14426</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure id="problem-info" class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">1 초</td><td class="has-text-align-left" data-align="left">1536 MB</td><td class="has-text-align-left" data-align="left">2587</td><td class="has-text-align-left" data-align="left">781</td><td class="has-text-align-left" data-align="left">574</td><td class="has-text-align-left" data-align="left">49.228%</td></tr></tbody></table></figure>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">문제</h2>



<p class="has-medium-font-size">문자열 S의 접두사란 S의 가장 앞에서부터 부분 문자열을 의미한다. </p>



<p class="has-medium-font-size">예를 들어, S = &#8220;codeplus&#8221;의 접두사는 &#8220;code&#8221;, &#8220;co&#8221;, &#8220;codepl&#8221;, &#8220;codeplus&#8221;가 있고, &#8220;plus&#8221;, &#8220;s&#8221;, &#8220;cude&#8221;, &#8220;crud&#8221;는 접두사가 아니다.</p>



<p class="has-medium-font-size">총 N개의 문자열로 이루어진 집합 S가 주어진다.</p>



<p class="has-medium-font-size">입력으로 주어지는 M개의 문자열 중에서 집합 S에 포함되어 있는 문자열 중 </p>



<p class="has-medium-font-size">적어도 하나의 접두사인 것의 개수를 구하는 프로그램을 작성하시오.</p>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">입력</h2>



<p class="has-medium-font-size">첫째 줄에 문자열의 개수 N과 M (1 ≤ N ≤ 10,000, 1 ≤ M ≤ 10,000)이 주어진다.</p>



<p class="has-medium-font-size">다음 N개의 줄에는 집합 S에 포함되어 있는 문자열이 주어진다.</p>



<p class="has-medium-font-size">다음 M개의 줄에는 검사해야 하는 문자열이 주어진다.</p>



<p class="has-medium-font-size">입력으로 주어지는 문자열은 알파벳 소문자로만 이루어져 있으며, 길이는 500을 넘지 않는다. </p>



<p class="has-medium-font-size">집합 S에 같은 문자열이 여러 번 주어지는 경우는 없다.</p>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출력</h2>



<p class="has-medium-font-size">첫째 줄에 M개의 문자열 중에 총 몇 개가 포함되어 있는 문자열 중 적어도 하나의 접두사인지 출력한다.</p>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">5 10
baekjoononlinejudge
startlink
codeplus
sundaycoding
codingsh
baekjoon
star
start
code
sunday
coding
cod
online
judge
plus</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">7</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출처</h2>



<ul class="wp-block-list">
<li>문제를 만든 사람:&nbsp;<a href="https://www.acmicpc.net/user/baekjoon" target="_blank" rel="noreferrer noopener">baekjoon</a></li>



<li>데이터를 추가한 사람:&nbsp;<a href="https://www.acmicpc.net/user/besyia0k0" target="_blank" rel="noreferrer noopener">besyia0k0</a>,&nbsp;<a href="https://www.acmicpc.net/user/jh05013" target="_blank" rel="noreferrer noopener">jh05013</a></li>



<li>문제의 오타를 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/Green55" target="_blank" rel="noreferrer noopener">Green55</a></li>
</ul>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">알고리즘 분류</h2>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/175" target="_blank" rel="noreferrer noopener">자료 구조</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/158" target="_blank" rel="noreferrer noopener">문자열</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/120" target="_blank" rel="noreferrer noopener">트리</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/79" target="_blank" rel="noreferrer noopener">트라이</a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<h1 class="wp-block-heading">통과된 코드</h1>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;unordered_map>

using namespace std;

// TrieNode클래스를 사용하여 Trie의 각 노드를 표현
class TrieNode {

public:
    unordered_map&lt;char, TrieNode*> children;
};

// Trie 클래스
class Trie {

    // Trie의 루트 노드에 대한 포인터인 Trie개인 멤버 변수
private:
    TrieNode* root; // 루트 노드

// 생성자새 인스턴스를 만들고 Trie를 초기화합
public:
    Trie() {
        root = new TrieNode();
    }

    // 문자열을 받아 Trie에 추가합니다. 
    // 루트 노드에서 시작하여 Trie 아래로 이동하여 단어의 각 문자에 필요한 새 노드를 만듭니다.
    void insert(string word) {
        TrieNode* curr = root; // 시작
        for (char c : word) {
            if (curr->children.find(c) == curr->children.end())
                curr->children[c] = new TrieNode(); // Trie에 없다면 새 노드 생성

            curr = curr->children[c]; // Trie에 이미 존재하는 경우 기존 노드로 이동
        }
    }

    // prefix(접두사)로 시작하는 단어가 Trie에 있는지 확인
    bool startsWith(string prefix) {
        TrieNode* curr = root;
        for (char c : prefix) {
            if (curr->children.find(c) == curr->children.end()) {
                return false;
            }
            curr = curr->children[c];
        }
        return true;
    }
};

int main() {

    ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화
    // cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다.
    cin.tie(NULL);
    cout.tie(NULL);

    Trie trie;
    int N, M, res = 0;
    string str;

    cin >> N >> M;

    for (int i = 0; i &lt; N; i++) {
        cin >> str;
        trie.insert(str); // 트라이 입력
    }

    for (int i = 0; i &lt; M; i++) {
        cin >> str;
        if (trie.startsWith(str)) res++; // 접두사 확인
    }

    cout &lt;&lt; res;

    return 0;
}</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="906" height="95" src="https://lycos7560.com/wp-content/uploads/2023/03/image-57.png" alt="" class="wp-image-34049" srcset="https://lycos7560.com/wp-content/uploads/2023/03/image-57.png 906w, https://lycos7560.com/wp-content/uploads/2023/03/image-57-300x31.png 300w, https://lycos7560.com/wp-content/uploads/2023/03/image-57-768x81.png 768w" sizes="(max-width: 906px) 100vw, 906px" /></figure>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5698326622209671" data-ad-slot="4245812909"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-14426%eb%b2%88-%ec%a0%91%eb%91%90%ec%82%ac-%ec%b0%be%ea%b8%b0-c-baekjoon/34046/">백준 14426번 (접두사 찾기, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-14426%eb%b2%88-%ec%a0%91%eb%91%90%ec%82%ac-%ec%b0%be%ea%b8%b0-c-baekjoon/34046/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 17352번 (여러분의 다리가 되어 드리겠습니다!, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-17352%eb%b2%88-%ec%97%ac%eb%9f%ac%eb%b6%84%ec%9d%98-%eb%8b%a4%eb%a6%ac%ea%b0%80-%eb%90%98%ec%96%b4-%eb%93%9c%eb%a6%ac%ea%b2%a0%ec%8a%b5%eb%8b%88%eb%8b%a4-c-baekjoon/33762/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-17352%eb%b2%88-%ec%97%ac%eb%9f%ac%eb%b6%84%ec%9d%98-%eb%8b%a4%eb%a6%ac%ea%b0%80-%eb%90%98%ec%96%b4-%eb%93%9c%eb%a6%ac%ea%b2%a0%ec%8a%b5%eb%8b%88%eb%8b%a4-c-baekjoon/33762/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Mon, 06 Mar 2023 12:32:40 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[17352]]></category>
		<category><![CDATA[17352번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[Union-Find]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[그래프 이론]]></category>
		<category><![CDATA[그래프 탐색]]></category>
		<category><![CDATA[기본]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 17352]]></category>
		<category><![CDATA[백준 17352번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[여러분의 다리가 되어 드리겠습니다!]]></category>
		<category><![CDATA[유니온 파인드]]></category>
		<category><![CDATA[자료구조]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[회의준비]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=33762</guid>

					<description><![CDATA[<p>백준 17352번 '여러분의 다리가 되어 드리겠습니다!' 문제에 대한 글입니다. union find 알고리즘을 이용하여 해결하였습니다. (This is Baekjoon 17352 about the question, "We will be your legs!" The solution was solved using the union find algorithm.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-17352%eb%b2%88-%ec%97%ac%eb%9f%ac%eb%b6%84%ec%9d%98-%eb%8b%a4%eb%a6%ac%ea%b0%80-%eb%90%98%ec%96%b4-%eb%93%9c%eb%a6%ac%ea%b2%a0%ec%8a%b5%eb%8b%88%eb%8b%a4-c-baekjoon/33762/">백준 17352번 (여러분의 다리가 되어 드리겠습니다!, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>


				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-f324320a      "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							목차 테이블						</div>
																						<div class="uagb-toc__list-wrap ">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#여러분의-다리가-되어-드리겠습니다" class="uagb-toc-link__trigger">여러분의 다리가 되어 드리겠습니다!</a><li class="uagb-toc__list"><a href="#접근-방법" class="uagb-toc-link__trigger">접근 방법</a><li class="uagb-toc__list"><a href="#통과된-코드" class="uagb-toc-link__trigger">통과된 코드</a></ol>					</div>
									</div>
				</div>
			


<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">여러분의 다리가 되어 드리겠습니다!</h1>



<p class="has-medium-font-size"><a href="https://www.acmicpc.net/problem/17352" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/17352</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure id="problem-info" class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">1 초</td><td class="has-text-align-left" data-align="left">512 MB</td><td class="has-text-align-left" data-align="left">2632</td><td class="has-text-align-left" data-align="left">1332</td><td class="has-text-align-left" data-align="left">1041</td><td class="has-text-align-left" data-align="left">51.791%</td></tr></tbody></table></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">문제</h2>



<p class="has-medium-font-size">선린월드에는&nbsp;<em>N</em>개의 섬이 있다. 섬에는 1, 2, &#8230;,&nbsp;<em>N</em>의 번호가 하나씩 붙어 있다.&nbsp;</p>



<p class="has-medium-font-size">그 섬들을&nbsp;<em>N&nbsp;</em>&#8211; 1개의 다리가 잇고 있으며, 어떤 두 섬 사이든 다리로 왕복할 수 있다.</p>



<p class="has-medium-font-size"><strong>어제까지는 그랬다.</strong></p>



<p class="has-medium-font-size">&#8220;왜 다리가&nbsp;<em>N&nbsp;</em>&#8211; 1개밖에 없냐, 통행하기 불편하다&#8221;며 선린월드에 불만을 갖던 욱제가 다리 하나를 무너뜨렸다! </p>



<p class="has-medium-font-size">안 그래도 불편한 통행이 더 불편해졌다. 서로 왕복할 수 없는 섬들이 생겼기 때문이다. </p>



<p class="has-medium-font-size">일단 급한 대로 정부는 선린월드의 건축가를 고용해, </p>



<p class="has-medium-font-size">서로 다른 두 섬을 다리로 이어서 다시 어떤 두 섬 사이든 왕복할 수 있게 하라는 지시를 내렸다.</p>



<p class="has-medium-font-size">그런데 그 건축가가 당신이다! 안 그래도 천하제일 코딩대회에 참가하느라 바쁜데&#8230;</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">입력</h2>



<p class="has-medium-font-size">첫 줄에 정수&nbsp;<em>N</em>이 주어진다. (2&nbsp;≤&nbsp;<em>N</em>&nbsp;≤ 300,000)</p>



<p class="has-medium-font-size">그 다음&nbsp;<em>N</em>&nbsp;&#8211; 2개의 줄에는 욱제가 무너뜨리지 않은 다리들이 잇는 두 섬의 번호가 주어진다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출력</h2>



<p class="has-medium-font-size">다리로 이을 두 섬의 번호를 출력한다. </p>



<p class="has-medium-font-size">여러 가지 방법이 있을 경우 그 중 아무거나&nbsp;한 방법만 출력한다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">4
1 2
1 3</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">1 4</pre>



<div style="height:15px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size">&#8220;4 1&#8243;이나 &#8220;2 4&#8221;, &#8220;4 3&#8221; 등 가능한 정답은 많이 있지만, 아무거나 하나만 출력해야 한다.</p>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 2</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">2</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 2</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">1 2</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 3</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">5
1 2
2 3
4 5</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 3</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">3 4</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출처</h2>



<p><a href="https://www.acmicpc.net/category/97" target="_blank" rel="noreferrer noopener">High School</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/394" target="_blank" rel="noreferrer noopener">선린인터넷고등학교</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/detail/2052" target="_blank" rel="noreferrer noopener">제3회 천하제일 코딩대회</a>&nbsp;D번</p>



<ul class="wp-block-list">
<li>문제를 만든 사람:&nbsp;<a href="https://www.acmicpc.net/user/jh05013" target="_blank" rel="noreferrer noopener">jh05013</a></li>
</ul>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">알고리즘 분류</h2>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/175" target="_blank" rel="noreferrer noopener">자료 구조</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/7" target="_blank" rel="noreferrer noopener">그래프 이론</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/11" target="_blank" rel="noreferrer noopener">그래프 탐색</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/81" target="_blank" rel="noreferrer noopener">분리 집합</a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<h1 class="wp-block-heading">접근 방법</h1>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1600" height="1244" src="https://lycos7560.com/wp-content/uploads/2023/03/Notes_230306_212602.jpg" alt="" class="wp-image-33769" srcset="https://lycos7560.com/wp-content/uploads/2023/03/Notes_230306_212602.jpg 1600w, https://lycos7560.com/wp-content/uploads/2023/03/Notes_230306_212602-300x233.jpg 300w, https://lycos7560.com/wp-content/uploads/2023/03/Notes_230306_212602-768x597.jpg 768w, https://lycos7560.com/wp-content/uploads/2023/03/Notes_230306_212602-1536x1194.jpg 1536w" sizes="(max-width: 1600px) 100vw, 1600px" /></figure>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">통과된 코드</h1>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<div style="height:0px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>

using namespace std;

constexpr int MAX = 300001;

int N, v, u;

int parent[MAX];

void MakeSet(int n)
{
	for (int i = 1; i &lt;= n; i++)  parent[i] = i;
}

int Find(int x) {

	if (parent[x] != x) parent[x] = Find(parent[x]); // Path compression

	return parent[x];
}

void Unite(int x, int y) {
	int rootX = Find(x);
	int rootY = Find(y);
	parent[rootY] = rootX; // Make rootX the parent of rootY
}

int main()
{
	ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화
	// cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다.
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> N;
	MakeSet(N);
	for (int i = 0; i &lt; N - 2; i++) {
		cin >> v >> u;
		Unite(v, u);
	}

	int first, second = -1;
	first = Find(1);
	for (int i = 2; i &lt;= N; i++) {
		if (Find(i) != first) {
			second = Find(i);
			break;
		}
	}

	cout &lt;&lt; first &lt;&lt; " " &lt;&lt; second;

	return 0;
}</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1031" height="185" src="https://lycos7560.com/wp-content/uploads/2023/03/image-24.png" alt="" class="wp-image-33765" srcset="https://lycos7560.com/wp-content/uploads/2023/03/image-24.png 1031w, https://lycos7560.com/wp-content/uploads/2023/03/image-24-300x54.png 300w, https://lycos7560.com/wp-content/uploads/2023/03/image-24-768x138.png 768w" sizes="(max-width: 1031px) 100vw, 1031px" /></figure>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5698326622209671" data-ad-slot="4245812909"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-17352%eb%b2%88-%ec%97%ac%eb%9f%ac%eb%b6%84%ec%9d%98-%eb%8b%a4%eb%a6%ac%ea%b0%80-%eb%90%98%ec%96%b4-%eb%93%9c%eb%a6%ac%ea%b2%a0%ec%8a%b5%eb%8b%88%eb%8b%a4-c-baekjoon/33762/">백준 17352번 (여러분의 다리가 되어 드리겠습니다!, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-17352%eb%b2%88-%ec%97%ac%eb%9f%ac%eb%b6%84%ec%9d%98-%eb%8b%a4%eb%a6%ac%ea%b0%80-%eb%90%98%ec%96%b4-%eb%93%9c%eb%a6%ac%ea%b2%a0%ec%8a%b5%eb%8b%88%eb%8b%a4-c-baekjoon/33762/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 2610번 (회의준비, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2610%eb%b2%88-%ed%9a%8c%ec%9d%98%ec%a4%80%eb%b9%84-c-baekjoon/33706/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2610%eb%b2%88-%ed%9a%8c%ec%9d%98%ec%a4%80%eb%b9%84-c-baekjoon/33706/#comments</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Sat, 04 Mar 2023 16:13:32 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[2610]]></category>
		<category><![CDATA[2610번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Floyd-Warshall]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[Union-Find]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[그래프 이론]]></category>
		<category><![CDATA[그래프 탐색]]></category>
		<category><![CDATA[기본]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 2610]]></category>
		<category><![CDATA[백준 2610번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[유니온 파인드]]></category>
		<category><![CDATA[자료구조]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[플로이드]]></category>
		<category><![CDATA[플로이드 워셜]]></category>
		<category><![CDATA[회의준비]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=33706</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 2610번 '회의준비' 문제에 대한 글입니다. 'Floyd-Warshall' &#038; 'Union Find' 알고리즘을 사용하여 해결하였습니다. (This is an article on the 'meeting preparation' problem in BAEKJOON No. 2610. Solved using 'Floyd-Warshall' &#038; 'UnionFind' algorithms.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2610%eb%b2%88-%ed%9a%8c%ec%9d%98%ec%a4%80%eb%b9%84-c-baekjoon/33706/">백준 2610번 (회의준비, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>


				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-46c59bdb      "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							목차 테이블						</div>
																						<div class="uagb-toc__list-wrap ">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#회의준비" class="uagb-toc-link__trigger">회의준비</a><ul class="uagb-toc__list"><li class="uagb-toc__list"><a href="#추가-반례" class="uagb-toc-link__trigger">추가 반례</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#통과된-코드" class="uagb-toc-link__trigger">통과된 코드</a></ul></ol>					</div>
									</div>
				</div>
			


<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">회의준비</h1>



<p class="has-medium-font-size"><a href="https://www.acmicpc.net/problem/2610" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/2610</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure id="problem-info" class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">1 초</td><td class="has-text-align-left" data-align="left">128 MB</td><td class="has-text-align-left" data-align="left">7575</td><td class="has-text-align-left" data-align="left">2180</td><td class="has-text-align-left" data-align="left">1640</td><td class="has-text-align-left" data-align="left">28.616%</td></tr></tbody></table></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">문제</h3>



<p class="has-medium-font-size">KOI 준비를 위해 회의를 개최하려 한다. </p>



<p class="has-medium-font-size">주최측에서는 회의에 참석하는 사람의 수와 참석자들 사이의 관계를 따져 하나 이상의 위원회를 구성하려고 한다. </p>



<p class="has-medium-font-size">위원회를 구성하는 방식은 다음과 같다.</p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size">1. 서로 알고 있는 사람은 반드시 같은 위원회에 속해야 한다.</p>



<p class="has-medium-font-size">2. 효율적인 회의 진행을 위해 위원회의 수는 최대가 되어야 한다.</p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size">이런 방식으로 위원회를 구성한 후에 각 위원회의 대표를 한 명씩 뽑아야 한다. </p>



<p class="has-medium-font-size">각 위원회의 대표만이 회의 시간 중 발언권을 가지며, </p>



<p class="has-medium-font-size">따라서 회의 참석자들이 자신의 의견을 말하기 위해서는 자신이 속한 위원회의 대표에게 자신의 의견을 전달해야 한다. </p>



<p class="has-medium-font-size">그런데 각 참석자는 자신이 알고 있는 사람에게만 의견을 전달할 수 있어 대표에게 의견을 전달하기 위해서는 때로 여러 사람을 거쳐야 한다. </p>



<p class="has-medium-font-size">대표에게 의견을 전달하는 경로가 여러 개 있을 경우에는 가장 적은 사람을 거치는 경로로 의견을 전달하며 </p>



<p class="has-medium-font-size">이때 거치는 사람의 수를 참석자의 의사전달시간이라고 한다.</p>



<p class="has-medium-font-size">위원회에서 모든 참석자들의 의사전달시간 중 최댓값이 최소가 되도록 대표를 정하는 프로그램을 작성하시오.</p>



<p class="has-medium-font-size">예를 들어 1번, 2번, 3번 세 사람으로 구성되어 있는 위원회에서 1번과 2번, 2번과 3번이 서로 알고 있다고 하자. </p>



<p class="has-medium-font-size">1번이 대표가 되면 3번이 대표인 1번에게 의견을 전달하기 위해서 2번을 거쳐야만 한다. </p>



<p class="has-medium-font-size">반대로 3번이 대표가 되어도 1번이 대표인 3번에게 의견을 전달하려면 2번을 거쳐야만 한다. </p>



<p class="has-medium-font-size">하지만 2번이 대표가 되면 1번과 3번 둘 다 아무도 거치지 않고 대표에게 직접 의견을 전달 할 수 있다. </p>



<p class="has-medium-font-size">따라서 이와 같은 경우 2번이 대표가 되어야 한다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">입력</h3>



<p class="has-medium-font-size">첫째 중에 회의에 참석하는 사람의 수 N이 주어진다. </p>



<p class="has-medium-font-size">참석자들은 1부터 N까지의 자연수로 표현되며 회의에 참석하는 인원은 100 이하이다. </p>



<p class="has-medium-font-size">둘째 줄에는 서로 알고 있는 관계의 수 M이 주어진다. </p>



<p class="has-medium-font-size">이어 M개의 각 줄에는 서로 아는 사이인 참석자를 나타내는 두개의 자연수가 주어진다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">출력</h3>



<p class="has-medium-font-size">첫째 줄에는 구성되는 위원회의 수 K를 출력한다. </p>



<p class="has-medium-font-size">다음 K줄에는 각 위원회의 대표 번호를 작은 수부터 차례로 한 줄에 하나씩 출력한다. </p>



<p class="has-medium-font-size">한 위원회의 대표가 될 수 있는 사람이 둘 이상일 경우 그중 한 명만 출력하면 된다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 입력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">8
7
1 2
2 3
4 5
5 6
4 6
6 7
7 4</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 출력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">3
2
4
8</pre>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading has-large-font-size">추가 반례</h2>



<h3 class="wp-block-heading has-large-font-size">예제 입력 A</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">100
0</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 출력 A</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">100
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 입력 B</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">9
8
1 2
2 3
3 4
4 5
4 6
4 7
4 8
4 9</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 출력 B</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">1
3</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 입력 C</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">5
4
5 1
1 2
2 3
3 4</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 출력 C</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">1
2</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">출처</h3>



<p><a href="https://www.acmicpc.net/category/2" target="_blank" rel="noreferrer noopener">Olympiad</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/55" target="_blank" rel="noreferrer noopener">한국정보올림피아드</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/57" target="_blank" rel="noreferrer noopener">한국정보올림피아드시․도지역본선</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/74">지</a><a href="https://www.acmicpc.net/category/74" target="_blank" rel="noreferrer noopener">역</a><a href="https://www.acmicpc.net/category/74">본선 2004</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/detail/380" target="_blank" rel="noreferrer noopener">중등부</a>&nbsp;3번</p>



<p><a href="https://www.acmicpc.net/category/2" target="_blank" rel="noreferrer noopener">Olympiad</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/55" target="_blank" rel="noreferrer noopener">한국정보올림피아드</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/57" target="_blank" rel="noreferrer noopener">한국정보올림피아드시․도지역본선</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/74">지역</a><a href="https://www.acmicpc.net/category/74" target="_blank" rel="noreferrer noopener">본</a><a href="https://www.acmicpc.net/category/74">선 2004</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/detail/381" target="_blank" rel="noreferrer noopener">고등부</a>&nbsp;3번</p>



<ul class="wp-block-list">
<li>데이터를 추가한 사람:&nbsp;<a href="https://www.acmicpc.net/user/eightco" target="_blank" rel="noreferrer noopener">eightco</a></li>



<li>문제의 오타를 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/sky1357" target="_blank" rel="noreferrer noopener">sky1357</a></li>
</ul>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">알고리즘 분류</h3>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/175" target="_blank" rel="noreferrer noopener">자료 구조</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/7" target="_blank" rel="noreferrer noopener">그래프 이론</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/11" target="_blank" rel="noreferrer noopener">그래프 탐색</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/81" target="_blank" rel="noreferrer noopener">분리 집합</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/31" target="_blank" rel="noreferrer noopener">플로이드–워셜</a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<h2 class="wp-block-heading has-large-font-size">통과된 코드</h2>



<p class="has-medium-font-size">Floyd-Warshall 알고리즘을 이용하여  의사전달 시간의 최솟값을 구한다.</p>



<p class="has-medium-font-size">그 후에 Union-Find를 이용하여 위원회의 개수와 대표를 구하면 된다.</p>



<p class="has-medium-font-size">(분리 집합 + 플로이드 와샬 문제)</p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;vector>
#include &lt;set>

using namespace std;

// N : 참석하는 사람
int N, K, f, b;

constexpr int INF = INT32_MAX/2;

constexpr int MAX = 101;

vector&lt;pair&lt;int, int>> graph[MAX];

int disArr[MAX][MAX];

int sumArr[MAX];

// 유니온 root노드 배열
int uArr[MAX];

set&lt;int> checkSet;

//부모를 찾는 함수
//모든 경로가 부모를 가르키게 함
//상수 시간의 복잡도
int Find(int x)
{
	if (uArr[x] == x) return x;
	else return uArr[x] = Find(uArr[x]);
}

//두 노드를 연결 시키는 것
//기준을 정해서 연결시키는 것이 헷갈리지 않음
//작은쪽이 부모 or 큰쪽이 부모
void Union(int x, int y)
{
	x = Find(x);
	y = Find(y);
	if (x != y) {
		if (sumArr[x] &lt;= sumArr[y]) uArr[y] = x;
		else uArr[x] = y;
	}
}

int main()
{
	ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화
	// cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다.
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> N >> K;

	// 유니온 배열 초기화
	for (int i = 1; i &lt;= N; i++) uArr[i] = i;

	// 서로를 알고 있음(양방향)
	for (int i = 0; i &lt; K; i++) {
		cin >> f >> b;
		graph[f].push_back(make_pair(b, 1));
		graph[b].push_back(make_pair(f, 1));
	}

	// 최단 거리 배열 disArr 배열을 INF 초기화
	for (int i = 1; i &lt;= N; i++) {
		for (int j = 1; j &lt;= N; j++) {
			disArr[i][j] = INF;
		}
	}

	for (int i = 1; i &lt;= N; i++) {
		for (int j = 0; j &lt; graph[i].size(); j++) {
			int v = graph[i][j].first;
			int weight = graph[i][j].second;
			disArr[i][v] = weight;
		}
	}

	// Floyd-Warshall 알고리즘
	for (int k = 1; k &lt;= N; k++) {
		for (int i = 1; i &lt;= N; i++) {
			for (int j = 1; j &lt;= N; j++) {
				if (i == j) disArr[i][j] = 0;
				else disArr[i][j] = min(disArr[i][j], disArr[i][k] + disArr[k][j]);
			}
		}
	}

	for (int i = 1; i &lt;= N; i++) {
		for (int j = 1; j &lt;= N; j++) {
			
			if (disArr[i][j] == INF) continue;
			// 의사전달시간 중 최대값이 최소가 되도록 대표를 선출
			sumArr[i] = max(sumArr[i], disArr[i][j]); 
		}
	}

	for (int i = 1; i &lt;= N; i++) {
		for (int j = 0; j &lt; graph[i].size(); j++) {
			// Union Find를 사용하여 위원회를 구성
			Union(i, graph[i][j].first);
		}
	}

	// 결과를 출력
	// Set을 이용하여 위원회의 개수와 대표를 저장
	for (int i = 1; i &lt;= N; i++) checkSet.insert(Find(i));
	
	cout &lt;&lt; checkSet.size() &lt;&lt; "\n";
	for (auto&amp; it : checkSet) cout &lt;&lt; it &lt;&lt; "\n";

	return 0;
}</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="913" height="171" src="https://lycos7560.com/wp-content/uploads/2023/03/image-20.png" alt="" class="wp-image-33710" srcset="https://lycos7560.com/wp-content/uploads/2023/03/image-20.png 913w, https://lycos7560.com/wp-content/uploads/2023/03/image-20-300x56.png 300w, https://lycos7560.com/wp-content/uploads/2023/03/image-20-768x144.png 768w" sizes="(max-width: 913px) 100vw, 913px" /></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5698326622209671" data-ad-slot="4245812909"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2610%eb%b2%88-%ed%9a%8c%ec%9d%98%ec%a4%80%eb%b9%84-c-baekjoon/33706/">백준 2610번 (회의준비, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2610%eb%b2%88-%ed%9a%8c%ec%9d%98%ec%a4%80%eb%b9%84-c-baekjoon/33706/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>백준 16928번 (뱀과 사다리 게임, C++, BFS) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-16928%eb%b2%88-%eb%b1%80%ea%b3%bc-%ec%82%ac%eb%8b%a4%eb%a6%ac-%ea%b2%8c%ec%9e%84-c-bfs-baekjoon/6264/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-16928%eb%b2%88-%eb%b1%80%ea%b3%bc-%ec%82%ac%eb%8b%a4%eb%a6%ac-%ea%b2%8c%ec%9e%84-c-bfs-baekjoon/6264/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Wed, 08 Feb 2023 19:23:11 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[16928]]></category>
		<category><![CDATA[16928번]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[BFS]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Search]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[그래프 이론]]></category>
		<category><![CDATA[그래프 탐색]]></category>
		<category><![CDATA[기본]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[너비]]></category>
		<category><![CDATA[너비 우선 탐색]]></category>
		<category><![CDATA[반례]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 16928]]></category>
		<category><![CDATA[백준 16928번]]></category>
		<category><![CDATA[뱀과 사다리 게임]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[예제]]></category>
		<category><![CDATA[자료구조]]></category>
		<category><![CDATA[추가 반례]]></category>
		<category><![CDATA[추가 예제]]></category>
		<category><![CDATA[추가반례]]></category>
		<category><![CDATA[추가예제]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=6264</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 16928번 '뱀과 사다리 게임' 문제에 대한 글입니다.<br />
'BFS 탐색'을 이용하여 해결하였습니다. (This is an article on the question of 'The Snake and the Ladder Game' in BAEKJOON No. 16928.<br />
Resolved using 'BFS Search'.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-16928%eb%b2%88-%eb%b1%80%ea%b3%bc-%ec%82%ac%eb%8b%a4%eb%a6%ac-%ea%b2%8c%ec%9e%84-c-bfs-baekjoon/6264/">백준 16928번 (뱀과 사다리 게임, C++, BFS) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>


				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-24997ec6      "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							목차 테이블						</div>
																						<div class="uagb-toc__list-wrap ">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#뱀과-사다리-게임" class="uagb-toc-link__trigger">뱀과 사다리 게임</a><li class="uagb-toc__list"><a href="#접근-방법" class="uagb-toc-link__trigger">접근 방법</a><li class="uagb-toc__list"><a href="#통과된-코드" class="uagb-toc-link__trigger">통과된 코드</a><li class="uagb-toc__list"><a href="#추가-반례" class="uagb-toc-link__trigger">추가 반례</a></ol>					</div>
									</div>
				</div>
			


<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">뱀과 사다리 게임</h1>



<p class="has-medium-font-size"><a href="https://www.acmicpc.net/problem/16928" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/16928</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure id="problem-info" class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">1 초</td><td class="has-text-align-left" data-align="left">512 MB</td><td class="has-text-align-left" data-align="left">23567</td><td class="has-text-align-left" data-align="left">8517</td><td class="has-text-align-left" data-align="left">6445</td><td class="has-text-align-left" data-align="left">33.294%</td></tr></tbody></table></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">문제</h2>



<p class="has-medium-font-size"><a href="https://en.wikipedia.org/wiki/Snakes_and_Ladders" target="_blank" rel="noreferrer noopener">뱀과 사다리 게임</a>을 즐겨 하는 큐브러버는 어느 날 궁금한 점이 생겼다.</p>



<p class="has-medium-font-size">주사위를 조작해 내가 원하는 수가 나오게 만들 수 있다면, 최소 몇 번만에 도착점에 도착할 수 있을까?</p>



<p class="has-medium-font-size">게임은 정육면체 주사위를 사용하며, 주사위의 각 면에는 1부터 6까지 수가 하나씩 적혀있다. </p>



<p class="has-medium-font-size">게임은 크기가 10×10이고, 총 100개의 칸으로 나누어져 있는 보드판에서 진행된다. </p>



<p class="has-medium-font-size">보드판에는 1부터 100까지 수가 하나씩 순서대로 적혀져 있다.</p>



<p class="has-medium-font-size">플레이어는 주사위를 굴려 나온 수만큼 이동해야 한다. </p>



<p class="has-medium-font-size">예를 들어, 플레이어가 i번 칸에 있고, 주사위를 굴려 나온 수가 4라면, i+4번 칸으로 이동해야 한다. </p>



<p class="has-medium-font-size">만약 주사위를 굴린 결과가 100번 칸을 넘어간다면 이동할 수 없다. </p>



<p class="has-medium-font-size">도착한 칸이 사다리면, 사다리를 타고 위로 올라간다. </p>



<p class="has-medium-font-size">뱀이 있는 칸에 도착하면, 뱀을 따라서 내려가게 된다. </p>



<p class="has-medium-font-size">즉, 사다리를 이용해 이동한 칸의 번호는 원래 있던 칸의 번호보다 크고, </p>



<p class="has-medium-font-size">뱀을 이용해 이동한 칸의 번호는 원래 있던 칸의 번호보다 작아진다.</p>



<p class="has-medium-font-size">게임의 목표는 1번 칸에서 시작해서 100번 칸에 도착하는 것이다.</p>



<p class="has-medium-font-size">게임판의 상태가 주어졌을 때, 100번 칸에 도착하기 위해 주사위를 굴려야 하는 횟수의 최솟값을 구해보자.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">입력</h2>



<p class="has-medium-font-size">첫째 줄에 게임판에 있는 사다리의 수 N(1 ≤ N ≤ 15)과 뱀의 수 M(1 ≤ M ≤ 15)이 주어진다.</p>



<p class="has-medium-font-size">둘째 줄부터 N개의 줄에는 사다리의 정보를 의미하는 x, y (x &lt; y)가 주어진다.</p>



<p class="has-medium-font-size">x번 칸에 도착하면, y번 칸으로 이동한다는 의미이다.</p>



<p class="has-medium-font-size">다음 M개의 줄에는 뱀의 정보를 의미하는 u, v (u &gt; v)가 주어진다. </p>



<p class="has-medium-font-size">u번 칸에 도착하면, v번 칸으로 이동한다는 의미이다.</p>



<p class="has-medium-font-size">1번 칸과 100번 칸은 뱀과 사다리의 시작 또는 끝이 아니다. </p>



<p class="has-medium-font-size">모든 칸은 최대 하나의 사다리 또는 뱀을 가지고 있으며, 동시에 두 가지를 모두 가지고 있는 경우는 없다. </p>



<p class="has-medium-font-size">항상 100번 칸에 도착할 수 있는 입력만 주어진다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출력</h2>



<p class="has-medium-font-size">100번 칸에 도착하기 위해 주사위를 최소 몇 번 굴려야 하는지 출력한다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">3 7
32 62
42 68
12 98
95 13
97 25
93 37
79 27
75 19
49 47
67 17</pre>



<div style="height:30px" aria-hidden="true" class="wp-block-spacer"></div>



<ol class="wp-block-list">
<li>5를 굴려 6으로 이동한다.</li>



<li>6을 굴려 12로 이동한다. 이 곳은 98로 이동하는 사다리가 있기 때문에, 98로 이동한다.</li>



<li>2를 굴려 100으로 이동한다.</li>
</ol>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">3</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 2</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">4 9
8 52
6 80
26 42
2 72
51 19
39 11
37 29
81 3
59 5
79 23
53 7
43 33
77 21</pre>



<div style="height:30px" aria-hidden="true" class="wp-block-spacer"></div>



<ol class="wp-block-list">
<li>5를 굴려 6으로 이동하고, 사다리를 이용해 80으로 이동한다.&nbsp;</li>



<li>6을 굴려 86으로</li>



<li>6을 또 굴려 92로</li>



<li>6을 또 굴려 98로 이동하고</li>



<li>2를 굴려 100으로 이동한다.</li>
</ol>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 2</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">5</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출처</h2>



<ul class="wp-block-list">
<li>문제를 번역한 사람:&nbsp;<a href="https://www.acmicpc.net/user/baekjoon" target="_blank" rel="noreferrer noopener">baekjoon</a></li>



<li>빠진 조건을 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/jh05013" target="_blank" rel="noreferrer noopener">jh05013</a></li>
</ul>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">알고리즘 분류</h2>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/7" target="_blank" rel="noreferrer noopener">그래프 이론</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/11" target="_blank" rel="noreferrer noopener">그래프 탐색</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/126" target="_blank" rel="noreferrer noopener">너비 우선 탐색</a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://lycos7560.com/wp-content/uploads/2023/02/image-56.jpg" alt="" class="wp-image-6279" width="567" height="562" srcset="https://lycos7560.com/wp-content/uploads/2023/02/image-56.jpg 690w, https://lycos7560.com/wp-content/uploads/2023/02/image-56-300x297.jpg 300w, https://lycos7560.com/wp-content/uploads/2023/02/image-56-150x150.jpg 150w" sizes="(max-width: 567px) 100vw, 567px" /></figure>



<p><a href="https://kr.123rf.com/photo_27714698_%EB%B1%80%EA%B3%BC-%EC%82%AC%EB%8B%A4%EB%A6%AC-%EB%B3%B4%EB%93%9C-%EA%B2%8C%EC%9E%84.html" target="_blank" rel="noreferrer noopener">https://kr.123rf.com/photo_27714698_%EB%B1%80%EA%B3%BC-%EC%82%AC%EB%8B%A4%EB%A6%AC-%EB%B3%B4%EB%93%9C-%EA%B2%8C%EC%9E%84.html</a></p>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">접근 방법</h1>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size">너비 우선 검색(BFS)을 사용하여 시작 위치에서 최종 위치까지의 최단 경로를 찾으면 됩니다.</p>



<p class="has-medium-font-size">BFS 탐색 시 Queue에는 사각형의 위치와 해당 사각형에 도달하는 데 필요한  주사위 횟수를 저장합니다.</p>



<p class="has-medium-font-size">Queue에서 꺼내고 방문한 각 사각형에 대해 주사위를 굴리고 (1 &#8211; 6)</p>



<p class="has-medium-font-size">주사위 만큼 다음 사각형으로 이동하고 주사위 횟수 + 1 을 해줍니다.</p>



<p class="has-medium-font-size">이 과정에서 사다리와 뱀에 대한 구현과 이동할 위치의 최소 주사위 횟수를 이용하여 경우의 수를 줄입니다.</p>



<p class="has-medium-font-size">최종 위치에 도달할 때까지 위의 과정을 반복합니다.</p>



<p class="has-medium-font-size">BFS 탐색을 어느 정도 알고 있다면 아래의 코드로 풀이를 쉽게 이해 가능하다.</p>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">통과된 코드</h1>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;vector>
#include &lt;queue>

using namespace std;

constexpr int MAX = 101;

constexpr int INF = INT32_MAX;

// 사다리와 뱀을 저장한다.
vector&lt;int> graph[MAX];

// BFS 탐색을 위한 Queue
queue&lt;pair&lt;int,int>> myQueue;

// BFS 탐색에서 주사위의 카운트,
// 방문처리 용도로 사용
int map[MAX];

int N, M, tempO, tempT, result;

int main()
{
	ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화
    //cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다.
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> N >> M;

	// 맵을 전부 INF(INT32_MAX)로 변경한다.
	fill(map, map + MAX, INF);

	// 사다리와 뱀의 경로를 넣어준다.
	for (int i = 0; i &lt; N + M; i++) {
		cin >> tempO >> tempT;
		// tempO -> tempT
		graph[tempO].push_back(tempT);
	}

	// 1번에서 시작, 주사위 횟수 0
	myQueue.push(make_pair(1,0));

	while (!myQueue.empty()) {

		int now = myQueue.front().first;
		int cnt = myQueue.front().second;
		myQueue.pop();

		// 결과에 도착했다면 넘어간다.
		if (now == 100) {
			result = cnt;
			break;	
		} 

		for (int i = 1; i &lt;= 6; i++) {

			int tempNow = now + i;

			// 만약 101과 크거나 같다면 넘어간다.
			// 어파치 100에 도착했을때가 큐에 들어가 있어서 
			// 결과에 영향이 없다.
			if (tempNow >= MAX) continue;

			// 해당 칸에 있는 사다리와 뱀을 확인하고 적용한다.
			for (int j = 0; j &lt; graph[tempNow].size(); j++) {
				tempNow = graph[tempNow][j];
			}

			// 해당 칸에 도착할때의 카운트가 기존의 카운트보다 크다면
			// 넘어간다.
			if (map[tempNow] &lt;= cnt + 1) continue;

			map[tempNow] = cnt + 1;
			myQueue.push(make_pair(tempNow, map[tempNow]));
		}
	}

	cout &lt;&lt; result;

	return 0;
}</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1037" height="145" src="https://lycos7560.com/wp-content/uploads/2023/02/image-55.png" alt="" class="wp-image-6268" srcset="https://lycos7560.com/wp-content/uploads/2023/02/image-55.png 1037w, https://lycos7560.com/wp-content/uploads/2023/02/image-55-300x42.png 300w, https://lycos7560.com/wp-content/uploads/2023/02/image-55-768x107.png 768w" sizes="(max-width: 1037px) 100vw, 1037px" /></figure>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">추가 반례</h1>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 A</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">2 1
7 94
8 94
55 54</pre>



<div style="height:30px" aria-hidden="true" class="wp-block-spacer"></div>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 A</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">2</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 B</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">2 1
2 60
30 98
65 25</pre>



<div style="height:30px" aria-hidden="true" class="wp-block-spacer"></div>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 B</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">4</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 C</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">2 1
2 50
51 100
52 2</pre>



<div style="height:30px" aria-hidden="true" class="wp-block-spacer"></div>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 C</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">2</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5698326622209671" data-ad-slot="4245812909"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-16928%eb%b2%88-%eb%b1%80%ea%b3%bc-%ec%82%ac%eb%8b%a4%eb%a6%ac-%ea%b2%8c%ec%9e%84-c-bfs-baekjoon/6264/">백준 16928번 (뱀과 사다리 게임, C++, BFS) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-16928%eb%b2%88-%eb%b1%80%ea%b3%bc-%ec%82%ac%eb%8b%a4%eb%a6%ac-%ea%b2%8c%ec%9e%84-c-bfs-baekjoon/6264/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 11286번 (절댓값 힙, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11286%eb%b2%88-%ec%a0%88%eb%8c%93%ea%b0%92-%ed%9e%99-c-baekjoon/6163/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11286%eb%b2%88-%ec%a0%88%eb%8c%93%ea%b0%92-%ed%9e%99-c-baekjoon/6163/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Tue, 07 Feb 2023 02:00:12 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[11286]]></category>
		<category><![CDATA[11286번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cin]]></category>
		<category><![CDATA[cin.tie(NULL)]]></category>
		<category><![CDATA[cout]]></category>
		<category><![CDATA[cout.tie(NULL)]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[ios_base::sync_with_stdio(false)]]></category>
		<category><![CDATA[ios_base::sync_with_stdio(false);]]></category>
		<category><![CDATA[pair]]></category>
		<category><![CDATA[priority_queue]]></category>
		<category><![CDATA[PriorityQueue]]></category>
		<category><![CDATA[Queue]]></category>
		<category><![CDATA[std]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[기본]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 11286]]></category>
		<category><![CDATA[백준 11286번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[우선 순위 큐]]></category>
		<category><![CDATA[자료구조]]></category>
		<category><![CDATA[절댓값 힙]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[큐]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=6163</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 11286번 '절댓값 힙' 문제에 대한 글입니다. 자료구조 'priority queue' 를 사용하여 해결하였습니다. (This is an article about the 'absolute value hip' problem in BAEKJOON No. 11286. We solved it using the data structure 'Priority Queue'.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11286%eb%b2%88-%ec%a0%88%eb%8c%93%ea%b0%92-%ed%9e%99-c-baekjoon/6163/">백준 11286번 (절댓값 힙, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">절댓값 힙</h1>



<p class="has-medium-font-size"><a href="https://www.acmicpc.net/problem/11286" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/11286</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure id="problem-info" class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">1 초</td><td class="has-text-align-left" data-align="left">256 MB</td><td class="has-text-align-left" data-align="left">34334</td><td class="has-text-align-left" data-align="left">19057</td><td class="has-text-align-left" data-align="left">15496</td><td class="has-text-align-left" data-align="left">56.292%</td></tr></tbody></table></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">문제</h2>



<p class="has-medium-font-size">절댓값 힙은 다음과 같은 연산을 지원하는 자료구조이다.</p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size">1. 배열에 정수 x (x ≠ 0)를 넣는다.</p>



<p class="has-medium-font-size">2. 배열에서 절댓값이 가장 작은 값을 출력하고, 그 값을 배열에서 제거한다. </p>



<p class="has-medium-font-size">절댓값이 가장 작은 값이 여러개일 때는, 가장 작은 수를 출력하고, 그 값을 배열에서 제거한다.</p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size">프로그램은 처음에 비어있는 배열에서 시작하게 된다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">입력</h2>



<p class="has-medium-font-size">첫째 줄에 연산의 개수 N(1≤N≤100,000)이 주어진다. </p>



<p class="has-medium-font-size">다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. </p>



<p class="has-medium-font-size">만약 x가 0이 아니라면 배열에 x라는 값을 넣는(추가하는) 연산이고, </p>



<p class="has-medium-font-size">x가 0이라면 배열에서 절댓값이 가장 작은 값을 출력하고 그 값을 배열에서 제거하는 경우이다. </p>



<p class="has-medium-font-size">입력되는 정수는 -2<sup>31</sup>보다 크고, 2<sup>31</sup>보다 작다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">18
1
-1
0
0
0
1
1
-1
-1
2
-2
0
0
0
0
0
0
0</pre>



<div style="height:30px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">-1
1
0
-1
-1
1
1
-2
2
0</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출처</h2>



<ul class="wp-block-list">
<li>문제를 만든 사람: <a href="https://www.acmicpc.net/user/baekjoon" target="_blank" rel="noreferrer noopener">baekjoon</a></li>



<li>잘못된 조건을 찾은 사람: <a href="https://www.acmicpc.net/user/dkim" target="_blank" rel="noreferrer noopener">dkim</a></li>



<li>데이터를 추가한 사람: <a href="https://www.acmicpc.net/user/nova9128" target="_blank" rel="noreferrer noopener">nova9128</a></li>



<li>문제의 오타를 찾은 사람: <a href="https://www.acmicpc.net/user/sgchoi5" target="_blank" rel="noreferrer noopener">sgchoi5</a>, <a href="https://www.acmicpc.net/user/youngminz" target="_blank" rel="noreferrer noopener">youngminz</a>, <a href="https://www.acmicpc.net/user/zerozero" target="_blank" rel="noreferrer noopener">zerozero</a></li>
</ul>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">비슷한 문제</h2>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/1927" target="_blank" rel="noreferrer noopener">1927번. 최소 힙</a></li>



<li><a href="https://www.acmicpc.net/problem/11279" target="_blank" rel="noreferrer noopener">11279번. 최대 힙</a></li>
</ul>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">알고리즘 분류</h2>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/175">자</a><a href="https://www.acmicpc.net/problem/tag/175" target="_blank" rel="noreferrer noopener">료 구조</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/59" target="_blank" rel="noreferrer noopener">우선순위 큐</a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<p class="has-medium-font-size">priority_queue의 성질을 이용하여 해결하였다.</p>



<p class="has-medium-font-size">아래의 자료구조만 이해한다면 쉽게 해결할 수 있는 문제</p>



<p class="has-medium-font-size"><a href="https://chanhuiseok.github.io/posts/algo-54/" target="_blank" rel="noreferrer noopener">https://chanhuiseok.github.io/posts/algo-54/</a>  &lt;- 해당 사이트에서 정독하면 쉽게 이해가능합니다.</p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1174" height="86" src="https://lycos7560.com/wp-content/uploads/2023/02/image-50.png" alt="" class="wp-image-6169" srcset="https://lycos7560.com/wp-content/uploads/2023/02/image-50.png 1174w, https://lycos7560.com/wp-content/uploads/2023/02/image-50-300x22.png 300w, https://lycos7560.com/wp-content/uploads/2023/02/image-50-768x56.png 768w" sizes="(max-width: 1174px) 100vw, 1174px" /></figure>



<figure class="wp-block-image size-full"><img decoding="async" width="781" height="311" src="https://lycos7560.com/wp-content/uploads/2023/02/image-52.png" alt="" class="wp-image-6174" srcset="https://lycos7560.com/wp-content/uploads/2023/02/image-52.png 781w, https://lycos7560.com/wp-content/uploads/2023/02/image-52-300x119.png 300w, https://lycos7560.com/wp-content/uploads/2023/02/image-52-768x306.png 768w" sizes="(max-width: 781px) 100vw, 781px" /></figure>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">통과된 코드</h2>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;cmath>
#include &lt;queue>

using namespace std;

priority_queue&lt;pair&lt;int, int>, vector&lt;pair&lt;int, int>>, greater&lt;pair&lt;int, int>>> myPQ;

pair&lt;int, int> tempP;

int N, tempO, tempT;

int main()
{

    ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화
	// cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다.
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> N;

	for (int i = 0; i &lt; N; i++) {

		cin >> tempO;

		if (tempO == 0) {
			if (myPQ.empty()) cout &lt;&lt; 0 &lt;&lt; "\n";
			else {
				tempP = myPQ.top();
				cout &lt;&lt; tempP.first * tempP.second &lt;&lt; "\n";
				myPQ.pop();
			}
			continue;
		}

		if (tempO > 0) tempT = 1; // 나중에 출력을 쉽게 하기 위해서 1로 만들어준다.
		else tempT = -1; // 나중에 출력을 쉽게 하기 위해서 -1로 만들어준다.

		myPQ.push(make_pair(abs(tempO), tempT));
	}

	return 0;
}</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://lycos7560.com/wp-content/uploads/2023/02/image-49.png" alt="" class="wp-image-6166" width="1188" height="118" srcset="https://lycos7560.com/wp-content/uploads/2023/02/image-49.png 1039w, https://lycos7560.com/wp-content/uploads/2023/02/image-49-300x30.png 300w, https://lycos7560.com/wp-content/uploads/2023/02/image-49-768x76.png 768w" sizes="(max-width: 1188px) 100vw, 1188px" /></figure>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5698326622209671" data-ad-slot="4245812909"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11286%eb%b2%88-%ec%a0%88%eb%8c%93%ea%b0%92-%ed%9e%99-c-baekjoon/6163/">백준 11286번 (절댓값 힙, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11286%eb%b2%88-%ec%a0%88%eb%8c%93%ea%b0%92-%ed%9e%99-c-baekjoon/6163/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
