๐Ÿ—๏ธ Algorithm/๐ŸŸฉ ๋ฐฑ์ค€

๐ŸŸฉ [๋ฐฑ์ค€] [Java] [Silver5] 1316๋ฒˆ_๊ทธ๋ฃน ๋‹จ์–ด ์ฒด์ปค

Dbswnstjd 2023. 12. 8. 15:36

๋ฌธ์ œ

https://www.acmicpc.net/problem/1316

 

1316๋ฒˆ: ๊ทธ๋ฃน ๋‹จ์–ด ์ฒด์ปค

๊ทธ๋ฃน ๋‹จ์–ด๋ž€ ๋‹จ์–ด์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ๋ฌธ์ž์— ๋Œ€ํ•ด์„œ, ๊ฐ ๋ฌธ์ž๊ฐ€ ์—ฐ์†ํ•ด์„œ ๋‚˜ํƒ€๋‚˜๋Š” ๊ฒฝ์šฐ๋งŒ์„ ๋งํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด, ccazzzzbb๋Š” c, a, z, b๊ฐ€ ๋ชจ๋‘ ์—ฐ์†ํ•ด์„œ ๋‚˜ํƒ€๋‚˜๊ณ , kin๋„ k, i, n์ด ์—ฐ์†ํ•ด์„œ ๋‚˜ํƒ€๋‚˜๊ธฐ ๋•Œ

www.acmicpc.net

ํ’€์ด

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class ๊ทธ๋ฃน๋‹จ์–ด์ฒด์ปค {

    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    public static void main(String[] args) throws IOException{

        int count = 0;
        int N = Integer.parseInt(br.readLine());

        for (int i = 0; i < N; i++) {
            if (check() == true) {
                count++;
            }
        }
        System.out.println(count);
    }

    public static boolean check() throws IOException {
        boolean[] check = new boolean[26];
        int prev = 0;
        String str = br.readLine();

        for(int i = 0; i < str.length(); i++) {
            int now = str.charAt(i);	// i ๋ฒˆ์งธ ๋ฌธ์ž ์ €์žฅ (ํ˜„์žฌ ๋ฌธ์ž)


            // ์•ž์„  ๋ฌธ์ž์™€ i ๋ฒˆ์งธ ๋ฌธ์ž๊ฐ€ ๊ฐ™์ง€ ์•Š๋‹ค๋ฉด?
            if (prev != now) {

                // ํ•ด๋‹น ๋ฌธ์ž๊ฐ€ ์ฒ˜์Œ ๋‚˜์˜ค๋Š” ๊ฒฝ์šฐ (false ์ธ ๊ฒฝ์šฐ)
                if ( check[now - 'a'] == false ) {
                    check[now - 'a'] = true;		// true ๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค
                    prev = now;					// ๋‹ค์Œ ํ„ด์„ ์œ„ํ•ด prev ๋„ ๋ฐ”๊ฟ”์ค€๋‹ค
                }

                // ํ•ด๋‹น ๋ฌธ์ž๊ฐ€ ์ด๋ฏธ ๋‚˜์˜จ ์ ์ด ์žˆ๋Š” ๊ฒฝ์šฐ (๊ทธ๋ฃน๋‹จ์–ด๊ฐ€ ์•„๋‹ˆ๊ฒŒ ๋จ)
                else {
                    return false;	//ํ•จ์ˆ˜ ์ข…๋ฃŒ
                }
            }


            // ์•ž์„  ๋ฌธ์ž์™€ i ๋ฒˆ์งธ ๋ฌธ์ž๊ฐ€ ๊ฐ™๋‹ค๋ฉด? (์—ฐ์†๋œ ๋ฌธ์ž)
            // else ๋ฌธ์€ ์—†์–ด๋„ ๋จ
            else {
                continue;
            }
        }
        return true;
    }
}