본문 바로가기

백준

16923 다음 다양한 단어

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

 

16923번: 다음 다양한 단어

다양한 단어란 모두 다른 알파벳 소문자로만 이루어진 단어를 의미한다. 예를 들어, "codeplus", "coding", "algorithm"은 다양한 단어, "baekjoon", "startlink"는 다양한 단어가 아니다. 다양한 단어 S가 주어졌

www.acmicpc.net

 

저번에 못 풀었던 ucpc 연습문제 미뤄두다가 오늘 드디어 푼다. 보자마자 개꿀 실버문제~ 라고 생각했는데 아앗 아니었다. 

난 그냥 안 나왔던 알파벳 중 하나 끝에 붙이면 된다고 생각했는데 마지막 예제 보고 아니라는 걸 깨달았다. 

 

으으음...

 

abc인 경우는 앞의 알파벳이 다 찼으니 새로운 d를 붙여야 하고..

codeplus인 경우는 앞의 a, b를 안 썼으니 a를 붙였고

zyxwvutsrqponmlkjihgfedcba 이거 처음에 모든 알파벳을 써서 -1인 줄 알았는데 마지막 다양한 단어라 -1이었다. 그냥 이 하나의 경우만 예외처리 해주면 될 것 같았다. 

 

뭔가 모든 알파벳을 다 쓴 경우 / 다 쓰지 않은 경우 이렇게 나누면 될 것 같다.

 

abcdefghijklmnopqrstuvwzyx

 

이건 일단 전부 다 쓴 경우인데... 아... 어떻게 하는지 모르겠다... 고민하다가 팀원이 푼 소스를 참고했다.

next_permutation 얘가 있구나!!! 맞아 다음 순열 찾는 이게 있었어. 나도 이걸 쓰면 될 것 같아서 아래처럼 짰는데 문제가 있다..

 

abcdefghijklmnopqrstuvwzyx 이걸 넣으면 abcdefghijklmnopqrstuvxwyz 이렇게 나온다.. 팀원의 코드는 잘 나오는데 왜 내것만 문제지?? 

 

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cmath>
#include <cstring>
#include <bitset>

#define xx first
#define yy second
#define all(x) (x).begin(), (x).end()

using namespace std;
using i64 = long long int;
using ii = pair<int, int>;
using ii64 = pair<i64, i64>;

char alpha[26];

int main() {
    string s;
    cin >> s;
    
    for (int i = 0; i < s.size(); i++) {
        alpha[s[i] - 'a']++;
    }
    
    if (s.size() == 26) {
        if (next_permutation(s.begin(), s.end()))
            cout << s;
        else
            cout << -1;
        
        return 0;
    }

    cout << s;
    for (int i = 0; i < 26; i++) {
        if (alpha[i] >= 1)
            continue;
        cout << (char)(i + 'a');
        break;
    }
}

 

아...! 미적다..! 이걸 배열로 받은 다음 널 포함해서 next_permutation 돌렸다. 와.. 신기하다. 이게 이런 배열에도 적용되는구나 싶고... 와... 

 

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cmath>
#include <cstring>
#include <bitset>
#include <stdio.h>

#define xx first
#define yy second
#define all(x) (x).begin(), (x).end()

using namespace std;
using i64 = long long int;
using ii = pair<int, int>;
using ii64 = pair<i64, i64>;

char alpha[26];

int main() {
    char s[30];
    scanf("%s", s);
    
    int len = strlen(s);
    for (int i = 0; i < len; i++) {
        alpha[s[i] - 'a']++;
    }
    
    if (len == 26) {
        if (next_permutation(s, s + 27))
            printf("%s", s);
        else
            cout << -1;
        
        return 0;
    }

    printf("%s", s);
    for (int i = 0; i < 26; i++) {
        if (alpha[i] >= 1)
            continue;
        cout << (char)(i + 'a');
        break;
    }
}

 

 

 

 

'백준' 카테고리의 다른 글

18436 수열과 쿼리 37  (0) 2021.07.24
2873 롤러코스터  (0) 2021.07.20
20149 선분 교차 3  (0) 2021.07.18
2090 조화평균  (0) 2021.07.17
12850 본대 산책2  (0) 2021.07.17