您的位置:首页 > 其它

ZOJ-2240 Run Length Encoding

2013-04-15 13:06 246 查看
Run Length Encoding

Time Limit: 2 Seconds      MemoryLimit: 65536 KB

Your task is towrite a program that performs a simple form of run-length encoding, asdescribed by the rules below.
Any sequence ofbetween 2 to 9 identical characters is encoded by two characters. The firstcharacter is the length of the sequence, represented by one of thecharacters 2 through 9. The second character is the value of the repeatedcharacter. A
sequence of more than 9 identical characters is dealt with byfirst encoding 9 characters, then the remaining ones.
Any sequence ofcharacters that does not contain consecutive repetitions of any characters isrepresented by a 1 characterfollowed by the sequence of characters, terminated with another 1. If a 1 appears as part of the sequence, it is escaped
with a 1, thus two 1 characters are output.
InputSpecification
The input consistsof letters (both upper- and lower-case), digits, spaces, and punctuation. Everyline is terminated with a newline character and no other characters appear inthe input.
OutputSpecification
Each line in theinput is encoded separately as described above. The newline at the end of eachline is not encoded, but is passed directly to the output.
Sample Input
AAAAAABCCCC
12344
Sample Output
6A1B14C
11123124

#include <stdio.h>
#include <string.h>

int main()
{
char str[1000];
int i, len, j, t;
char count[3000][2];
while(gets(str) != NULL) {
len = strlen(str);
for(i = 0, t = 0; i < len;) {
j = i;
while(j < len && str[i] == str[j] && j-i < 9) {
j++;
}
count[t][0] = str[i];
count[t++][1] = j-i+'0';
i = j;
}
for(i = 0; i < t;) {
if(count[i][1] > '1') {
printf("%c%c", count[i][1], count[i][0]);
i++;
} else {
j = i;
printf("1");
while(j < t && count[j][1] == '1') {
printf("%c", count[j][0]);
if(count[j][0] == '1') {
printf("1");
}
j++;
}
printf("1");
i = j;
}
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM解题报告 模拟