Baekjoon Algorithm | Problem 2941: Croatian Alphabet
Source
https://www.acmicpc.net/problem/2941
Problem
Solve Baekjoon Online Judge problem 2941, Croatian Alphabet.
Input
Follow the input format and constraints given in the original problem statement.
Output
Print the answer required by the problem.
Sample Input 1
ljes=njak
Sample Output 1
6
Sample Input 2
ddz=z=
Sample Output 2
3
Sample Input 3
nljj
Sample Output 3
3
Sample Input 4
c=c=
Sample Output 4
2
Sample Input 5
dz=ak
Sample Output 5
3
Algorithm Classification
- Implementation
- String
Solution
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
String input = br.readLine();
String[] chars = new String[]{"c=", "c-", "dz=", "d-", "lj", "nj", "s=", "z="};
int count = 0;
int fromIndex = 0;
do {
String searchChar = null;
for (String ch : chars) {
if (input.startsWith(ch, fromIndex)) {
searchChar = ch;
break;
}
}
if (searchChar != null) {
fromIndex += searchChar.length();
} else {
fromIndex++;
}
//System.out.println("searchChar=" + searchChar + ", fromIndex=" + fromIndex + ", input.length=" + input.length());
count++;
} while (input.length() > fromIndex);
System.out.println(count);
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main2 {
public static void main(String[] args) throws IOException {
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
String str = br.readLine();
String[] chars = new String[]{"c=", "c-", "dz=", "d-", "lj", "nj", "s=", "z="};
for (String c : chars) {
str = str.replace(c, "_");
}
System.out.println(str.length());
}
}
}