알고리즘/SWEA

5672. [Professional] 올해의 조련사

병인 2019. 10. 28. 13:22

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWXRgX36gSIDFAUo&categoryId=AWXRgX36gSIDFAUo&categoryType=CODE

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

<JAVA>

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Solution {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		int T = Integer.parseInt(br.readLine().trim());
		for (int t = 1; t <= T; ++t) {
             sb = new StringBuilder();
			int N = Integer.parseInt(br.readLine().trim());
			char[] arr = new char[N];
			for (int i = 0; i < N; ++i) {
				arr[i] = br.readLine().charAt(0);
			}
			sb.append("#").append(t).append(" ");
			int start = 0;

			int end = arr.length - 1;
			while (start <= end) {
				char startChar = arr[start];
				char endChar = arr[end];
				if (end == start) {
					sb.append(arr[end]);
					break;
				}
				if (startChar < endChar) {
					sb.append(startChar);
					++start;
				} else if (startChar > endChar) {
					sb.append(endChar);
					--end;
				} else if (startChar == endChar) {
					int s = start;
					int e = end;
					while (true) {
						if (s >= e) { // s가 e보다 커지면
							sb.append(arr[s]); // 모두 같다는 의미이므로, 그냥 앞의 것을 더해준다.
							++start;
							break;
						}
						if (arr[s] != arr[e]) { // 앞과 뒤가 다르면
							if (arr[s] < arr[e]) { // 크기 비교해서 추가 해준다.
								sb.append(arr[start]);
								++start;
							} else {
								sb.append(arr[end]);
								--end;
							}
							break;
						}
						++s;
						--e;
					}
				}
			}
			System.out.println(sb);
		}
	}
}

<C++>

#include<iostream>
#include<vector>

using namespace std;
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	int T, N;
	vector<char> v;
	char temp;
	string str;
	cin >> T;
	for (int t = 1; t <= T; ++t) {
		v.clear();
		str = "";
		cin >> N;
		for (int i = 0; i < N; ++i) {
			cin >> temp;
			v.push_back(temp);
		}
		int start = 0;
		int end = N - 1;
		while (start <= end) {
			char startChar = v[start];
			char endChar = v[end];
			if (end == start) {
				str += v[end];
				break;
			}
			if (startChar < endChar) {
				str += startChar;
				++start;
			}
			else if (startChar > endChar) {
				str += endChar;
				--end;
			}
			else if (startChar == endChar) {
				int s = start;
				int e = end;
				while (true) {
					if (s >= e) { // s가 e보다 커지면
						str += v[s]; // 모두 같다는 의미이므로, 그냥 앞의 것을 더해준다.
						++start;
						break;
					}
					if (v[s] != v[e]) { // 앞과 뒤가 다르면
						if (v[s] < v[e]) { // 크기 비교해서 추가 해준다.
							str += v[start];
							++start;
						}
						else {
							str += v[end];
							--end;
						}
						break;
					}
					++s;
					--e;
				}
			}
		}
		cout<<"#"<<t<<" " << str << "\n";
	}
}