https://www.acmicpc.net/problem/3986
public static class BOJ3986 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
String[] arr = new String[N];
for (int i = 0; i < N; i++) {
arr[i] = br.readLine();
}
int count = 0;
for (String s : arr) {
Stack<Character> stack = new Stack<>();
char[] charArray = s.toCharArray();
for (char c : charArray) {
if (!stack.isEmpty() && stack.peek() == c) {
stack.pop();
} else {
stack.push(c);
}
}
if (stack.isEmpty()) {
count++;
}
}
System.out.println(count);
}
}
https://www.acmicpc.net/problem/11441
public static class BOJ11441 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int[] given = new int[N];
int[] prefixSum = new int[N + 1];
prefixSum[0] = 0;
given[0] = Integer.parseInt(st.nextToken()); // 0번 인덱스 명시적 받아옴
for (int i = 1; i < N; i++) {
given[i] = Integer.parseInt(st.nextToken());
prefixSum[i] = given[i - 1] + prefixSum[i - 1];
}
prefixSum[N] = prefixSum[N - 1] + given[N - 1];
int M = Integer.parseInt(br.readLine());
int[] answer = new int[M];
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int start = Integer.parseInt(st.nextToken()); //1
int end = Integer.parseInt(st.nextToken()); // 3
answer[i] = prefixSum[end] - prefixSum[start - 1];
}
for (int i : answer) {
System.out.println(i);
}
}
}
답이 찍히긴 하지만, prefixSum 배열을 명시 설정 해주는 게 마음에 안들었다.
그래서 그냥 주어지는 값을 추가해가며 누적합 배열을 만들었음
public static class BOJ11441_2 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int[] given = new int[N + 1]; // 원본 어레이 사용하지 않으므로 그냥 바로 해줄것
given[0] = 0;
for (int i = 1; i <= N; i++) {
given[i] = Integer.parseInt(st.nextToken());
given[i] += given[i - 1]; // 여기서 바로 그냥 계싼때림
}
int M = Integer.parseInt(br.readLine());
int[] answer = new int[M];
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int start = Integer.parseInt(st.nextToken());
int end = Integer.parseInt(st.nextToken());
answer[i] = given[end] - given[start - 1];
}
for (int i : answer) {
System.out.println(i);
}
}
}