1251. 단어 나누기

phoenixKim·2022년 9월 30일
0

백준 알고리즘

목록 보기
137/174
  • 틀림.

맨처음 생각

: a / r / rested 로 확인
a / rr / ested 로 확인하는 방식으로 접근해야 할듯 함?

  • for문으로 처리하면됨.
    쉽게 생각하자.

첫번째 풀이 전략

-> 시간 복잡도 때문에 틀리는 듯함.

  • 단어 하나씩 확인을 하면서 가장 작은 소문자 발견할 경우, substr

  • 그 값을 reverse 하는 방식으로 진행함.
    -> 14퍼센트에서 틀림.

  • 반례가 있음.
    : zzzzaaaa
    -> 이거 때문에 c> s[i] 에서 c >= s[i] 로 변경함.

  • 코드

#include <iostream>
#include <vector>
#include <string>
using namespace std;
#include <algorithm>

//21:43 ~ 22:13

void dfs(string word)
{



}

int main()
{
	// 두 부분으로 단어를 쪼개야 함. 

	// 어떤 방식으로 쪼갤지는 주어지지 않음. 

	// arrested 

	// a / r / rested

	// a / rr / ested

	// a / rre / sted 

	// 이런 방식임.
	// 내 생각에는 굳이 이렇게 해야 할 필요가 있나 싶음... 
	string s;
	cin >> s;

	string res = "";
	
	int index = 0;
	int shortIndex = index;

	for (int h = 0; h < 2; ++h)
	{
		int cnt = 0;
		char c = 'z';
		for (int i = index; i < s.length(); ++i)
		{
			if (c >= s[i])
			{
				c = s[i];
				shortIndex = i;
			}
		}
		string temp = s.substr(index, shortIndex + 1 - index);
		index = shortIndex + 1;
		reverse(temp.begin(), temp.end());
		res += temp;
	}
	
	string lastWord = s.substr(index);
	reverse(lastWord.begin(), lastWord.end());
	res += lastWord;

	cout << res;
}
profile
🔥🔥🔥

0개의 댓글

관련 채용 정보