poj1782(Run Length Encoding)

来源:互联网 发布:淘宝刷单平台源码 编辑:程序博客网 时间:2024/05/19 12:27

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

AAAAAABCCCC12344

Sample Output

6A1B14C11123124
这个题目真是难懂啊,主要是英文难看懂,再加上题目的细节太多了,但是是个好题目,而且Google面试题目就有这个题目
希望自己再接再厉
题目分析:单独输入一个1,得到1111
输入234得到12341(就是前后都有一个1)
输入1231得到11123111
再注意一下输入换行符的时候也是输出换行符,还有空格这些都要注意的
#include<stdio.h>#include<string.h>char a[1100];bool tag=false;int i;void print(int n,char t){    if(n==0)        return ;    else if(n==1){        if(tag==false){            printf("1");            tag=true;        }        if(t=='1'){            printf("11");        }        else{            printf("%c",t);        }    }    else{        if(tag==true){            printf("1");            tag=false;        }        printf("%d%c",n,t);    }    return ;}int main(){    while(gets(a))    {        if(a[0]=='\0'){            puts("");            continue;        }        tag=false;        int length=strlen(a);        int sum=1;        char temp=a[0];        for(i=1;i<length;i++){            if(temp==a[i]){                sum++;                if(sum==9){                    if(tag==true){                        printf("1");                        tag=false;                    }                    printf("9%c",temp);                    sum=0;                }            }            else{                print(sum,temp);                sum=1;                temp=a[i];            }        }        print(sum,temp);        if(tag==true)            printf("1");        puts("");    }    return 0;}


0 0