๐Ÿ•ธ Algorithm/๐Ÿ•ธ ๋ฐฑ์ค€ BaekJoon

[๋ฐฑ์ค€][java][2941][ํฌ๋กœ์•„ํ‹ฐ์•„ ์•ŒํŒŒ๋ฒณ]

yeun.log 2024. 2. 10. 02:27
๋ฐ˜์‘ํ˜•

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

// 	2941	ํฌ๋กœ์•„ํ‹ฐ์•„ ์•ŒํŒŒ๋ฒณ
public class Main {
	
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);

		String word = in.next();
		in.close();
		
		final String[] cr = {"c=", "c-", "dz=", "d-", "lj", "nj", "s=", "z="};
		
		int cnt = 0;
		
		for (int i = 0; i < cr.length; i++) {
			Pattern p = Pattern.compile(cr[i]);
			Matcher m = p.matcher(word);
			
			while (m.find()) {
				cnt++;
			}
			
			word = word.replace(cr[i], " ");
		}
		
		System.out.print(cnt + word.replace(" ", "").length());
	}
}
  • ํฌ๋กœ์•„ํ‹ฐ์•„ ์•ŒํŒŒ๋ฒณ์„ cr๋ฐฐ์—ด์— ๋‹ด์•„ for๋ฌธ์„ ๋Œ๋ฆฌ๋Š”๋ฐ
  • Pattern&Matcher๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐฏ์ˆ˜cnt๋ฅผ ์„ธ์ค๋‹ˆ๋‹ค.
  • ๋‚จ๋Š” ๊ธ€์ž๋“ค์˜ ๊ธธ์ด๋ฅผ ์žฐ ํ›„ cnt์™€ ํ•ฉ์ณ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
๋ฐ˜์‘ํ˜•