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
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; }
相关文章推荐
- ZOJ 2240. Run Length Encoding
- POJ 1782/ ZOJ 2240 : Run Length Encoding - 字符串处理(模拟)
- POJ 1782 Run Length Encoding
- poj 1782 Run Length Encoding
- 行程长度压缩算法(Run Length Encoding)
- HOJ 1670 Run Length Encoding
- 2018_1_30_Run Length Encoding_string_STL_模拟
- POJ 1782 Run Length Encoding 笔记
- POJ1782:Run Length Encoding
- Run-Length Encoding
- Run Length Encoding
- 【FOJ 1360】 Run Length Encoding
- SDUT 2352 Run Length Encoding
- (字符串的模式模式匹配4.7.9)POJ 1782 Run Length Encoding(具有重复字符的字符串的处理方式)
- POJ-1782 Run Length Encoding-相同字符个数
- 1639. Run Length Encoding
- poj 1782 Run Length Encoding 自动机处理字符串
- Run-Length Encoding
- 1639. Run Length Encoding
- [转]行程编码(Run Length Encoding)和算术编码