POJ1782:Run Length Encoding

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

题意:给定一个串要求按照以下的方式压缩。

1.对于连续重复子串的前面写上重复次数,然后加上重复的字符,如果超过9个的话先输出9个,后面的重复第一步。

2.如果不重复的,找出最长的不重复的子串,在子串的两边加上1,并且把该串中的所有的1用两个1代替,即11

 

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int main()
{
    char str[10000];
    int len,i,j,cnt,s,e;
    while(gets(str))
    {
        len = strlen(str);
        i = s = e = 0;
        while(1)
        {
            cnt = 1;
            while(str[i] == str[i+cnt] && cnt<10 && i<len)
            cnt++;
            if(i == len)
            {
                if(e)
                printf("1");
                break;
            }
            else if(cnt!=1)
            {
                s = 0;
                if(e)
                {
                    printf("1");
                    e = 0;
                }
                if(cnt<10)
                printf("%d%c",cnt,str[i]);
                else
                printf("%d%c",--cnt,str[i]);
            }
            else
            {
                e = 1;
                if(!s)
                {
                    printf("1");
                    s = 1;
                }
                printf("%c",str[i]);
                if(str[i] == '1')
                printf("%c",str[i]);
            }
            i+=cnt;
        }
        printf("\n");
    }

    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值