POJ--1782 Run Length Encoding

23185 단어
Run Length Encoding
Time Limit: 1000MS
 
Memory Limit: 30000K
Total Submissions: 4277
 
Accepted: 1385
Description
Your task is to write a program that performs a simple form of run-length encoding, as described by the rules below. 
Any sequence of between 2 to 9 identical characters is encoded by two characters. The first character is the length of the sequence, represented by one of the characters 2 through 9. The second character is the value of the repeated character. A sequence of more than 9 identical characters is dealt with by first encoding 9 characters, then the remaining ones. 
Any sequence of characters that does not contain consecutive repetitions of any characters is represented by a 1 character followed 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. 
Input
The input consists of letters (both upper- and lower-case), digits, spaces, and punctuation. Every line is terminated with a newline character and no other characters appear in the input.
Output
Each line in the input is encoded separately as described above. The newline at the end of each line is not encoded, but is passed directly to the output.
Sample Input
AAAAAABCCCC
12344

Sample Output
6A1B14C
11123124

Source
Ulm Local 2004
제목 자체가 어렵지 않고 줄바꿈을 입력할 때 줄바꿈을 출력해야 하는 구덩이가 있다.모든 1은 11로 바뀌고, 한 문자의 수가 9개를 넘으면, 출력 9와 이 문자를 다시 계산합니다.
공백, 문장부호는 모두 문자로 처리해야 한다.
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char s[100000];
int main()
{
	while (gets(s))
	{
		char temp;
		int i = 0;
		temp = s[0];
		int sum = 0;
		if (strlen(s) >=1)
		{
			for (;;)
			{
				if (temp == s[i + 1])// 
				{
					for (int j = i;; j++)
					{
						if (sum >= 9)
						{
							i = j;
							break;
						}
						if (temp == s[j])
						{
							sum++;
						}
						else
						{
							i = j;
							break;
						}
					}
					printf("%d%c", sum, temp);
					temp = s[i];
					sum = 0;
				}
				if (temp != s[i + 1])// 
				{
					printf("1");
					for (int j = i;; j++)
					{
						if (temp != s[j + 1])
						{
							if (temp == '1')
								printf("1");
							printf("%c", temp);
							temp = s[j + 1];
						}
						else
						{
							i = j;
							printf("1");
							break;
						}
					}
				}
				if (s[i] == '\0')// '\0' , 
					break;
			}
		}
		printf("
"); memset(s, 0, sizeof(s)); } return 0; }

좋은 웹페이지 즐겨찾기