Solved.ac Class3++
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();
}
}
성공
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)
}
}