[C++][백준 9081] 단어 맞추기

PublicMinsu·2025년 9월 8일

문제

https://www.acmicpc.net/problem/9081

접근 방법

순열 문제입니다.
다음 순열을 구해주면 됩니다.

코드

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

int T;
string word;

int main()
{
    ios::sync_with_stdio(0), cin.tie(0);

    cin >> T;

    while (T--)
    {
        cin >> word;

        if (!next_permutation(word.begin(), word.end()))
        {
            prev_permutation(word.begin(), word.end());
        }

        cout << word << "\n";
    }

    return 0;
}

풀이

next_permutation을 활용하여서 다음 순열을 확인합니다.
만약 false라면 마지막 순열이라는 의미이므로 prev_permutation를 활용해서 되돌려 놓습니다.

profile
연락 : publicminsu@naver.com

0개의 댓글