Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

56 9kyo hwang #204

Merged
merged 7 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <iostream>
#include <string>
#include <unordered_set>
#include <unordered_map>

using namespace std;

int main()
{
cin.tie(nullptr)->sync_with_stdio(false);

unordered_set<string> Storage;
unordered_map<string, int> NicknameMap;

int N; cin >> N;
while(N--)
{
string Nickname; cin >> Nickname;
NicknameMap[Nickname]++;

bool bEmplaced = false;
string Prefix{};
for(const char& ch : Nickname)
{
Prefix += ch;
if(Storage.count(Prefix) == 0 && !bEmplaced)
{
cout << Prefix << "\n";
bEmplaced = true;
}
Storage.emplace(Prefix);
}

if(!bEmplaced)
{
string Prefix = Nickname;
if(NicknameMap[Nickname] > 1)
{
Prefix += to_string(NicknameMap[Nickname]);
}
Storage.emplace(Prefix);
cout << Prefix << "\n";
}
}

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <string>
#include <vector>
#include <unordered_set>

using namespace std;

bool IsUnique(const vector<vector<string>>& Relation, const int RowSize, const int ColSize, int AttributeSet)
{
vector<int> Columns;
for(int Column = 0; Column < ColSize; ++Column)
{
if((AttributeSet >> Column) & 1)
{
Columns.emplace_back(Column);
}
}

unordered_set<string> Set;
for(int Row = 0; Row < RowSize; ++Row)
{
string Attributes{};
for(int Column : Columns)
{
Attributes += Relation[Row][Column];
}
Set.emplace(Attributes);
}

return Set.size() == RowSize;
}

bool IsMinimal(const vector<int>& CandidateKeys, int AttributeSet)
{
for(int CandidateKey : CandidateKeys)
{
if((AttributeSet & CandidateKey) == CandidateKey)
{
return false;
}
}
return true;
}

int solution(vector<vector<string>> Relation)
{
const size_t RowSize = Relation.size(), ColSize = Relation.front().size();
vector<int> CandidateKeys;

for(int AttributeSet = 1; AttributeSet < (1 << ColSize); ++AttributeSet)
{
if(!IsMinimal(CandidateKeys, AttributeSet)) continue;
if(!IsUnique(Relation, RowSize, ColSize, AttributeSet)) continue;

CandidateKeys.emplace_back(AttributeSet);
}

return CandidateKeys.size();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
#include <iostream>
#include <set>

using namespace std;

class Database
{
public:
int Recommend1(int G, int x)
{
if(x == 1)
{
return Recommend1P(G);
}
else if(x == -1)
{
return Recommend1N(G);
}
}

int Recommend2(int x)
{
if(x == 1)
{
return Recommend2P();
}
else if(x == -1)
{
return Recommend2N();
}
}

int Recommend3(int x, int L)
{
if(x == 1)
{
return Recommend3P(L);
}
else if(x == -1)
{
return Recommend3N(L);
}
}

void Add(int P, int L, int G)
{
ProblemByLevel[L].emplace(P);
ProblemByLevelAndGroup[L][G].emplace(P);
Problems[P] = {L, G};
}

void Solved(int P)
{
ProblemByLevel[Problems[P].first].erase(P);
ProblemByLevelAndGroup[Problems[P].first][Problems[P].second].erase(P);
}

private:
set<int> ProblemByLevel[101];
set<int> ProblemByLevelAndGroup[101][101];
pair<int, int> Problems[100001];

int Recommend1P(int G)
{
for(int L = 100; L >= 1; --L)
{
if(!ProblemByLevelAndGroup[L][G].empty())
{
return *(--ProblemByLevelAndGroup[L][G].end());
}
}
}

int Recommend1N(int G)
{
for(int L = 1; L <= 100; ++L)
{
if(!ProblemByLevelAndGroup[L][G].empty())
{
return *ProblemByLevelAndGroup[L][G].begin();
}
}
}

int Recommend2P()
{
for(int L = 100; L >= 1; --L)
{
if(!ProblemByLevel[L].empty())
{
return *(--ProblemByLevel[L].end());
}
}
}

int Recommend2N()
{
for(int L = 1; L <= 100; ++L)
{
if(!ProblemByLevel[L].empty())
{
return *ProblemByLevel[L].begin();
}
}
}

int Recommend3P(int L)
{
for(; L <= 100; ++L)
{
if(!ProblemByLevel[L].empty())
{
return *ProblemByLevel[L].begin();
}
}
return -1;
}

int Recommend3N(int L)
{
for(L -= 1; L >= 1; --L)
{
if(!ProblemByLevel[L].empty())
{
return *(--ProblemByLevel[L].end());
}
}
return -1;
}
};

int main()
{
cin.tie(nullptr)->sync_with_stdio(false);

Database* DB = new Database();

int N; cin >> N;
while(N--)
{
int P, L, G; cin >> P >> L >> G;
DB->Add(P, L, G);
}

int M; cin >> M;
while(M--)
{
string Command; cin >> Command;
if(Command == "recommend") // find Max/Min in Category G
{
int G, x; cin >> G >> x;
cout << DB->Recommend1(G, x) << "\n";
}
else if(Command == "recommend2") // find Max/Min any Category
{
int x; cin >> x;
cout << DB->Recommend2(x) << "\n";
}
else if(Command == "recommend3") // find lower bound
{
int x, L; cin >> x >> L;
cout << DB->Recommend3(x, L) << "\n";
}
else if(Command == "add")
{
int P, L, G; cin >> P >> L >> G;
DB->Add(P, L, G);
}
else if(Command == "solved")
{
int P; cin >> P;
DB->Solved(P);
}
}

delete DB;

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

const vector<pair<int, int>> Offset{{-1, 0}, {0, 1}, {1, 0}, {0, -1}};

size_t N, M;
vector<vector<vector<bool>>> Visited; // (x, y, dir)

inline void Compensation(int& Val, int MinVal, int MaxVal)
{
if(Val < MinVal) Val = MaxVal;
else if(Val > MaxVal) Val = 0;
}

int DFS(const vector<string>& Grid, int x, int y, int d, int CycleLength = 1)
{
Visited[x][y][d] = true;

int nd;
switch(Grid[x][y])
{
case 'S':
nd = d;
break;
case 'L':
nd = d - 1;
Compensation(nd, 0, 3);
break;
case 'R':
nd = d + 1;
Compensation(nd, 0, 3);
break;
}

const auto& [dx, dy] = Offset[nd];
int nx = x + dx, ny = y + dy;
Compensation(nx, 0, N - 1), Compensation(ny, 0, M - 1);

if(Visited[nx][ny][nd])
{
return CycleLength;
}
else
{
return DFS(Grid, nx, ny, nd, CycleLength + 1);
}
}

vector<int> solution(vector<string> Grid)
{
N = Grid.size(), M = Grid[0].size();
Visited.assign(N, vector(M, vector(4, false)));

vector<int> Answer;
for(int x = 0; x < N; ++x)
{
for(int y = 0; y < M; ++y)
{
for(int d = 0; d < 4; ++d)
{
if(!Visited[x][y][d])
{
Answer.emplace_back(DFS(Grid, x, y, d));
}
}
}
}

sort(Answer.begin(), Answer.end());

return Answer;
}
8 changes: 7 additions & 1 deletion 9-kyo-hwang/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,10 @@
| 47์ฐจ์‹œ | 2024.5.16 | Brute Force | [14500 ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ](https://www.acmicpc.net/problem/14503) | [#171](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/171) |
| 48์ฐจ์‹œ | 2024.5.20 | Shortest Path | [1162 ๋„๋กœํฌ์žฅ](https://www.acmicpc.net/problem/1162) | [#175](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/175) |
| 49์ฐจ์‹œ | 2024.5.23 | Data Structure | [21939 ๋ฌธ์ œ ์ถ”์ฒœ ์‹œ์Šคํ…œ Version 1](https://www.acmicpc.net/problem/21939) | [#179](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/179) |
| 50์ฐจ์‹œ | 2024.5.25 | Graph Traversal | [26146 ์ฆ‰ํฅ ์—ฌํ–‰ (Easy)](https://www.acmicpc.net/problem/26146) | [#182](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/182) |
| 50์ฐจ์‹œ | 2024.5.25 | Graph Traversal | [26146 ์ฆ‰ํฅ ์—ฌํ–‰ (Easy)](https://www.acmicpc.net/problem/26146) | [#182](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/182) |
| 51์ฐจ์‹œ | 2024.5.30 | Data Structure | [21944 ๋ฌธ์ œ ์ถ”์ฒœ ์‹œ์Šคํ…œ Version 2](https://www.acmicpc.net/problem/21944) | [#185](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/185) |
| 52์ฐจ์‹œ | 2024.6.03 | Data Structure | [2019 KAKAO BLIND RECRUITMENT ํ›„๋ณดํ‚ค](https://school.programmers.co.kr/learn/courses/30/lessons/42890) | [#187](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/187) |
| 53์ฐจ์‹œ | 2024.7.01 | Graph Traversal | [์›”๊ฐ„ ์ฝ”๋“œ ์ฑŒ๋ฆฐ์ง€ ์‹œ์ฆŒ3 ๋น›์˜ ๊ฒฝ๋กœ ์‚ฌ์ดํด](https://school.programmers.co.kr/learn/courses/30/lessons/86052) | [#193](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/193) |
| 54์ฐจ์‹œ | 2024.7.04 | Data Structure | [16934 ๊ฒŒ์ž„ ๋‹‰๋„ค์ž„](https://www.acmicpc.net/problem/16934) | [#197](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/197) |
| 55์ฐจ์‹œ | 2024.7.09 | Simulation | [17144 ๋ฏธ์„ธ๋จผ์ง€ ์•ˆ๋…•!](https://www.acmicpc.net/problem/16934) | [#202](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/202) |
| 56์ฐจ์‹œ | 2024.7.12 | Tree | [15681 ํŠธ๋ฆฌ์™€ ์ฟผ๋ฆฌ](https://www.acmicpc.net/problem/15681) | [#204](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/204) |
Loading
Loading