본문 바로가기

algorithm

(69)
백준 17608번 막대기 https://www.acmicpc.net/problem/17608 아이디어:1. 우선 스택에 다 넣고, 마지막부터 비교하면서 뺀다. #include #include using namespace std;int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int N, tmp, ans = 0; stack S; cin >> N; while (N--) { cin >> tmp; S.push(tmp); } tmp = S.top(); while (!S.empty()) { if (S.top() > tmp) { tmp = S.top(); ans += 1; } S.pop(); } cout
백준 12605번 단어순서 뒤집기, C++ stringstream으로 문자열 공백 기준 자르기 https://www.acmicpc.net/problem/12605 아이디어:1. 스택에 문자열을 잘라 넣고 마지막에 그대로 꺼내서 출력2. 공백 포함 한줄 문자열 입력 (ex. "this is a test")string tmp;// cin.ignore(); // if you already use cingetline(cin, tmp); 3. 한줄 문자열의 공백으로 잘라서 스택에 입력string t;stack str;stringstream ss(tmp);while(ss >> t) str.push(t); #include #include using namespace std;int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int N; string ..
백준 1874번 스택 수열 https://www.acmicpc.net/problem/1874 아이디어:1. 스택 사용2. 입력한 수열과 현재 자연수 (1~n)을 비교3. 결과를 배열에 저장해서 마지막에 출력4. 현재 수열 값이 현재 자연수보다 크거나 같으면 값이 같아질 때 까지 계속 push하고 마지막에만 pop(ex. 백준 예제 입력의 경우, 수열이 4로 시작한다. 자연수는 1이므로, 4에 도달할 때 까지 계속 push후, 마지막에 pop해서 4를 결과 배열에 저장)5. 현재 수열 값이 현재 자연수보다 작을 때, pop해서 나온 값이 수열 값과 다르면 후입선출의 원리에 의해 수열 만들기가 불가능하다. > NO 출력, flag를 false로 만들고 종료6. 불가능하지 않다면 pop만 실행(ex. 백준 예제 입력의 경우에서 4를 p..
백준 12891번 DNA 비밀번호 https://www.acmicpc.net/problem/12891 아이디어:1. 슬라이딩 윈도우 사용2. 현재 부분문자열의 검사 결과를 저장하고 윈도우가 이동할 때마다 검사 결과를 업데이트 // 12891#include using namespace std;int S, P, checkAnswer, answer = 0;string str;int myArr[4] = { 0 };int checkArr[4] = { 0 };void Add(char c);void Remove(char c);void check();int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> S >> P; cin >> str; for (int i = 0..
백준 1253번 좋다 https://www.acmicpc.net/problem/1253 아이디어:1. vector, 정렬, 투 포인터 사용2. 정수 범위이므로 음수 고려 > 끝 인덱스가 N - 1에서 시작3. 자기자신을 더할 때는 제외한다. // 1253#include #include #include using namespace std;int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long N, answer = 0; cin >> N; vector A(N, 0); long start = 0; long end = 0; for (int i = 0; i > A[i]; } sort(A.begin(), A.end()); for (int i = 0; i..