zoukankan      html  css  js  c++  java
  • poj 1782 Run Length Encoding

    题目链接

    Run Length Encoding
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 5006   Accepted: 1560

    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)2<=连续字符长度<=9 输出个数+字符;>9把9个先输出,后面的截断再判断
       (2)没有相同字符的子序,在第一个和最后一个字符前面输出1,如果字符是1,就用1转意,所以1个1就是11;

    AC代码:

    #include<cstring>
    #include<cstdio>
    using namespace std;
    const int N=1010;
    char s[N];
    int main(){
        #ifdef ONLINE_JUDGE
        #else
            freopen("in.txt","r",stdin);
        #endif // ONLINE_JUDGE
        while(gets(s)!=NULL){
            int len=strlen(s);
            int i=0,j;
            while(i<len){
                    
                int k=1;
                while(s[i]==s[i+1]&&i<len){
                    k++;
                    i++;
                    if(k>=9) break;
                }
                if(k>1) {
                    printf("%d%c",k,s[i-1]);
                    i++;
                }
                if(i>=len) break;
                if(s[i]!=s[i+1]){
                    printf("1");
                    while(s[i]!=s[i+1]&&i<len){
                        if(s[i]=='1') printf("1");
                        printf("%c",s[i]);
                        i++;
                    }
                    printf("1");
                }
            }
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    linux常用命令
    ANAFI EXTENOED无人机(1)环境配置和基础开发
    无人机自主降落
    ROS开发(1)安装环境
    bebop无人机(1)环境配置和基础开发
    YOLO标注软件
    Python2与Python3之间切换
    python实现IOU计算
    读取多个(海康大华)网络摄像头的视频流 (使用opencv-python),解决实时读取延迟问题
    如何到外面的世界看看
  • 原文地址:https://www.cnblogs.com/kun-/p/9746996.html
Copyright © 2011-2022 走看看