POJ 1782 Run Length Encoding
2015-08-13 16:25
239 查看
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
Sample Output
Source
Ulm Local 2004
PS 空格和空行也是元素
AC代码:
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 4330 | Accepted: 1405 |
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; }
相关文章推荐
- hdu 1021 Fibonacci Again(矩阵连乘 || 循环节)
- 【web】mybatis入门详解,基础案例demo
- 传值还是传引用
- win8和win7下解决php5.3和5.4、5.5等不能加载php_curl.dll的终极解决办法
- 黑马程序员--java技术blog---第九篇:网络编程(2)
- Linux集群系列01-LVS_NAT模型配置
- WebService学习
- 挑选镇长问题
- 我是如何构建一个持续发展的项目
- 使用py2exe生成独立的exe文件
- linux学习笔记----文件处理命令--head= 显示文件前几行内容
- Find The Multiple(POJ--1426
- linux安装vmware tools 找不到kernel路径
- c&c++的若干条知识点汇总
- Ofbiz 入门介绍
- hdu 4445 Crazy Tank (暴力枚举)
- 将博客搬至CSDN
- Red and Black (BFS)poj 1979
- 黑客成长之路------十问(第6天)
- 整形数组合并_用 set