#include <iostream>
#include <algorithm>
using namespace std;
int Cim(string s1, string s2) { // 대소문자 무시하고 문자열 비교하는 함수
transform(s1.begin(), s1.end(), s1.begin(), ::tolower);
transform(s2.begin(), s2.end(), s2.begin(), ::tolower);
if (s1.compare(s2) == 0)
return 1; // 문자열이 같다면 1
return 0; // 다르다면 0
}
void LRU(int cachesize, string* citinames, int arrlength) {
if (cachesize == 0) { // 캐시 사이즈가 0이라면 원소 개수*5 출력 후 종료
cout << arrlength * 5 << endl;
return;
}
string* cache = new string[cachesize];
int* num = new int[cachesize];
int large = 0;
int large_1 = 0;
int time = 0;
int _switch = 0;
for (int i = 0; i < cachesize; i++) {
cache[i] = "0";
num[i] = 0;
}
for (int i = 0; i < arrlength; i++) {
for (int i = 0; i < cachesize; i++) {
if (cache[i] == "0")
num[i] = 0;
else
num[i]++;
}
for (int c = 0; c < cachesize; c++) { // 캐시 내에 존재하는지 체크
if (Cim(cache[c], citinames[i]) == 1) {
time += 1;
_switch = 1;
break;
}
}
if (_switch == 1) {// 캐시 체크 스위치
_switch = 0;
continue;
}
for (int j = 0; j < cachesize; j++) { // 캐시에 없다면
if (cache[j] == "0") { // 캐시가 비어있다면
cache[j] = citinames[i]; // 비어있는 캐시에 문자열 저장
time += 5; // hit 5초 증가
num[j]++; // 채운 캐시 카운트
break;
}
if (j == cachesize - 1) { // 비어있는 캐시가 없다면
large = num[0];
for (int g = 1; g < cachesize; g++) // 가장 큰 캐시 카운트 체크
large = large > num[g] ? large : num[g];
for (int g = 0; g < cachesize; g++) {
if (large == num[g]) {
large_1 = g;
}
}
cache[large_1] = citinames[i]; // 가장 큰 카운트 캐시에 문자열 저장
num[large_1] = 0; // 해당 캐시 카운트 초기화
time += 5; // hit 초 증가
break;
}
}
}
cout << time << endl;;
}
int main()
{
string cities1[] = { "Jeju", "Pangyo", "Seoul", "NewYork", "LA", "Jeju", "Pangyo", "Seoul", "NewYork", "LA" };
string cities2[] = { "Jeju", "Pangyo", "Seoul", "Jeju", "Pangyo", "Seoul", "Jeju", "Pangyo", "Seoul" };
string cities3[] = { "Jeju", "Pangyo", "Seoul", "NewYork", "LA", "SanFrancisco", "Seoul", "Rome", "Paris", "Jeju", "NewYork", "Rome" };
string cities4[] = { "Jeju", "Pangyo", "Seoul", "NewYork", "LA", "SanFrancisco", "Seoul", "Rome", "Paris", "Jeju", "NewYork", "Rome" };
string cities5[] = { "Jeju", "Pangyo", "NewYork", "newyork" };
string cities6[] = { "Jeju", "Pangyo", "Seoul", "NewYork", "LA" };
LRU(3, cities1, sizeof(cities1) / sizeof(string));
LRU(3, cities2, sizeof(cities2) / sizeof(string));
LRU(2, cities3, sizeof(cities3) / sizeof(string));
LRU(5, cities4, sizeof(cities4) / sizeof(string));
LRU(2, cities5, sizeof(cities5) / sizeof(string));
LRU(0, cities6, sizeof(cities6) / sizeof(string));
}