boj 36
7495 단어 BO
For this problem, you will write a program that encodes a block of data using a very simple RLE algorithm. A run is encoded using a two byte sequence. The first byte of the sequence contains the count, and the second contains the value to repeat. The count is encoded using an 8 bit value with the high order bit set to 1. The remaining 7 bits represent the count-3. This gives a maximum run count of 130 per 2 byte sequence. (This implies that the minimum run count is 3). Bytes that are not part of a run are encoded as-is with a prefix byte indicating the count of bytes in the non-run minus 1, 0 through 127, representing a range of 1 - 128 (the high order bit will always be 0 in the case of nonrun data).
If a run contains more than 130 bytes, then it must be encoded using multiple sequences, the first of which will always be a run of 130. All runs of 3 or more must be encoded as a run. If a non-run contains more than 128 bytes, then multiple non-run sequences must be used. For example, a run of 262 would be encoded as two runs of 130 followed by a non-run of 2.
Input
The first line of input contains a single integer P, (1 <= P <= 1000), which is the number of data sets that follow. Each data set consists of multiple lines. The first line contains two (2) decimal integer values: the problem number, followed by a space, followed by the number of bytes B, (1 <= B <= 5000), to encode. The remaining line(s) contain(s) the data to be encoded. Each line of data to encode will contain 80 hexadecimal digits (except the last line, which may contain less). 2 hexadecimal digits are used to represent each byte. Hexadecimal digits are: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F.
Output
For each data set, there are multiple lines of output. The first line contains a decimal integer giving the data set number followed by a single space, followed by a decimal integer giving the total number of encoded bytes. The remaining lines contain the encoded data each with 80 hexadecimal digits, except the last, which may contain less.
Sample Input
4
1 1
07
2 5
F4A5A5A5A5
3 44
0000000000000000FFFFFF66665A5A5A5A5A71727374758008011011135555555555555501020399
777777CC
4 40
68686868686868686868686868686868686868686868686868686868686868686868686868686868
Sample Output
1 2
0007
2 4
00F481A5
3 32
850080FF016666825A0A717273747580080110111384550301020399807700CC
4 2
A568
예전에 했던 문제, 문자열 처리, 세심해야 돼, 코드가 길어...
코드:
#include<iostream>
using namespace std;
char run[10000];
char data[10000];
char print[10000];
int info[10000];
int main()
{
int p,n,len;
cin>>p;
while(p--)
{
cin>>n>>len;
len=2*len;
int i=0;
while(i!=len)
{
char ch;
ch=cin.get();
if(ch!=10)
{
run[i]=ch;
i++;
}
}
for(i=0;i<len;i++)
info[i]=1;
int pos=0,posi=0,cur=0;
i=0;
run[len]=run[len+1]='G';
while(i<len)
{
if(run[i]==run[i+2]&&run[i+1]==run[i+3])
{
info[posi]++;
i=i+2;
}
else
{
data[pos]=run[i];
data[pos+1]=run[i+1];
pos=pos+2;
posi++;
data[pos]=run[i+2];
data[pos+1]=run[i+3];
i=i+2;
}
}
posi--;
for(i=0;i<=posi;i++)
{
if(info[i]>=3)
{
while(info[i]>130)
{
print[cur++]='F';
print[cur++]='F';
print[cur++]=data[2*i];
print[cur++]=data[2*i+1];
info[i]=info[i]-130;
}
if(info[i]>=3)
{
int numh,numl;
info[i]=info[i]-3;
numl=info[i]%16;
numh=info[i]/16+8;
if(numh>9) print[cur++]=numh-10+'A';
else print[cur++]=numh+'0';
if(numl>9) print[cur++]=numl-10+'A';
else print[cur++]=numl+'0';
print[cur++]=data[2*i];
print[cur++]=data[2*i+1];
}
else
{
int j=i,sum=0;
while(info[i]<3&&i<=posi)
{
sum+=info[i];
i++;
}
if(sum<=128)
{
int p=i,numh,numl;
numl=(sum-1)%16;
numh=(sum-1)/16;
print[cur]=numh+'0';
if(numl>9) print[cur++]=numl-10+'A';
else print[cur++]=numl+'0';
for(i=j;i<p;i++)
{
while(info[i]!=0)
{
print[cur++]=data[2*i];
print[cur++]=data[2*i+1];
info[i]--;
}
}
i--;
}
else
{
int p=i,counter=0;
print[cur++]='7';
print[cur++]='F';
for(i=j;i<p;i++)
{
while(info[i]!=0&&counter!=128)
{
print[cur++]=data[2*i];
print[cur++]=data[2*i+1];
counter++;
info[i]--;
}
if(counter==128)break;
}
}
}
}
else
{
int j=i,sum=0;
while(info[i]<3&&i<=posi)
{
sum+=info[i];
i++;
}
if(sum<=128)
{
int p=i,numh,numl;
numl=(sum-1)%16;
numh=(sum-1)/16;
print[cur++]=numh+'0';
if(numl>9) print[cur++]=numl-10+'A';
else print[cur++]=numl+'0';
for(i=j;i<p;i++)
{
while(info[i]!=0)
{
print[cur++]=data[2*i];
print[cur++]=data[2*i+1];
info[i]--;
}
}
i--;
}
else
{
int p=i,counter=0;
print[cur++]='7';
print[cur++]='F';
for(i=j;i<p;i++)
{
while(info[i]!=0&&counter!=128)
{
print[cur++]=data[2*i];
print[cur++]=data[2*i+1];
counter++;
info[i]--;
}
if(counter==128)break;
}
}
}
}
print[cur]='\0';
int length=strlen(print);
cout<<n<<' '<<length/2<<endl;
for(i=0;i<length;i++)
{
cout<<print[i];
if(i%80==79)
cout<<endl;
}
if(i%80!=0)cout<<endl;
}
//system("pause");
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
cowboy의 루트 방식직접 부호 붙이기 route_helper.erl catch_all_handler에서 catch 로 처리all_handler에서 시작된 모든 url 요청 catch_all_handler.erl test_handler....
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.