POJ 1782 Run Length Encoding

Run Length Encoding

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 4330   Accepted: 1405

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

PS 空格和空行也是元素

AC代码:

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int main(){
    char s[1000];
    while(gets(s)){
        for(int i=0;s[i];++i){
            if(s[i+1]!=s[i]&&s[i]){
                printf("1");
                while(s[i+1]!=s[i]&&s[i]){
                    putchar(s[i]);
                    if(s[i]=='1')
                        putchar(s[i]);
                    i++;
                }
                i--;
                putchar('1');
            }
            else if(s[i+1]==s[i]&&s[i]){
                int cns=2;
                i++;
                while(s[i+1]==s[i]&&s[i]){
                    cns++;
                    if(cns>9){
                        cns=9;
                        break;
                    }
                    i++;
                }
                putchar(cns+'0');
                putchar(s[i]);
            }
        }
        putchar('\n');
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-11 21:02:15

POJ 1782 Run Length Encoding的相关文章

Count and Say (Array Length Encoding) -- LeetCode

The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as "one 2, then one 1" or 1211. Given an

POJ 1579-Function Run Fun(记忆化搜索)

Function Run Fun Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16503   Accepted: 8514 Description We all love recursion! Don't we? Consider a three-parameter recursive function w(a, b, c): if a <= 0 or b <= 0 or c <= 0, then w(a, b

POJ 1379 Run Away

题意:有n个陷阱,在X,Y范围内要求出一个点使得这个点到陷阱的最小距离最大. 思路:模拟退火,随机撒入40个点,然后模拟退火随机化移动. (这题poj坑爹,加了srand(time(NULL))不能交G++,不加srand(time(NULL))又会WA,交了C++不能用acos(-1),只能用3.1415926代替,真是坑爹.) #include<algorithm> #include<cstdio> #include<cmath> #include<cstri

poj 1379 Run Away 模拟退火 难度:1

Run Away Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6482   Accepted: 1993 Description One of the traps we will encounter in the Pyramid is located in the Large Room. A lot of small holes are drilled into the floor. They look complet

POJ 1579-Function Run Fun(内存搜索)

Function Run Fun Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16503   Accepted: 8514 Description We all love recursion! Don't we? Consider a three-parameter recursive function w(a, b, c): if a <= 0 or b <= 0 or c <= 0, then w(a, b

POJ 1379 Run Away 模拟退火

一开始写了一发很快的,发现一会能过一会不能,貌似有点悬,毕竟是随机算法. 后来重写了一发迭代5遍的,基本上把把AC了= = 模拟退火果然是一种不是很靠谱的算法. #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <cstdlib> #include <ctime> using namespace std; const

POJ 1379 Run Away 【基础模拟退火】

题意:找出一点,距离所有所有点的最短距离最大 二维平面内模拟退火即可,同样这题用最小圆覆盖也是可以的. Source Code: //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <fstream> #include <cstring> #include <cm

POJ 1009 Edge Detection

Edge Detection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22153   Accepted: 5201 Description IONU Satellite Imaging, Inc. records and stores very large images using run length encoding. You are to write a program that reads a compre

POJ刷题

这篇文章会持续更新, 记录我所有AC的POJ题目. PS:我所有的POJ代码都存在我的github上. 1000 A+B 水题不说. 1001 Exponentiation 求一个数的n次方,用高精度,注意细节. 1002 487-3279 题目描述:设计程序,按照功能机上的9键键位把字母电话号码转化成数字电话号码,并将电话号码格式化(原本的电话号码格式里可能出现无数个短横线-(=_=#)) 对于给定输入,输出有重复的电话号码,并给出重复的次数. 输入格式:第一行一整数n,表示电话号码的数量,接