#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
 
int solution(string s)
{
    int dp[2510][2510= {0};
    int Max;
    
    // 길이 1
    for(int i = 0; i < s.size(); i++)
    {
        dp[i][i] = 1;
        Max = 1;
    }
    
    // 길이 2
    for(int i = 0; i < s.size()-1; i++)
    {
        if(s[i] == s[i+1])
        {
            dp[i][i+1= 1;
            Max = 2;
        }
    }
    
    // 길이 3 이상
    for(int k = 2; k < s.size(); k++)
    {
        for(int i = 0; i <= s.size()-1-k; i++)
        {
            int j = i + k;
            
            if(s[i] == s[j] && dp[i+1][j-1== 1)
            {
                dp[i][j] = 1;
                
                if(Max < k+1)
                {
                    Max = k+1;
                }
            }
        }
    }
 
    return Max;
}
cs
#include <string>
#include <vector>
#include <iostream>
using namespace std;
 
vector<int> solution(int n) 
{
    vector<int> answer;
    string paper = "0";
    
    for(int i = 1; i < n; i++)
    {
        string temp_paper = paper;
 
        // 원래 종이의 가운데 부분만 다른 숫자로 변경
        if(temp_paper[temp_paper.size()/2== '0')
        {
            temp_paper[temp_paper.size()/2= '1';
        }
        else
        {
            temp_paper[temp_paper.size()/2= '0';
        }
 
        // 0 더하고, 가운데 부분만 변경한 종이를 이어붙이면 되는 규칙
        paper += "0";
        paper += temp_paper;
    }
        
    for(int i = 0; i < paper.size(); i++)
    {
        answer.push_back(paper[i]-'0');
    }
 
    return answer;
}
cs
#include <string>
#include <vector>
using namespace std;
 
long long dp[60010];
 
int solve(int n)
{
    if(n == 0)
    {
        return 1;
    }
        
    if(dp[n] != 0)
    {
        return dp[n];
    }
    
    if(n-2 >= 0)
    {
        dp[n] += (solve(n-2) % 1000000007);
    }
    
    if(n-1 >= 0)
    {
        dp[n] += (solve(n-1) % 1000000007);
    }
    
    return dp[n] % 1000000007;
}
 
int solution(int n) 
{
    int ans = solve(n);
    
    return ans;
}
cs

1.

#include <string>
#include <vector>
#include <iostream>
#include <map>
using namespace std;
 
vector<vector<string>> relations;
vector<int> ans;
int row;
int col;
 
int check_min(int bit)
{
    // 모든 튜플을 유일하게 식별하는 데 꼭 필요한 속성들로만 구성되어있지 않다면, 최소성 만족 X
    for(int i = 0; i < ans.size(); i++)
    {
        if((ans[i] & bit) == ans[i])
        {
            return 0;
        }
    }
 
    // 최소성 만족 O
    return 1;
}
 
int solution(vector<vector<string>> relation) 
{
    relations = relation;
    row = relation.size();
    col = relation[0].size();
    
    // 릴레이션에 있는 모든 튜플에 대해 유일하게 식별되어야 한다 -> 유일성 만족 O
    for(int i = 1; i < (1 << col); i++)
    {
        map<stringint> check;
        
        for(int j = 0; j < row; j++)
        {
            string now;
            
            for(int k = 0; k < col; k++)
            {
                if(i & (1 << k))
                {
                    now += relation[j][k];        
                }
            }
            
            check[now] = 1;
        }
        
        if(check.size() == row && check_min(i) == 1)
        {
            ans.push_back(i);
        }
    }
    
    return ans.size();
}
 
cs

 

2.

#include <string>
#include <vector>
#include <iostream>
#include <map>
using namespace std;
 
vector<vector<string>> relations;
vector<int> ans;
int row;
int col;
 
int check_min(int bit)
{
    // 모든 튜플을 유일하게 식별하는 데 꼭 필요한 속성들로만 구성되어있지 않다면, 최소성 만족 X
    for(int i = 0; i < ans.size(); i++)
    {
        if((ans[i] & bit) == ans[i])
        {
            return 0;
        }
    }
 
    // 최소성 만족 O
    return 1;
}
 
int solution(vector<vector<string>> relation) 
{
    relations = relation;
    row = relation.size();
    col = relation[0].size();
    
    // 릴레이션에 있는 모든 튜플에 대해 유일하게 식별되어야 한다 -> 유일성 만족 O
    for(int i = 1; i < (1 << col); i++)
    {
        map<stringint> check;
        
        for(int j = 0; j < row; j++)
        {
            string now;
            
            for(int k = 0; k < col; k++)
            {
                if(i & (1 << k))
                {
                    now += relation[j][k];        
                }
            }
            
            check[now] = 1;
        }
        
        if(check.size() == row && check_min(i) == 1)
        {
            ans.push_back(i);
        }
    }
    
    return ans.size();
}
 
cs
#include <string>
#include <vector>
#include <iostream>
using namespace std;
 
int row, col;
int blockCnt;
int black = -1;
int boomCnt;
vector<vector<int>> copy_board(55vector<int>(55));
 
void copy(vector<vector<int>> &to, vector<vector<int>> &from)
{
    for(int i = 0; i < row; i++)
    {
        for(int j = 0; j < col; j++)
        {
            to[i][j] = from[i][j];
        }
    }
}
 
int isSquare(int x, int y, int shape, int color, vector<vector<int>> &board) // shape(0): 2*3 , shape(1): 3*2
{
    int colorCnt = 0;
    int blackCnt = 0;
    
    if(shape == 0)
    {
        for(int i = x; i <= x+1; i++)
        {
            for(int j = y; j <= y+2; j++)
            {
                if(board[i][j] == color)
                {
                    colorCnt++;
                }
                else if(board[i][j] == black)
                {
                    blackCnt++;
                }
            }
        }
    }
    else
    {
        for(int i = x; i <= x+2; i++)
        {
            for(int j = y; j <= y+1; j++)
            {
                if(board[i][j] == color)
                {
                    colorCnt++;
                }
                else if(board[i][j] == black)
                {
                    blackCnt++;
                }
            }
        }
    }
    
    if(colorCnt == 4 && blackCnt == 2)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
 
void print(vector<vector<int>> &board)
{
    for(int i = 0; i < col; i++)
    {
        for(int j = 0; j < row; j++)
        {
            cout << board[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;
}
 
void bomb(int x, int y, int shape, vector<vector<int>> &board)
{
    if(shape == 0)
    {
        for(int i = x; i <= x+1; i++)
        {
            for(int j = y; j <= y+2; j++)
            {
                board[i][j] = 0;
                copy_board[i][j] = 0;
            }
        }
    }
    else
    {
        for(int i = x; i <= x+2; i++)
        {
            for(int j = y; j <= y+1; j++)
            {
                board[i][j] = 0;
                copy_board[i][j] = 0;
            }
        }
    }   
}
 
bool fall(vector<vector<int>> &board)
{
    bool boomCheck = false;
    copy(copy_board, board);
    
    // 검은 블록 떨어뜨리기
    for(int i = 0; i < col; i++)
    {    
        for(int j = 0; j < row; j++)
        {   
            if(board[j][i] != 0)
            {
                break;
            }
            
            board[j][i] = black;
         }
    }
    
    // print(board);
    
    for(int i = 1; i <= blockCnt; i++)
    {
        // 2*3 모양
        for(int j = 0; j <= row-2; j++)
        {
            for(int k = 0; k <= col-3; k++)
            {
                if(isSquare(j, k, 0, i, board))
                {
                    boomCheck = true;
                    bomb(j, k, 0, board);
                    boomCnt++;
                }
            }
        }
        
        // 3*2 모양
        for(int j = 0; j <= row-3; j++)
        {
            for(int k = 0; k <= col-2; k++)
            {
                if(isSquare(j, k, 1, i, board))
                {
                    boomCheck = true;
                    bomb(j, k, 1, board);
                    boomCnt++;
                }
            }
        }
    }
    
    copy(board, copy_board);
    
    // print(board);
    
    return boomCheck;
}
 
int solution(vector<vector<int>> board) 
{
    row = board.size();
    col = row;
      
    for(int i = 0; i < row; i++)
    {
        for(int j = 0; j < col; j++)
        {
            if(board[i][j] > blockCnt)
            {
                blockCnt = board[i][j];
            }
        }
    }
    
    while(1)
    {
        bool check = fall(board);
        
        if(check == false)
        {
            break;
        }
    }       
    
    return boomCnt;
}
cs
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
using namespace std;
 
typedef struct node
{
    int x;
    int y;
    int num;
    struct node *left;
    struct node *right;
}node;
 
vector<node> newinfo;
node *root;
vector<int> pre;
vector<int> post;
 
bool cmp(node a, node b)
{
    if(a.y > b.y)
    {
        return true;
    }
    else if(a.y == b.y)
    {
        if(a.x < b.y)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    else
    {
        return false;
    }
}
 
void find_position_and_connect(node *now, int idx)
{        
    if(now == NULL)
    {
        return;
    }
        
    if(now->< newinfo[idx].x)
    {
        if(now->right == NULL)
        {
            node *newNode = (node*)malloc(sizeof(node)*1);
            newNode[0= {newinfo[idx].x, newinfo[idx].y, newinfo[idx].num, NULLNULL};
            now->right = newNode;
            
            return;
        }
        else
        {
            find_position_and_connect(now->right, idx);   
        }
    }
    else
    {
        if(now->left == NULL)
        {
            node *newNode = (node*)malloc(sizeof(node)*1);
            newNode[0= {newinfo[idx].x, newinfo[idx].y, newinfo[idx].num, NULLNULL};
            now->left = newNode;
            
            return;
        }
        else
        {
            find_position_and_connect(now->left, idx);   
        }
    }
}
 
void preOrder(node *now)
{
    if(now == NULL)
    {
        return;
    }
    
    pre.push_back(now->num+1);
    preOrder(now->left);
    preOrder(now->right);
}
 
void postOrder(node *now)
{
    if(now == NULL)
    {
        return;
    }
    
    postOrder(now->left);
    postOrder(now->right);
    post.push_back(now->num+1);
}
 
vector<vector<int>> solution(vector<vector<int>> nodeinfo) 
{    
    vector<vector<int>> answer;
    
    for(int i = 0; i < nodeinfo.size(); i++)
    {
        newinfo.push_back({nodeinfo[i][0], nodeinfo[i][1], i, NULLNULL}); // x, y, 노드번호
    }
    sort(newinfo.begin(), newinfo.end(), cmp);
    
    root = (node*)malloc(sizeof(node)*1);
    root[0= {newinfo[0].x, newinfo[0].y, newinfo[0].num, NULLNULL};
    
    for(int i = 1; i < newinfo.size(); i++)
    {
        find_position_and_connect(root, i);
    }
 
    preOrder(root);
    postOrder(root);
    
    answer.push_back(pre);
    answer.push_back(post);
    
    return answer;
}
cs
#include <string>
#include <vector>
#include <iostream>
using namespace std;
 
void lower(string &a)
{
    for(int i = 0; i < a.size(); i++)
    {
        if('A' <= a[i] && a[i] <= 'Z')
        {
            a[i] = a[i]-'A'+'a';
        }
    }
}
 
int solution(string word, vector<string> pages) 
{
    int stdScore[25= {0};
    double result[25= {0};
    vector<string> my_page[25];
    vector<string> out_page[25];
    
    for(int i = 0; i < pages.size(); i++)
    {
        // 페이지, 찾는 단어 소문자 변환
        int idx = 0;
        lower(pages[i]);
        lower(word);
        
        // my_page
        idx = pages[i].find("<meta property=\"og:url\"", idx);
        idx = pages[i].find("https://", idx);
        
        string my_url = "";
        for(int j = idx; pages[i][j] != '"'; j++)
        {
            my_url += pages[i][j];
        }
        my_page[i].push_back(my_url);
        
        // out_page
        while(pages[i].find("<a href=", idx) != string::npos)
        {
            idx = pages[i].find("<a href=", idx);
            idx = pages[i].find("https://", idx);
            
            string link_url = "";
            for(int j = idx; pages[i][j] != '"'; j++)
            {
                link_url += pages[i][j];
            }       
            out_page[i].push_back(link_url);
        }   
        
        // 기본점수
        idx = 0;
        int wordCnt = 0;
        while(pages[i].find(word, idx) != string::npos)
        {
            bool notWord = false;
            idx = pages[i].find(word, idx);
    
            // 찾은 단어 바로 앞 확인
            if(0 <= idx-1 && idx-1 < pages[i].size() && 'a' <= pages[i][idx-1&& pages[i][idx-1<= 'z')
            {
                notWord = true;
            }
            
            idx += word.size();
            
            // 찾은 단어 뒤 확인
            while(0 <= idx && idx < pages[i].size() && 'a' <= pages[i][idx] && pages[i][idx] <= 'z')
            {
                notWord = true;
                idx++;   
            }
            
            if(notWord == false)
            {
                wordCnt++;
            }
        }   
        stdScore[i] = wordCnt;
    }
    
    // 링크점수 + 매칭점수
    for(int i = 0; i < pages.size(); i++)
    {
        int linkCnt = 0;
        vector<int> link;
            
        for(int j = 0; j < pages.size(); j++)
        {
            if(i == j)
            {
                continue;
            }
            
            for(int k = 0; k < out_page[j].size(); k++)
            {
                if(my_page[i][0== out_page[j][k])
                {
                    link.push_back(j);
                }
            }   
        }
        
        result[i] = stdScore[i];
        for(int j = 0; j < link.size(); j++)
        {
            if(stdScore[link[j]] == 0 || out_page[link[j]].size() == 0)
            {
                continue;    
            }
            
            double linkScore = (double)stdScore[link[j]] / out_page[link[j]].size();
            result[i] += linkScore;
        }
    }
    
    double Max = -1;
    int MaxIdx = -1;
    for(int i = 0; i < pages.size(); i++)
    {
        if(Max < result[i])
        {
            Max = result[i];
            MaxIdx = i;
        }
    }
    
    return MaxIdx;
}
cs

1.

#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
 
vector<int> s;
vector<int> e;
vector<string> name;
vector<string> content;
 
// 재생된 시간도 같을 경우 먼저 입력된 음악 제목을 반환
bool cmp(pair<stringint> a, pair<stringint> b)
{
    if(a.second < b.second)
    {
        return true;
    }
    else
    {
        return false;
    }
}
 
string solution(string m, vector<string> musicinfos) 
{
    string answer = "";
    
    // C# -> c, D# -> d, F# -> f, G# -> g, A#-> a 변환
    for(int i = 0; i < m.size(); i++)
    {
        int idx = 0;
        
        while(m.find("#", idx) != string::npos)
        {
            idx = m.find("#", idx);
            char lower_alphabet = m[idx-1]-'A'+'a';
            string alphabet;
            alphabet += lower_alphabet;
            m.replace(idx-12, alphabet);
        }
    }
    
    for(int i = 0; i < musicinfos.size(); i++)
    {
        int idx = 0;
        
        while(musicinfos[i].find("#", idx) != string::npos)
        {
            idx = musicinfos[i].find("#", idx);
            char lower_alphabet = musicinfos[i][idx-1]-'A'+'a';
            string alphabet;
            alphabet += lower_alphabet;
            musicinfos[i].replace(idx-12, alphabet);
        }
    }
    
    // 시작시각, 끝난시각, 제목, 악보별 정리
    for(int i = 0; i < musicinfos.size(); i++)
    {
        int temp_start = 0;  // 시작시각을 분으로 저장
        int temp_arrive = 0// 도착시각을 분으로 저장
        
        string hour = musicinfos[i].substr(0,2); // 시
        string minute = musicinfos[i].substr(3,2); // 분
        
        temp_start = stoi(hour)*60 + stoi(minute);
        
        hour = musicinfos[i].substr(6,2);
        minute = musicinfos[i].substr(9,2);
        
        temp_arrive = stoi(hour)*60 + stoi(minute);
        
        s.push_back(temp_start);
        e.push_back(temp_arrive);
        
        int j;
        string temp_name;
        for(j = 12; musicinfos[i][j] != ','; j++)
        {
            temp_name += musicinfos[i][j];
        }
        name.push_back(temp_name);
        
        j++;
        string temp_content;
        for(int k = j; k < musicinfos[i].size(); k++)
        {
            temp_content += musicinfos[i][k];
        }
        content.push_back(temp_content);
    }
    
    int max_play = 0// 제일 긴 재생 시간 저장
    vector<pair<stringint>> reserve; // 음악 제목, 인덱스 저장
    
    // 시작입력의 갯수 = 음악의 갯수만큼 반복
    for(int i = 0; i < s.size(); i++)
    {
        int diff = e[i]-s[i];
        string total_content;
        
        for(int j = 0; j < diff; j++)
        {
            total_content += content[i][j % content[i].size()];
        }
        
        if(total_content.find(m) != string::npos)
        {
            // 조건이 일치하는 음악이 여러 개일 때에는 라디오에서 재생된 시간이 제일 긴 음악 제목을 반환
            if(diff > max_play)
            {
                reserve.clear();
                
                max_play = diff;
                reserve.push_back({name[i], i});
            }
            else if(diff == max_play)
            {
                reserve.push_back({name[i], i});
            }
        }
    }
    
    if(reserve.size() == 0)
    {
        answer = "(None)";
    }
    else if(reserve.size() == 1)
    {
        answer = reserve[0].first;
    }
    else
    {
        sort(reserve.begin(), reserve.end(), cmp);   
        answer = reserve[0].first;
    }
    
    return answer;
}
cs

 

2.

#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
 
vector<int> s;
vector<int> e;
vector<string> name;
vector<string> content[110];
 
// 재생된 시간도 같을 경우 먼저 입력된 음악 제목을 반환
bool cmp(pair<stringint> a, pair<stringint> b)
{
    if(a.second < b.second)
    {
        return true;
    }
    else
    {
        return false;
    }
}
 
string solution(string m, vector<string> musicinfos) 
{
    string answer = "";
    
    // 시작시각, 끝난시각, 제목, 악보별 정리
    for(int i = 0; i < musicinfos.size(); i++)
    {
        int temp_start = 0;  // 시작시각을 분으로 저장
        int temp_arrive = 0// 도착시각을 분으로 저장
        
        string hour = musicinfos[i].substr(0,2); // 시
        string minute = musicinfos[i].substr(3,2); // 분
        
        temp_start = stoi(hour)*60 + stoi(minute);
        
        hour = musicinfos[i].substr(6,2);
        minute = musicinfos[i].substr(9,2);
        
        temp_arrive = stoi(hour)*60 + stoi(minute);
        
        s.push_back(temp_start);
        e.push_back(temp_arrive);
        
        int j;
        string temp_name;
        for(j = 12; musicinfos[i][j] != ','; j++)
        {
            temp_name += musicinfos[i][j];
        }
        name.push_back(temp_name);
        
        j++;
        for(int k = j; k < musicinfos[i].size(); k++)
        {
            string temp_content;
            
            if(k+1 < musicinfos[i].size() && musicinfos[i][k+1== '#')
            {
                temp_content += musicinfos[i][k];
                temp_content += musicinfos[i][k+1];
                temp_content += '.'// # 구분을 위해서 . 삽입
                
                k = k+1;
                content[i].push_back(temp_content);
            }
            else
            {
                temp_content += musicinfos[i][k];
                temp_content += '.'// # 구분을 위해서 . 삽입
                
                content[i].push_back(temp_content);
            }
        }
    }
    
    // 악보의 음마다 . 삽입했기때문에 찾는 m의 사이사이마다 . 삽입한 M 생성
    string M;
    for(int i = 0; i < m.size(); i++)
    {
        if(i+1 < m.size() && m[i+1== '#')
        {
            M += m[i];
            M += m[i+1];
            M += '.';
            
            i = i+1;
        }
        else
        {
            M += m[i];
            M += '.';   
        }
    }
    
    int max_play = 0// 제일 긴 재생 시간 저장
    vector<pair<stringint>> reserve; // 음악 제목, 인덱스 저장
    
    // 시작입력의 갯수 = 음악의 갯수만큼 반복
    for(int i = 0; i < s.size(); i++)
    {
        int diff = e[i]-s[i];
        string total_content;
        
        for(int j = 0; j < diff; j++)
        {
            total_content += content[i][j % content[i].size()];
        }
        
        if(total_content.find(M) != string::npos)
        {
            // 조건이 일치하는 음악이 여러 개일 때에는 라디오에서 재생된 시간이 제일 긴 음악 제목을 반환
            if(diff > max_play)
            {
                reserve.clear();
                
                max_play = diff;
                reserve.push_back({name[i], i});
            }
            else if(diff == max_play)
            {
                reserve.push_back({name[i], i});
            }
        }
    }
    
    if(reserve.size() == 0)
    {
        answer = "(None)";
    }
    else if(reserve.size() == 1)
    {
        answer = reserve[0].first;
    }
    else
    {
        sort(reserve.begin(), reserve.end(), cmp);   
        answer = reserve[0].first;
    }
    
    return answer;
}
cs

 

+ Recent posts