#include <string>
#include <vector>
#include <algorithm>
using namespace std;
 
int solution(vector<int> citations) 
{
    sort(citations.begin(), citations.end());
    
    for(int k = citations[citations.size()-1]; k >= 0; k--)
    {
        int cnt = 0;
        
        for(int i = citations.size()-1; i >= 0; i--)
        {
            if(k <= citations[i])
            {
                cnt++;
            }
            else
            {
                break;
            }
        }   
        
        if(cnt >= k && citations.size()-cnt <= k)
        {
            return k;
        }
    }
}
cs
#include <iostream>
using namespace std;
 
long long gcd(long long a, long long b)
{
    if(b == 0)
    {
        return a;
    }
    else
    {
        return gcd(b, a%b);
    }
}
 
long long solution(int w,int h)
{
    long long W = w;
    long long H = h;
    long long divide = gcd(W, H);
    
    long long notUse = (W / divide) + (H / divide) - 1;
    notUse *= divide;
    
    return W * H - notUse;
}
cs

1. 

#include <string>
#include <vector>
#include <map>
#include <iostream>
using namespace std;
 
vector<int> solution(string msg) 
{
    vector<int> answer;
    map<stringint> dic;
    
    for(int i = 0; i < 26; i++)
    {
        char alphabet = 'A' + i;
        string temp;
        temp += alphabet;
        
        dic[temp] = i+1;
    }
 
    string now;
    for(int i = 0; i < msg.size(); i++)
    {
        now += msg[i];
        
        // 사전에서 못찾은 경우
        if(dic.find(now) == dic.end())
        {
            // 사전에 입력 추가
            dic[now] = dic.size() + 1;
            
            // 한 단계 전의 입력을 출력해야되기 때문에 맨 뒤 문자 삭제
            now.pop_back();
            
            // 출력
            answer.push_back(dic[now]);
            
            // 초기화
            now.clear();
            
            i--;
        }      
    }
    
    // 남은 입력
    if(now.size() > 0)
    {
        answer.push_back(dic[now]);
    }  
    
    return answer;
}
cs

 

2.

#include <string>
#include <vector>
#include <iostream>
using namespace std;
 
vector<int> solution(string msg) 
{
    vector<int> answer;
    vector<pair<stringint>> dic;
    
    for(int i = 0; i < 26; i++)
    {
        char alphabet = 'A' + i;
        string temp;
        temp += alphabet;
        
        dic.push_back({temp, i+1});
    }
    
    int nowIdx = 0;
    while(nowIdx != msg.size())
    {
        string now;
        bool find = true;
 
        while(find == true)
        {
            // 마지막까지 남아있는 문자열 처리를 위한 구문
            if(nowIdx == msg.size())
            {
                for(int i = 0; i < dic.size(); i++)
                {
                    if(now == dic[i].first)
                    {
                        answer.push_back(dic[i].second);
                        return answer;
                    }
                }
            }
            
            find = false;
            now += msg[nowIdx];
            
            // 사전에서 입력을 찾는 과정
            for(int i = 0; i < dic.size(); i++)
            {
                if(now == dic[i].first)
                {
                    find = true;
                    break;
                }
            }
            
            // 입력을 찾지 못한 경우
            if(find == false)
            {
                // 사전추가
                dic.push_back({now, dic.size()+1});
                
                // 한 단계 전의 입력을 출력해야되기 때문에 맨 뒤 문자 삭제
                now.pop_back();
                
                for(int i = 0; i < dic.size(); i++)
                {
                    if(now == dic[i].first)
                    {
                        // 출력
                        answer.push_back(dic[i].second);
                        break;
                    }
                }
            }
            // 입력을 찾은 경우 : 인덱스 증가
            else
            {
                nowIdx += 1;
            }
        }
    }      
    
    return answer;
}
cs
#include <string>
#include <vector>
#include <iostream>
using namespace std;
 
string change(int num, int n)
{
    string temp;
    
    if(num == 0)
    {
        return "0";
    }
    
    while(num > 0)
    {
        if(num % n < 10)
        {
            temp = to_string(num % n) + temp;   
        }
        else
        {
            if(num % n == 10)
            {
                temp = "A" + temp;
            }
            else if(num % n == 11)
            {
                temp = "B" + temp;
            }
            else if(num % n == 12)
            {
                temp = "C" + temp;
            }
            else if(num % n == 13)
            {
                temp = "D" + temp;
            }
            else if(num % n == 14)
            {
                temp = "E" + temp;
            }
            else if(num % n == 15)
            {
                temp = "F" + temp;
            }
        }
        
        num /= n;
    }
    
    return temp;
}
 
string solution(int n, int t, int m, int p) 
{
    string answer = "";
    
    string num;
    int cnt = 0;
    
    while(num.size() < t*m)
    {
        num = num + change(cnt, n);
        cnt++;
    }
    
    for(int i = p-1; i < num.size(); i += m)
    {
        answer += num[i];
        
        if(answer.size() == t)
        {
            return answer;
        }
    }
}
cs
#include <string>
#include <iostream>
#include <vector>
using namespace std;
 
int solution(string dartResult) 
{
    int answer = 0;
    vector<int> reserve;
    
    for(int i = 0; i < dartResult.size();)
    {
        string score;
        int j;
        for(j = i; dartResult[j] != 'S' && dartResult[j] != 'D' && dartResult[j] != 'T'; j++)
        {
            score += dartResult[j];
        }
        
        int scoreNum = stoi(score);
        if(dartResult[j] == 'S')
        {
            reserve.push_back(scoreNum);
        }
        else if(dartResult[j] == 'D')
        {
            reserve.push_back(scoreNum * scoreNum);
        }
        else if(dartResult[j] == 'T')
        {
            reserve.push_back(scoreNum * scoreNum * scoreNum);
        }
        
        j++;
        
        if(dartResult[j] == '*')
        {
            reserve[reserve.size()-1*= 2;
            if(reserve.size() >= 2)
            {
                reserve[reserve.size()-2*= 2;    
            }
            j++;
        }
        else if(dartResult[j] == '#')
        {
            reserve[reserve.size()-1*= -1;
            j++;
        }
        
        i = j;        
    }
    
    for(int i = 0; i < reserve.size(); i++)
    {
        answer += reserve[i];
    }
    
    return answer;
}
cs
#include <iostream>
#include <string>
#include <vector>
using namespace std;
 
string change(int num, int len)
{
    string temp;
    
    while(num > 0)
    {
        temp = to_string(num % 2+ temp;
        num /= 2;
    }
    
    while(len != temp.size())
    {
        temp = "0" + temp;
    }
    
    return temp;
}
 
vector<string> solution(int n, vector<int> arr1, vector<int> arr2) 
{
    vector<string> answer;
    vector<string> set1;
    vector<string> set2;
 
    for(int i = 0; i < arr1.size(); i++)
    {
        if(arr1[i] != 0)
        {
            set1.push_back(change(arr1[i], n));   
        }
        else
        {
            string temp;
            for(int j = 1; j <= n; j++)
            {
                temp += "0";
            }
            
            set1.push_back(temp);
        }
        
        if(arr2[i] != 0)
        {
            set2.push_back(change(arr2[i], n));   
        }
        else
        {
            string temp;
            for(int j = 1; j <= n; j++)
            {
                temp += "0";
            }
            
            set2.push_back(temp);
        }
    }
    
    for(int i = 0; i < set1.size(); i++)
    {
        string temp;
        
        for(int j = 0; j < set1[i].size(); j++)
        {
            if(set1[i][j] == '1' || set2[i][j] == '1')
            {
                temp += "#";
            }
            else
            {
                temp += " ";
            }
        }
        
        answer.push_back(temp);
    }
    
    return answer;
}
cs
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
 
string tolower(string s)
{
    for(int i = 0; i < s.size(); i++)
    {
        if('A' <= s[i] && s[i] <= 'Z')
        {
            s[i] = s[i] - 'A' + 'a';
        }
    }
           
    return s;
}
 
int solution(int cacheSize, vector<string> cities)
{
    int answer = 0;
    vector<string> cache;
    
    if(cacheSize == 0)
    {
        return cities.size() * 5;
    }
 
    for(int i = 0; i < cities.size(); i++)
    {
        string city = tolower(cities[i]);
        // 캐시에 공간이 남아있을때
        if(cache.size() < cacheSize)
        {
            // 캐시검색
            bool find = false;
            for(int j = 0; j < cache.size(); j++)
            {
                // 찾으면
                if(city == cache[j])
                {
                    for(int k = j; k < cache.size()-1; k++)
                    {
                        cache[k] = cache[k+1];
                    }
                    cache[cache.size()-1= city;
                    
                    find = true;
                    answer += 1
                    break;
                }
            }
            
            // 못찾으면
            if(find == false)
            {
                cache.push_back(city);
                answer += 5;    
            }
        }   
        // 캐시에 공간이 남아있지않을때
        else
        {
            // 캐시검색
            bool find = false;
            for(int j = 0; j < cache.size(); j++)
            {
                // 찾으면
                if(city == cache[j])
                {
                    for(int k = j; k < cache.size()-1; k++)
                    {
                        cache[k] = cache[k+1];
                    }
                    cache[cache.size()-1= city;
                    
                    find = true;
                    answer += 1
                    break;
                }
            }
            
            // 못찾으면
            if(find == false)
            {
                for(int k = 0; k < cache.size()-1; k++)
                {
                    cache[k] = cache[k+1];
                }
                cache[cache.size()-1= city;
                
                answer += 5;
            }
        }
    }
    
    return answer;
}
cs
#include <iostream>
#include <string>
#include <vector>
using namespace std;
 
int solution(string str1, string str2) 
{
    int overlapCnt = 0;
    int visited[1000= {0};
    vector<string> set1;
    vector<string> set2;
    
    for(int i = 0; i <= str1.size()-2; i++)
    {
        if((('A' <= str1[i] && str1[i] <= 'Z'|| ('a' <= str1[i] && str1[i] <= 'z')) && (('A' <= str1[i+1&& str1[i+1<= 'Z'|| ('a' <= str1[i+1&& str1[i+1<= 'z')))
        {
            string temp;
            temp += tolower(str1[i]);
            temp += tolower(str1[i+1]);
            
            set1.push_back(temp);
        }
    }
    
    for(int i = 0; i <= str2.size()-2; i++)
    {   
        if((('A' <= str2[i] && str2[i] <= 'Z'|| ('a' <= str2[i] && str2[i] <= 'z')) && (('A' <= str2[i+1&& str2[i+1<= 'Z'|| ('a' <= str2[i+1&& str2[i+1<= 'z')))
        {
            string temp;
            temp += tolower(str2[i]);
            temp += tolower(str2[i+1]);
            
            set2.push_back(temp);
        }
    }
    
    for(int i = 0; i < set1.size(); i++)
    {
        for(int j = 0; j < set2.size(); j++)
        {
            if(set1[i] == set2[j] && visited[j] == 0)
            {
                visited[j] = 1;
                overlapCnt++;
                break;
            }
        }
    }
    
    if(overlapCnt == 0)
    {
        if(set1.size() + set2.size() - overlapCnt == 0)
        {
            return 65536;       
        }
        else
        {
            return 0;
        }
    }
    else
    {
        double zacard = overlapCnt / (double)(set1.size() + set2.size() - overlapCnt);
 
        return zacard * 65536;
    }
}
cs

+ Recent posts