기술 블로그

1213번 팰린드롬 만들기 본문

알고리즘 문제/BOJ

1213번 팰린드롬 만들기

parkit 2018. 12. 29. 02:34
728x90
반응형

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


홀수개인 알파벳의 개수가 2개 이상이면 팰린드롬을 만들 수 없다.


그 외에는 나온 개수만큼(홀수개 일 때는 하나 감소 시켜서) 앞, 뒤로 넣어주고,


홀수개인 알파벳을 가운데에 넣어준다.



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
75
76
#include <iostream>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <math.h>
#include <algorithm>
#include <map>
 
using namespace std;
 
int alpha[91= { 0, };
 
int main(void)
{
    string s;
    
    vector<char> v;
 
    cin >> s;
 
    for (int i = 0; i < s.length(); i++)
        ++alpha[s[i]];
 
    int odd = 0;
 
    for (int i = 'A'; i <= 'Z'; i++)
    {
        if (alpha[i] % 2 != 0++odd;
    }
 
    if (odd > 1)
    {
        printf("I'm Sorry Hansoo\n");
        return 0;
    }
 
    for(int i='Z'; i >= 'A'; i--)
    {
        if (alpha[i] == 0continue;
 
        int chance = alpha[i];
 
        if (alpha[i] % 2 != 0--chance;
        
        int cnt = 1;
 
        for (int j = 0; j < chance; j++)
        {    
            if (cnt == 1)
            {
                v.insert(v.begin(), i);
 
                cnt = -1;
            }
            else if (cnt == -1)
            {
                v.push_back(i);
 
                cnt = 1;
            }
 
            --alpha[i];
        }
    }
 
    for (int i = 'A'; i <= 'Z'; i++)
        if (alpha[i] == 1) v.insert(v.begin() + v.size() / 2, i);
 
    for (auto i : v) printf("%c", i);
    printf("\n");
 
    return 0;
}
cs


728x90
반응형

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

10448번 유레카 이론  (0) 2018.12.29
7568번 덩치  (0) 2018.12.29
1038번 감소하는 수  (0) 2018.12.28
1107번 리모컨  (0) 2018.12.26
11657번 타임머신  (0) 2018.12.26