[SWEA] 나는 개구리로소이다

문제 보러가기

제한사항

각 테스트 케이스의 첫 번째 줄에 개구리들의 울음소리를 나타내는 길이 5 이상 2,500이하인 문자열이 주어진다. 이 문자열은 ‘c’, ‘r’, ‘o’, ‘a’, ‘k’로만 이루어져 있다.

개구리의 울음소리로 불가능한 경우 -1을 출력하면 된다.

첫번째 생각
  1. 개구리의 울음소리가 온전하게 이루어지는지를 확인하기 위해

    c r o a k의 갯수를 관리하는 변수를 선언한다.

1
int cCount=0,rCount=0,oCount=0,aCount=0,kCount=0;
  1. 만약 문자열이 끝났을때, cCount가 남아있다면 개구리의 울음소리가 불가능한 경우이므로 -1을 출력한다.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.util.ArrayList;
import java.util.Scanner;

public class Solution {
	static int res=0;
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for(int tc=1;tc<=T;tc++) {
			res =0;
			int ret=0;
			int cCount=0,rCount=0,oCount=0,aCount=0,kCount=0;
			String s = sc.next();
			for(int i=0; i<s.length(); i++) {
				char cha = s.charAt(i);
				switch (cha) {
					case 'c': {
						cCount++;
						rCount++;
						
						if(cCount>ret) {
							ret = cCount;
						}
						break;
					}
					case 'r': {
						if (rCount == 0) {
							res = -1;
						} else {
							rCount--;
							oCount++;
						}
						break;
					}
					
					case 'o': {
						if (oCount == 0) {
							res = -1;
						} else {
							oCount--;
							aCount++;
						}
						break;
					}
					
					case 'a': {
						if (aCount == 0) {
							res = -1;
						} else {
							aCount--;
							kCount++;
						}
						break;
					}
					
					case 'k': {
						if (kCount == 0) {
							res = -1;
						} else {
							cCount--;
							kCount--;
						}
						break;
					}
				}
			}
			
			if (cCount > 0) {
				res = -1;
			} else if (res != -1){
				res = ret;
			}
			
			System.out.println("#"+tc+" "+res);
		}
		
	}

}