기술 블로그

16923번 다음 다양한 단어 본문

알고리즘 문제/BOJ

16923번 다음 다양한 단어

parkit 2019. 3. 16. 00:11
728x90
반응형

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



못 접근 했다.


azbcdefghijklmnopqrstuvwxy을 

입력하면, b가 될 줄 알았는데(z가 기준인 줄로 알았음)

생각해보니 그게 아니었다.


끝에서 차례로 검사를 해야한다.




테스트 케이스

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
입력
출력
 
abcdefghijklmnopqrstuvwzyx
abcdefghijklmnopqrstuvx
 
azbcdefghijklmnopqrstuvwxy
azbcdefghijklmnopqrstuvwy
 
zabcdefghijklmnopqrstuvwxy
zabcdefghijklmnopqrstuvwy
 
zndica
zndicab
 
bzacdefghijklmnopqrstuvwxy
bzacdefghijklmnopqrstuvwy
 
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxz
 
zyxwvutsrqponmlkjihgfedcba
-1
 
z
za
 
cz
cza
 
a
ab
 
xyz
xyza
 
abcdefghijklznopqrstuvwxym
abcdefghijklznopqrstuvwy
cs









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
#include <iostream>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <math.h>
#include <algorithm>
#include <map>
#include <set>
 
#pragma warning(disable:4996)  
#pragma comment(linker, "/STACK:336777216")
 
using namespace std;
 
string s;
 
bool used[140= { false, };
 
int main(void)
{
    cin >> s;
 
    for (int i = 0; i < s.length(); i++) used[s.at(i)] = true// 사용한 문자 체크
 
    if (s.length() < 26// 길이가 26 미만일 때
    {
        for (int i = 'a'; i <= 'z'; i++// 낮은 순서부터
        {
            if (!used[i]) // 사용하지 않은 것을 찾은 후 
            {
                cout << s;
                printf("%c\n", i); // 바로 출력하면 된다
                break;
            }
        }
    }
    else // s.length() == 26
    {
        memset(used, falsesizeof(used));
 
        for (int i = 25; i >= 1; i--)
        {
            if (s.at(i - 1< s.at(i))
            {
                for (int j = 0; j <= i - 1; j++)
                {
                    if (!used[s.at(j)]) used[s.at(j)] = true;
                }
 
                string g;
 
                g = s.substr(0, i - 1);
 
                cout << g;
 
                for (int j = s.at(i - 1); j <= 'z'; j++)
                {
                    if (!used[j])
                    {
                        printf("%c\n", j);
                        return 0;
                    }
                }
            }
        }
 
        printf("-1\n");
    }
 
    return 0;
}
cs













728x90
반응형

'알고리즘 문제 > BOJ' 카테고리의 다른 글

16922번 로마 숫자 만들기  (0) 2019.03.18
15651번 N과 M (3)  (0) 2019.03.18
16918번 봄버맨  (0) 2019.03.15
16917번 양념 반 후라이드 반  (0) 2019.03.14
17069번 파이프 옮기기 2  (0) 2019.03.13