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.

输入样例

AAAAAABCCCC
12344




输出样例


6A1B14C


11123124

现在说下题目要求,其实就是两种策略,

1.对于重复的字符,我们应该输出的是重复个数和重复的字符,

2.而对于不重复的字符我们应该是使用1开始,接下来是不重复的字符,最后是用1结尾。

3.对于字符1,我们使用两个1字符代替

好了,我们现在说说求解方法,事先说明本方法是看了大牛的,我只是解析一下,自己弄了好半天,也没出啥结果,惭愧啊

1.首先输如字符串,从i=0

2.开始遍历字符串,  比较inp[i]和inp[i+1]是否相等,如果相等的话,则继续遍历索引位置i++;计数器cnt++

3.比较cnt是否大于1,如果大于1吗,也就是前面的是相同的字符串,如果当前state==SINGLE,也就是说前面是不同的字符串,我们还需要在输出字符串中添加‘1’表示结束

4.然后再输出字符串outp中添加cnt和inp[i],表示“3A”,同时state=MANY;将state设置成没有不同字符串的标记

5.如果cnt不大于1,也就是不同的字符,如果当前状态是MANY,也就是刚刚开始有不同的字符,在输出字符串outp中添加开始标记'1',如果不同的字符是‘1’,则再添加一个‘1’

6.否则的话,就将当前字符添加在outp当中,将state设置成SINGLE

7.i++转而执行步骤3

#include "stdafx.h"     
#include <stdio.h>
void main()
{
char inp[20], outp[20];
enum{MANY, SINGLE} state;
while(1)
{
printf("input:");
gets(inp);
int i = 0, j = 0;
state = MANY;
while(inp[i]!='\0')
{
int cnt = 1;
// find successive identical characters,
//寻找连续的字符串
while((inp[i+1]!='\0') && (inp[i]==inp[i+1]))
{
i ++;
cnt ++;
}
//
if(cnt>1) // there exist successive identical characters
{
if(state == SINGLE) // if the previous state is SINGLE, '1' should be added to terminate
outp[j++] = '1';
outp[j++] = cnt+'0'; // add "cnt"+"symbol"
outp[j++] = inp[i++]; // i pointing to a new data
state = MANY; // state transformed to MANY
}
else // no successive identical characters
{
if(state == MANY) // '1' should be added to start
outp[j++] = '1';
if(inp[i] == '1') // if inp[i] == '1', then an extra '1' should be added
outp[j++] = '1';
outp[j++] = inp[i++]; // i pointing to a new data
state = SINGLE; // state transformed to SINGLE
}
}
if(state == SINGLE)
outp[j++] = '1';
outp[j] = '\0';
printf("output: %s\n", outp);
}
}