#include <string>
#include <vector>
using namespace std;
 
vector<vector<int>> answer;
 
void hanoi(int num, int from, int by, int to)
{
    // 1번판(마지막 원판) -> 3번판
    if(num == 1)
    {
        vector<int> temp;
        temp.push_back(from);
        temp.push_back(to);
        answer.push_back(temp);
        
        return;
    }
    
    // 1번판 -> 2번판
    hanoi(num-1, from, to, by);
    
    // 1번판 -> 3번판
    vector<int> temp;
    temp.push_back(from);
    temp.push_back(to);
    answer.push_back(temp);
    
    // 2번판 -> 3번판
    hanoi(num-1, by, from, to);
}
 
vector<vector<int>> solution(int n) 
{
    hanoi(n, 123);
    
    return answer;
}
cs
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
 
vector<string> solution(vector<string> record) 
{
    vector<string> answer;
    vector<pair<stringstring>> temp;
    map<stringstring> reserve;
    
    for(int i = 0; i < record.size(); i++)
    {
        if(record[i][0== 'E')
        {
            int j;
            string id;
            for(j = 6; record[i][j] != ' '; j++)
            {
                id += record[i][j];
            }
            
            string nickname;
            for(int k = j+1; k < record[i].size(); k++)
            {
                nickname += record[i][k];
            }
            
            reserve[id] = nickname;
            temp.push_back({id, "E"});
        }
        else if(record[i][0== 'L')
        {
            int j;
            string id;
            for(j = 6; j < record[i].size(); j++)
            {
                id += record[i][j];
            }
            
            temp.push_back({id, "L"});
        }
        else if(record[i][0== 'C')
        {
            int j;
            string id;
            for(j = 7; record[i][j] != ' '; j++)
            {
                id += record[i][j];
            }
            
            string nickname;
            for(int k = j+1; k < record[i].size(); k++)
            {
                nickname += record[i][k];
            }
            
            reserve[id] = nickname;
        }
    }
    
    for(int i = 0; i < temp.size(); i++)
    {
        string result;
        result += reserve[temp[i].first];
        
        if(temp[i].second == "E")
        {
            result += "님이 들어왔습니다.";   
        }
        else if(temp[i].second == "L")
        {
            result += "님이 나갔습니다.";   
        }
        
        answer.push_back(result);
    }
    
    return answer;
}
cs
#include <vector>
using namespace std;
 
int solution(vector<int> nums)
{
    int answer = 0;
    int visited[200010= {0};
    
    for(int i = 0; i < nums.size(); i++)
    {
        if(answer == nums.size()/2)
        {
            break;
        }
        
        if(visited[nums[i]] == 0)
        {
            visited[nums[i]] = 1;
            answer++;
        }
    }
    
    return answer;
}
cs
#include <stdio.h>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
using namespace std;
 
string s;
int cnt0, cnt1;
 
int main(void)
{
//    freopen("B1439_input.txt", "r", stdin);
    
    cin >> s;
    
    for(int i = 0; i < s.size(); i++)
    {
        if(s[i] == '1' && s[i] != s[i+1])
        {
            cnt1++;
        }
        else if(s[i] == '0' && s[i] != s[i+1])
        {
            cnt0++;
        }
    }
    
    cout << min(cnt1, cnt0);
    
    return 0;
}
cs
#include <stdio.h>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
using namespace std;
 
string board;
string ans;
 
int main(void)
{
//    freopen("B1343_input.txt", "r", stdin);
    
    cin >> board;
    
    for(int i = 0; i < board.size();)
    {
        if(board[i] == '.')
        {
            ans += '.';
            i += 1;
            
            continue;
        }
        
        int cnt = 0;
        
        for(int j = i; j < board.size() && board[j] == 'X'; j++)
        {
            cnt++;
        }
        i += cnt;
        
        if(cnt % 2 == 1)
        {
            cout << "-1";
            return 0;
        }
        
        while(1)
        {
            if(cnt >= 4)
            {
                ans += "AAAA";
                cnt -= 4;
            }
            else if(cnt == 2)
            {
                ans += "BB";
                cnt -= 2;
            }
            else
            {
                break;
            }
        }
    }
    
    cout << ans;
    
    return 0;
}
cs

'Baekjoon > Greedy' 카테고리의 다른 글

[백준 11047] 동전 0 (Greedy) (C/C++)  (0) 2020.03.07
#include <stdio.h>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
using namespace std;
 
int N;
long long num[2010];
int Left, Right;
int ans;
 
int main(void)
{
//    freopen("B1253_input.txt", "r", stdin);
    
    cin >> N;
    
    for(int i = 1; i <= N; i++)
    {
        cin >> num[i];
    }
    
    sort(num+1, num+N+1);
    
    for(int i = 1; i <= N; i++)
    {
        long long find = num[i];
        Left = 1;
        Right = N;
        
        // 투포인터 알고리즘 활용 
        while(Left < Right)
        {    
            if(num[Left] + num[Right] == find)
            {
                // find는 서로 다른 두 수의 합이여야됨을 체크 
                if(Left != i && Right != i)
                {
                    ans++;
                    break;    
                }
                else if(Left == i)
                {
                    Left++;
                }
                else if(Right == i)
                {
                    Right--;
                }
            }
            else if(num[Left] + num[Right] < find)
            {
                Left++;
            }
            else
            {
                Right--;
            } 
        }
    }
    
    cout << ans;
    
    return 0;
}
cs

#include <stdio.h>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
using namespace std;
 
int N, S;
int num[21];
int choice[21];
int ans;
 
int check(int cnt)
{
    int sum = choice[0];
    
    for(int i = 1; i < cnt; i++)
    {
        sum += choice[i];
    }
    
    if(sum == S)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
 
 
void combination(int idx, int cnt)
{
    if(cnt > N)
    {
        return;
    }
    else if(cnt != 0)
    {
        if(check(cnt) == 1)
        {
            ans++;
        }
    }
    
    for(int i = idx; i < N; i++)
    {
        choice[cnt] = num[i];
        combination(i+1, cnt+1);
    }
}
 
int main(void)
{
//    freopen("B1182_input.txt", "r", stdin);
    
    cin >> N >> S;
    
    for(int i = 0; i < N; i++)
    {
        cin >> num[i];
    }
    
    combination(00);
    
    cout << ans;
    
    return 0;
}
cs

'Baekjoon > etc' 카테고리의 다른 글

[백준 1439] 뒤집기 (C/C++)  (0) 2019.12.30
[백준 1253] 좋다 (C/C++)  (0) 2019.12.30
[백준 1145] 적어도 대부분의 배수 (C/C++)  (0) 2019.12.18
[백준 1065] 한수 (C/C++)  (0) 2019.12.18
[백준 1024] 수열의 합 (C/C++)  (0) 2019.12.17

+ Recent posts