Algorithm๐Ÿฅ‡

11655.ROT13

hae02y 2023. 10. 17. 14:00
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

ROT13์€ ์นด์ด์‚ฌ๋ฅด ์•”ํ˜ธ์˜ ์ผ์ข…์œผ๋กœ ์˜์–ด ์•ŒํŒŒ๋ฒณ์„ 13๊ธ€์ž์”ฉ ๋ฐ€์–ด์„œ ๋งŒ๋“ ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, "Baekjoon Online Judge"๋ฅผ ROT13์œผ๋กœ ์•”ํ˜ธํ™”ํ•˜๋ฉด "Onrxwbba Bayvar Whqtr"๊ฐ€ ๋œ๋‹ค. ROT13์œผ๋กœ ์•”ํ˜ธํ™”ํ•œ ๋‚ด์šฉ์„ ์›๋ž˜ ๋‚ด์šฉ์œผ๋กœ ๋ฐ”๊พธ๋ ค๋ฉด ์•”ํ˜ธํ™”ํ•œ ๋ฌธ์ž์—ด์„ ๋‹ค์‹œ ROT13ํ•˜๋ฉด ๋œ๋‹ค. ์•ž์—์„œ ์•”ํ˜ธํ™”ํ•œ ๋ฌธ์ž์—ด "Onrxwbba Bayvar Whqtr"์— ๋‹ค์‹œ ROT13์„ ์ ์šฉํ•˜๋ฉด "Baekjoon Online Judge"๊ฐ€ ๋œ๋‹ค.

ROT13์€ ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž์—๋งŒ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์•ŒํŒŒ๋ฒณ์ด ์•„๋‹Œ ๊ธ€์ž๋Š” ์›๋ž˜ ๊ธ€์ž ๊ทธ๋Œ€๋กœ ๋‚จ์•„ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, "One is 1"์„ ROT13์œผ๋กœ ์•”ํ˜ธํ™”ํ•˜๋ฉด "Bar vf 1"์ด ๋œ๋‹ค.

๋ฌธ์ž์—ด์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, "ROT13"์œผ๋กœ ์•”ํ˜ธํ™”ํ•œ ๋‹ค์Œ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž, ์†Œ๋ฌธ์ž, ๊ณต๋ฐฑ, ์ˆซ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด S๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. S์˜ ๊ธธ์ด๋Š” 100์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— S๋ฅผ ROT13์œผ๋กœ ์•”ํ˜ธํ™”ํ•œ ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•œ๋‹ค.

์‹œ๊ฐ„ ์ œํ•œ ๋ฉ”๋ชจ๋ฆฌ ์ œํ•œ ์ œ์ถœ ์ •๋‹ต ๋งžํžŒ ์‚ฌ๋žŒ ์ •๋‹ต ๋น„์œจ
1 ์ดˆ 256 MB 23573 14155 11914 60.674%

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

ํ’€์ด

ROT 13 ๋‚˜๋ฌด์œ„ํ‚ค
ROT 13์€ ๋‹จ์ˆœํ•œ ์นด์ด์‚ฌ๋ฅด ์•”ํ˜ธ์˜ ์ผ์ข…์œผ๋กœ ์˜์–ด ์•ŒํŒŒ๋ฒณ์„ 13๊ธ€์ž์”ฉ ๋ฐ€์–ด์„œ ๋งŒ๋“ ๋‹ค.

๊ณ„์‚ฐํ•ด์•ผํ•  ๋ถ€๋ถ„์€ ์˜๋ฌธ์ž๊ฐ€ ๋Œ€๋ฌธ์ž, ์†Œ๋ฌธ์ž ๋ฒ”์œ„๋ฅผ ๋„˜์–ด๊ฐˆ๋•Œ์ด๋‹ค. ์˜๋ฌธ์ž์˜ ์•„์Šคํ‚ค์ฝ”๋“œ๊ฐ€ ๋Œ€๋ฌธ์ž๋Š” 65 ~ 90 ๊นŒ์ง€, ์†Œ๋ฌธ์ž๋Š” 97 ~ 122 ๊นŒ์ง€์ธ๋ฐ, 13์„ ๋”ํ•˜๊ฒŒ๋˜๋ฉด ๋ฒ”์œ„๋ฅผ ๋„˜์–ด๊ฐ„๋‹ค. ์ด๋ ‡๊ฒŒ ๋„˜์–ด๊ฐ€๋Š” ๊ฒฝ์šฐ์—๋Š” ์•ŒํŒŒ๋ฒณ ์ „์ฒด๊ฐฏ์ˆ˜ 26 ์„ ๋นผ์ฃผ๋ฉด ๋œ๋‹ค.

์ฝ”๋“œ

public class ROT13 {  
    public static void main(String[] args) throws IOException {  
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
        StringBuilder sb = new StringBuilder();  
        String string = br.readLine();  

        for(int i=0; i<string.length(); i++){  
            char c = string.charAt(i);  

            if(c>='a' && c<='z'){  
                if(c + 13 > 'z'){ //c + 13 ์ด ์†Œ๋ฌธ์ž ๋ฒ”์œ„๋ฅผ ๋„˜์–ด๊ฐ€๋Š” ๊ฒฝ์šฐ  
                    c = (char) (c - 13);  
                    sb.append(c);  
                }else{ //c + 13 ์ด ์†Œ๋ฌธ์ž ๋ฒ”์œ„๋ฅผ ๋„˜์–ด๊ฐ€์ง€ ์•Š๋Š”๊ฒฝ์šฐ ์ถœ๋ ฅ  
                    c = (char) (c + 13);  
                    sb.append(c);  
                }  

            }else if(c>='A' && c<='Z'){  
                if(c + 13 > 'Z'){ //c + 13 ์ด ๋Œ€๋ฌธ์ž ๋ฒ”์œ„๋ฅผ ๋„˜์–ด๊ฐ€๋Š” ๊ฒฝ์šฐ  
                    c = (char) (c - 13);  
                    sb.append(c);  
                }else{ //c + 13 ์ด ๋Œ€๋ฌธ์ž ๋ฒ”์œ„๋ฅผ ๋„˜์–ด๊ฐ€์ง€ ์•Š๋Š”๊ฒฝ์šฐ ์ถœ๋ ฅ  
                    c = (char) (c + 13);  
                    sb.append(c);  
                }  
            }else {  //์˜๋ฌธ์ž๊ฐ€ ์•„๋‹Œ๊ฒฝ์šฐ ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅ  
                sb.append(c);  
            }  
        }  
        System.out.println(sb);  
        br.close();  
    }  
}

๋‹ค๋ฅธ ๋ฐฉ๋ฒ•

import java.io.*; 
import java.util.*; 

public class Main { 
public static void main(String[] args) throws IOException { 
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    StringBuilder sb = new StringBuilder(); 
    String s = br.readLine(); 

    for (int i = 0; i < s.length(); i++) { 
        char c = s.charAt(i); 
        if (!Character.isLetter(c)) { 
        sb.append(c); 
        continue; 
        } 

        int n = (int)c + 13; 
        //๋Œ€๋ฌธ์ž์ผ ๋•Œ 90๋ณด๋‹ค ํฌ๋ฉด 26 ๋นผ๊ธฐ 
        if (Character.isUpperCase(c) && n > 90) { 
            n -= 26; } 
        //์†Œ๋ฌธ์ž์ผ ๋•Œ 122๋ณด๋‹ค ํฌ๋ฉด 26 ๋นผ๊ธฐ
        if (Character.isLowerCase(c) && n > 122) {
            n -= 26; 
        } 
        sb.append((char)n); 
        } 
    System.out.print(sb); 
    } 
}
๋ฐ˜์‘ํ˜•