<?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>백준 2661번 Archives - 어제와 내일의 나 그 사이의 이야기</title>
	<atom:link href="https://lycos7560.com/tag/%EB%B0%B1%EC%A4%80-2661%EB%B2%88/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>생각의 흐름을 타고 다니며 만드는 블로그</description>
	<lastBuildDate>Thu, 23 Feb 2023 00:16:25 +0000</lastBuildDate>
	<language>ko-KR</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.2</generator>

<image>
	<url>https://lycos7560.com/wp-content/uploads/2022/11/cropped-cropped-cropped-log-1-150x150-1-80x80.png</url>
	<title>백준 2661번 Archives - 어제와 내일의 나 그 사이의 이야기</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>백준 2661번 (좋은수열, C++, Backtracking) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon-2661%eb%b2%88-%ec%a2%8b%ec%9d%80%ec%88%98%ec%97%b4-c-backtracking/532/</link>
					<comments>https://lycos7560.com/cpp/baekjoon-2661%eb%b2%88-%ec%a2%8b%ec%9d%80%ec%88%98%ec%97%b4-c-backtracking/532/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Fri, 28 Oct 2022 11:55:17 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[2661]]></category>
		<category><![CDATA[Backtracking]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[dfs]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 2661]]></category>
		<category><![CDATA[백준 2661번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[좋은수열]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.synology.me/?p=532</guid>

					<description><![CDATA[<p>좋은수열 https://www.acmicpc.net/problem/2661 시간 제한 메모리 제한 제출 정답 맞힌 사람 정답 비율 1 초 128 MB 11169 5455 4188 50.144% 문제 숫자 1, 2, 3으로만 이루어지는 수열이 있다. 임의의 길이의 인접한 두 개의 부분 수열이 동일한 것이 있으면, 그 수열을 나쁜 수열이라고 부른다. 그렇지 않은 수열은 좋은 수열이다. 다음은 나쁜 수열의 예이다. 다음은 좋은 수열의 [&#8230;]</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon-2661%eb%b2%88-%ec%a2%8b%ec%9d%80%ec%88%98%ec%97%b4-c-backtracking/532/">백준 2661번 (좋은수열, C++, Backtracking) [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>



<h1 class="wp-block-heading"><strong>좋은수열</strong></h1>



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



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



<figure id="problem-info" class="wp-block-table is-style-stripes"><table class="has-ast-global-color-8-color has-ast-global-color-6-background-color has-text-color has-background"><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">11169</td><td class="has-text-align-left" data-align="left">5455</td><td class="has-text-align-left" data-align="left">4188</td><td class="has-text-align-left" data-align="left">50.144%</td></tr></tbody></table></figure>



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



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



<p class="has-medium-font-size wp-block-paragraph">숫자 1, 2, 3으로만 이루어지는 수열이 있다. <br>임의의 길이의 인접한 두 개의 부분 수열이 동일한 것이 있으면, 그 수열을 나쁜 수열이라고 부른다. <br>그렇지 않은 수열은 좋은 수열이다.</p>



<p class="has-medium-font-size wp-block-paragraph">다음은 나쁜 수열의 예이다.</p>



<ul class="wp-block-list">
<li>33</li>



<li>32121323</li>



<li>123123213</li>
</ul>



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



<p class="has-medium-font-size wp-block-paragraph">다음은 좋은 수열의 예이다.</p>



<ul class="wp-block-list">
<li>2</li>



<li>32</li>



<li>32123</li>



<li>1232123</li>
</ul>



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



<p class="has-medium-font-size wp-block-paragraph">길이가 N인 좋은 수열 들을 N자리의 정수로 보아 그 중 가장 작은 수를 나타내는 <br>수열을 구하는 프로그램을 작성하라. <br><br>예를 들면, 1213121과 2123212는 모두 좋은 수열이지만 <br>그 중에서 작은 수를 나타내는 수열은 1213121이다.</p>



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



<p class="has-normal-font-size wp-block-paragraph">입력은 숫자 N하나로 이루어진다. N은 1 이상 80 이하이다.</p>



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



<p class="has-normal-font-size wp-block-paragraph">첫 번째 줄에 1, 2, 3으로만 이루어져 있는 길이가 N인 좋은 수열 들 중에서 가장 작은 수를 <br>나타내는 수열만 출력한다. <br>수열을 이루는 1, 2, 3 들 사이에는 빈칸을 두지 않는다.</p>



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



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



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



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



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



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



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



<h2 class="has-medium-font-size wp-block-heading">출처</h2>



<p class="has-normal-font-size wp-block-paragraph"><a href="https://www.acmicpc.net/category/2" target="_blank" rel="noreferrer noopener">Olympiad</a> > <a href="https://www.acmicpc.net/category/55" target="_blank" rel="noreferrer noopener">한국정보올림피아드</a> > <a href="https://www.acmicpc.net/category/81" target="_blank" rel="noreferrer noopener">KOI 1997</a> > <a href="https://www.acmicpc.net/category/detail/401" target="_blank" rel="noreferrer noopener">중등부</a> 1번</p>



<ul class="wp-block-list">
<li>문제의 오타를 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/busyhuman" target="_blank" rel="noreferrer noopener">busyhuman</a>,&nbsp;<a href="https://www.acmicpc.net/user/mwy3055" target="_blank" rel="noreferrer noopener">mwy3055</a></li>
</ul>



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



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



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/5" target="_blank" rel="noreferrer noopener">백트래킹</a></li>
</ul>



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



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



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



<p class="wp-block-paragraph">1차 시도 <br>BFS로 모든 경우를 다 만들어 놓고 시작하려니 N이 80 이 되는 순간 프로그램이 나락을 간다.</p>



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



<h1 class="wp-block-heading"><strong>실패코드</strong></h1>



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



<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;queue>
#include &lt;cmath> 

using namespace std;

queue&lt;pair&lt;int,int>> myQueue;

int N;

void BFS(int N)
{
	myQueue.push({0, N});

	while (!myQueue.empty())
	{
		int x = myQueue.front().first;
		int n = myQueue.front().second;
		myQueue.pop();

		cout &lt;&lt; n &lt;&lt; " \n";
		if (n == 0)
		{
			//cout &lt;&lt; x &lt;&lt; " \n";
			continue;
		}
		for (int i = 1; i &lt;= 3; i++)
		{
			if (i == 1)
			{
				myQueue.push({ x + 1 * pow(10, n - 1), n-1 });
			}
			else if (i == 2)
			{
				myQueue.push({ x + 2 * pow(10, n - 1), n - 1 });
			}
			else if (i == 3)
			{
				myQueue.push({ x + 3 * pow(10, n - 1), n - 1 });
			}
		}
	}

}


int main()
{
	cin >> N;
	BFS(N);
	return 0;
}</pre>



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



<p class="has-medium-font-size wp-block-paragraph">계산 중 깊이를 측정해보니 이건 노답 이라는 것을 깨달았다.</p>



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



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="931" height="545" src="https://lycos7560.com/wp-content/uploads/image-72.png" alt="" class="wp-image-2293" srcset="https://lycos7560.com/wp-content/uploads/image-72.png 931w, https://lycos7560.com/wp-content/uploads/image-72-300x176.png 300w, https://lycos7560.com/wp-content/uploads/image-72-768x450.png 768w" sizes="(max-width: 931px) 100vw, 931px" /></figure>



<div style="height:50px" 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>



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



<p class="has-medium-font-size wp-block-paragraph">DFS로 변경<br> 목표는 가장 낮은 숫자를 찾는 것 =&gt; 1부터 시작해서 탐색 </p>



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



<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;queue>
#include &lt;cmath> 

using namespace std;

int N;

void DFS(int n, int value)
{
	if (n == 0)
	{
		cout &lt;&lt; value &lt;&lt; "\n";
		return;
	}

	for (int i = 1; i &lt;= 3; i++)
	{
		if (i == 1)
		{
			DFS(n - 1, value + 1 * pow(10, n - 1));
		}
		else if (i == 2)
		{
			DFS(n - 1, value + 2 * pow(10, n - 1));
		}
		else if (i == 3)
		{
			DFS(n -1, value + 3 * pow(10, n - 1));
		}
	}
}

int main()
{
	cin >> N;
	DFS(N, 0);
	cout &lt;&lt; "end";
	return 0;
}</pre>



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



<p class="has-medium-font-size wp-block-paragraph">N은 80까지 가능<br>80 자리…<br>string 으로 변환…. </p>



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



<figure class="wp-block-image size-full"><img decoding="async" width="939" height="752" src="https://lycos7560.com/wp-content/uploads/image-73.png" alt="" class="wp-image-2294" srcset="https://lycos7560.com/wp-content/uploads/image-73.png 939w, https://lycos7560.com/wp-content/uploads/image-73-300x240.png 300w, https://lycos7560.com/wp-content/uploads/image-73-768x615.png 768w" sizes="(max-width: 939px) 100vw, 939px" /></figure>



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



<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 N;

void DFS(int n, string value)
{
	if (n == 0)
	{
		cout &lt;&lt; value &lt;&lt; "\n";
		return;
	}

	
	for (int i = 1; i &lt;= 3; i++)
	{
		if (i == 1)
		{
			DFS(n - 1, value + '1');
		}
		else if (i == 2)
		{
			DFS(n - 1, value + '2');
		}
		else if (i == 3)
		{
			DFS(n - 1, value + '3');
		}
	}
}

int main()
{
	cin >> N;
	DFS(N, "");
	cout &lt;&lt; "end";
	return 0;
}</pre>



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



<figure class="wp-block-image size-full"><img decoding="async" width="871" height="453" src="https://lycos7560.com/wp-content/uploads/image-74.png" alt="" class="wp-image-2295" srcset="https://lycos7560.com/wp-content/uploads/image-74.png 871w, https://lycos7560.com/wp-content/uploads/image-74-300x156.png 300w, https://lycos7560.com/wp-content/uploads/image-74-768x399.png 768w" sizes="(max-width: 871px) 100vw, 871px" /></figure>



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



<p class="has-medium-font-size wp-block-paragraph">조건에 부합하는 가장 처음 값이 정답</p>



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



<h2 class="wp-block-heading"><strong> 성공 코드(DFS) </strong></h2>



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



<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 N;
string number;

void DFS(char ch, int cnt)
{
	// 제일 먼저 조건에 부합하는 숫자가 답
	if (cnt - 1 == N) { 
		cout &lt;&lt; number; 
		exit(0);
	}
	
	number += ch;
	
	for (int i = 1; i &lt;= cnt/2; i++)
	{
		string a = number.substr(cnt - i, i);
		string b = number.substr(cnt - i * 2, i);

		if (a == b){ 
			// 나쁜 수열이면 지우고 리턴
			number.erase(cnt - 1);
			return;
		}
	}

	for (int i = 1; i &lt;= 3; i++)
	{
		DFS(i + '0', cnt + 1);
	}

	// cnt - 1 자리가 성립하지 않을 경우
	number.erase(cnt - 1);
}

int main()
{
	cin >> N;
	for (int i = 1; i &lt;= 3; i++)
	{
		// '0' 에서 i 만큼 더하면 i
		DFS(i + '0', 1);
	}

	return 0;
}</pre>



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



<figure class="wp-block-image size-full"><img decoding="async" width="794" height="81" src="https://lycos7560.com/wp-content/uploads/image-75.png" alt="" class="wp-image-2296" srcset="https://lycos7560.com/wp-content/uploads/image-75.png 794w, https://lycos7560.com/wp-content/uploads/image-75-300x31.png 300w, https://lycos7560.com/wp-content/uploads/image-75-768x78.png 768w" sizes="(max-width: 794px) 100vw, 794px" /></figure>



<div style="height:89px" 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; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-5698326622209671"
     data-ad-slot="5078714126"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon-2661%eb%b2%88-%ec%a2%8b%ec%9d%80%ec%88%98%ec%97%b4-c-backtracking/532/">백준 2661번 (좋은수열, C++, Backtracking) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon-2661%eb%b2%88-%ec%a2%8b%ec%9d%80%ec%88%98%ec%97%b4-c-backtracking/532/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
