본문 바로가기
개발/알고리즘문제

알고스팟-URIDecoding

by 사과잼빵 2015. 7. 23.

문제 출처  - Algospot ICPC Seoul Regional Warmup 2008

- 문제

URI (Uniform Resource Identifier) is a compact string used to identify or name resources on the Internet. Some examples of URI are below:

  • http://icpc.baylor.edu.cn/
  • mailto:foo@bar.org
  • ftp://127.0.0.1/pub/linux
  • readme.txt

When transmitting *URI*s through the Internet, we escape some special characters in *URI*s with percent-encoding. Percent-encoding encodes an ASCII character into a percent sign ("%") followed by a two-digit Hexadecimal representation of the ASCII number. The other characters are not touched in the encoding process. The following table shows the special characters and their corresponding encodings:

Special CharacterEncoded String
" "%20
"!"%21
"$"%24
"%"%25
"("%28
")"%29
"*"%2a

Note that the quotes are for clarity only.

Write a program which reverses this process.

입력

The first line of the input file will contain the number of test cases C (1C100). The following Clines will each contain a test case — which is the percent-encoded URI. Their length will be at most 80.

출력

Print one line for each test cases — the decoded original URI.


- 풀이


#include <iostream>

using namespace std;


void Change(char* input, char* result); //문자열 인코딩 함수

bool Check(char* input);  //문자열에 인코딩해야할 문자열이 있는지 검사



int main(void)

{

char* pStringArr[80];

char* pResultArr[80];

int iTestCase = 0;

cin>>iTestCase;

for(int i = 0; i<iTestCase; ++i)

{

pStringArr[i] = (char*)malloc(sizeof(char) * 80);

pResultArr[i] = (char*)malloc(sizeof(char) * 80);

cin>>pStringArr[i];

 Change(pStringArr[i], pResultArr[i]);

 cout<<pResultArr[i]<<endl;

}

return 0;


}


void Change(char* input, char* result)

{

int iResultIdx = 0;

for(int j = 0; j<80; ++j)

{

if(input[j] == 0)

{

result[iResultIdx] = 0;

break;

}

else if(input[j] == '%')

{

if(input[j+2] == '0')

{

result[iResultIdx] = ' ';

j+=2;

}

else if(input[j+2] == '1')

{

result[iResultIdx] = '!';

j+=2;

}

else if(input[j+2] == '4')

{

result[iResultIdx] = '$';

j+=2;

}

else if(input[j+2] == '5')

{

result[iResultIdx] = '%';

j+=2;

}

else if(input[j+2] == '8')

{

result[iResultIdx] = '(';

j+=2;

}

else if(input[j+2] == '9')

{

result[iResultIdx] = ')';

j+=2;

}

else if(input[j+2] == 'a')

{

result[iResultIdx] = '*';

j+=2;

}

else

{

result[iResultIdx] = input[j];

}

}

else

{

result[iResultIdx] = input[j];

}

iResultIdx++;

}

}



bool Check(char* input)

{

for(int i = 0; i< 80; ++i)

{

if(input[i] == '%')

{

if(input[i+1] == '2')

{

if(input[i+2] == '0' || input[i+2] == '1' || input[i+2]=='4' || input[i+2]=='5' || input[i+2] == '8' || input[i+2] == '9' || input[i+2] == 'a') 

return false;

else

return true;

}

else

return true;

}

}


return true;

}


처음 풀이 할때는 인코딩해야될 조건을 "입력 문자열에  %2가 있을시"로 두어서 오답 판정을 받았었다. 이 경우 %23 같은 입력 문자열에도 인코딩 해야하는 것으로 조건문에 걸려서 오답이 발생하게 된다.  그리고 %2525의 경우 앞의 %25가 %로 인코딩되어 %25이므로 최종적으로 %2525 = %로 출력되어야 하는지 착각하였는데 인코딩된 %와 처음 입력된 문자열의 %는 서로 다른 의미를 가지고 있기 때문에 %2525는 %25로 인코딩 되어야 정답이 되는 문제였다.



'개발 > 알고리즘문제' 카테고리의 다른 글

다음 입사문제  (0) 2015.03.21