반응형

문제 출처 :


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



알고리즘 분석 :


문제 해결에 필요한 사항

1. permutation


문자열을 받고 next_permutation함수를 이용하여 순열을 돌려주면 된다.


이때 현재와 이전 문자열이 같은지 다른지 체크하고 같으면 chk = false를 해주면 된다.


이때 chk는 bool이기에 ans += chk를 하면 모든 개수를 파악 할 수 있다.






소스 코드 : 


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
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
 
using namespace std;
 
int main()
{
    string str;
    cin >> str;
 
    string tmp = str;
 
    int ans = 0;
 
    bool chk = true;
    for (int i = 1; i < str.size(); i++)
        if (str[i] == str[i - 1])
            chk = false;
 
    ans += chk;
 
    next_permutation(str.begin(), str.end());
    while (str != tmp)
    {
        bool chk = true;
        for (int i = 1; i < str.size(); i++)
            if (str[i] == str[i - 1])
                chk = false;
 
        ans += chk;
        next_permutation(str.begin(), str.end());
    }
    cout << ans;
 
    return 0;
}
 
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
Crocus

반응형

'Applied > 알고리즘 문제풀이' 카테고리의 다른 글

[3067번] Coins  (0) 2018.03.21
[9494번] 데구르르  (0) 2018.03.19
[CSAcademy] Prefix Free Subset  (4) 2018.03.11
[9202번] Boggle  (0) 2018.03.10
[5446번] 용량 부족  (0) 2018.03.08