문제 출처 - 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 Character | Encoded 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
출력
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로 인코딩 되어야 정답이 되는 문제였다.