본문 바로가기

Algorithm/Algospot

[algospot]URI

문제

https://algospot.com/judge/problem/read/URI

URI는 인터넷에서 사용되는 자원을 구분하는데 사용되는 문자열이다.

URI를 전송할 때 퍼센트 인코딩을 가지고 우리는 특수문자를 피할 수 있다. 퍼센트 인코딩은 아스키 문자의 두자리 16진수 앞에 %를 붙여서 표현한다.

여기서 문제~ 이 작업을 반대로하는 프로그램을 작성하라~

 

입력

1건 이상 100건 이하의  최대 80글자의  암호화된 문자열

 

출력

복호화된 URI.

 

풀이

입력받은 URI문자열에 대해서 %xX문자를 특수문자로 치환한다.

 

package com.tutorial;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class URI {
    public static void main(String[] args)throws Exception {
        String[] encList = {
            "%20",
            "%21",
            "%24",
            "%25",
            "%28",
            "%29",
            "%2a"
        };
        String[] charList = {
            " ",
            "!",
            "$",
            "%",
            "(",
            ")",
            "*"
        };
        BufferedReader br = new BufferedReader(new InputStreamReader(System. in));
        int cnt = Integer.parseInt(br.readLine());
        while (cnt -- > 0) {
            String a = br.readLine();
            char[] b = a.toCharArray();
            Boolean temp = false;
            String result = "";
            for (int i = 0, len = b.length; i < len;) {
                if (b[i] == '%' && (i + 2) < len) {
                    temp = false;
                    for (int j = 0; j < encList.length; j ++) {
                        if (encList[j].equals(b[i] + "" + b[i + 1] + "" + b[i + 2])) {
                            result += charList[j];
                            temp = true;
                        }
                    }
                    if (temp) {
                        i += 3;
                    } else 
                        result += b[i ++];
                    
                } else {
                    result += b[i ++];
                }
            }
            System.out.println(result);
        }
    }
}

 

'Algorithm > Algospot' 카테고리의 다른 글

[algospot]HAMMINGCODE  (0) 2014.12.15
[algospot]WEIRD  (0) 2014.12.15
[algospot]XHAENEUNG  (0) 2014.12.11
[algospot]HOTSUMMER  (0) 2014.12.11
[algospot]CONVERT  (0) 2014.12.11