Programing 187

[C++][백준 1522] 문자열 교환

https://www.acmicpc.net/problem/1522풀이직접 교환을 진행하는 것보단 최소 횟수로 교환이 진행된 문자열과 비교를 통해 총 몇 번 교환해야 하는지를 알아내는 방식이 나을 것 같았다(풀고 나서 알았지만 이와 같은 방식을 슬라이딩 윈도우 알고리즘이라고 한다) 입력받은 문자열에서 a만 남긴 문자열을 만들어주고, 해당 문자열을 오른쪽으로 한 칸씩 이동시켜가며 입력받은 문자열과 일대일로 비교하며 다를 경우 count를 올려준다 문자열의 끝은 서로 연결되어 있다고 가정하기 때문에 만약 문자열의 길이를 넘어간 인덱스에 접근할 차례면 앞으로 보내준다이런 식으로 한 칸씩 이동해 가면서 요소들을 비교하는 식으로 진행하였다오른쪽으로 계속 이동하다 문자열의 길이를 넘어가는 경우 위와 같이 비교한다 최..

[C++][백준 20291] 파일 정리

https://www.acmicpc.net/problem/20291 제출 코드#include #include #include using namespace std;int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; map m; while(n--) { string str; cin >> str; m[str.substr(str.find('.') + 1)]++; } for (auto res : m) cout C++에서 문자열을 특정 기준으로 나눌 때 substr(시작 위치, 길이) 함수를 사용할 수 있다* 길이: 시..

[C++][백준 1929] 소수 구하기

https://www.acmicpc.net/problem/1929 제출 코드(시간 초과)#include #include using namespace std;vector dp;int pnt = -1;bool getPrimeNum(int num){ for (int i = 0; i > m >> n; for (int i = 2; i 저장된 소수들로 나눠서 소수를 판별하는 함수를 만들어서 사용했는데 시간초과가 떴다 소수를 판별할 수 있는 다른 방법을 찾아보다가 에라토스테네스의 체를 발견하였다#include #include using namespace std;int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int ..

[C++][백준 2839] 설탕 배달

https://www.acmicpc.net/problem/2839 제출 코드#include using namespace std;int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int count = 0; int n, left = 0; cin >> n; while (1) { if (n % 5 == 0) break; n -= 3; count++; if (n 그리디나 DP에 대해 잘 모르고 작성한 코드라 밑에 각 접근법 별로 정리해두겠다 1. 그리디 알고리즘그리디 알고리즘이란 매 단계마다 최적으로 보이는 선택을 반복해 최종적으로 최적에 가까운 해답을 ..

[C++][백준 7568] 덩치

https://www.acmicpc.net/problem/7568 제출 코드#include #include using namespace std;int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; int repeat = 1; vector> v; cin >> n; for (int i = 0; i p; cin >> p.first >> p.second; v.push_back(p); } for (int i = 0; i n의 최댓값이 50이기 때문에 매번 모든 사람과 비교해서 등수를 매기는 것이 가능하다고 판단했다grade를 1로 초기화해주고 벡터 내 모든 ..