#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

+ Recent posts