Baekjoon - 5525

Tadap·2023년 10월 12일
0

Baekjoon

목록 보기
50/94
post-custom-banner

문제

Solved.ac Class3++

1차시도

public class Main {
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int iCount = Integer.parseInt(br.readLine());
		int length = Integer.parseInt(br.readLine());
		int count = 0;
		int targetSize = 1 + 2 * iCount;
		String targetString = makeTargetString(targetSize);

		Stack<Integer> startPointStack = new Stack<>();
		String[] data = br.readLine().split("");

		for (int i = 0; i < length; i++) {
			if (data[i].equals("I")) {
				startPointStack.add(i);
			}
		}

		while (!startPointStack.empty()) {
			Integer point = startPointStack.pop();
			StringBuilder sb = new StringBuilder();
			if (length - point >= targetSize) {
				for (int i = point; i < point + targetSize; i++) {
					sb.append(data[i]);
				}
				String checkString = sb.toString();
				if (targetString.equals(checkString)) {
					count++;
				}
			}
		}
		System.out.println(count);
	}

	private static String makeTargetString(int targetSize) {
		StringBuilder sb = new StringBuilder();
		sb.append("I");
		for (int i = 0; i < (targetSize - 1) / 2; i++) {
			sb.append("O").append("I");
		}

		return sb.toString();
	}
}

부분 성공

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Stack;

public class Main {
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int iCount = Integer.parseInt(br.readLine());
		int length = Integer.parseInt(br.readLine());

		int targetSize = 1 + 2 * iCount;
		String targetString = makeTargetString(targetSize);

		Stack<Integer> startPointStack = new Stack<>();
		String data = br.readLine();
		char[] charData = data.toCharArray();

		int count = 0;
		int answer = 0;
		for (int i = 1; i < length - 1; ) {
			if (charData[i] == 'O' && charData[i + 1] == 'I') {
				count++;
				if (count == iCount) {
					if (charData[i - (count * 2) + 1] == 'I') {
						answer++;
					}
					count--;
				}
				i += 2;
			} else {
				count = 0;
				i++;
			}
		}
		System.out.println(answer);
	}

	private static String makeTargetString(int targetSize) {
		StringBuilder sb = new StringBuilder();
		sb.append("I");
		for (int i = 0; i < (targetSize - 1) / 2; i++) {
			sb.append("O").append("I");
		}

		return sb.toString();
	}
}
  1. OI 가 반복되니 OI를 찾는다
  2. 반복 횟수가 만족하면 맨 앞이 I로 시작하는지 찾는다.
  3. count를 -1 해서 IOIOIOI 처럼 나와 반복되는 경우 처리를 해준다.
  4. 만약 만족하지 않으면 초기화 후 다음수를 체크한다.

    성공

ToKotlin

fun main() {
    ToKotlin().solve()
}

class ToKotlin {
    fun solve() {
        val iCount: Int = readln().toInt()
        val length: Int = readln().toInt()
        val data: String = readln()
        val charData = data.toCharArray()

        var count = 0
        var answer = 0
        var i = 1


        while (i < length - 1) {
            if (charData[i] == 'O' && charData[i + 1] == 'I') {
                count++
                if (count == iCount) {
                    if (charData[i - (count * 2) + 1] == 'I') {
                        answer++
                    }
                    count--
                }
                i += 2
            } else {
                count = 0
                i++
            }
        }

        println(answer)
    }
}

post-custom-banner

0개의 댓글