POJ-1782 Run Length Encoding-相同字符个数
2015-08-20 16:05
246 查看
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
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 4331 | Accepted: 1406 |
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
#include<stdio.h> #include<string.h> int main()//空格也算字符 { char str[200]; int i,j,cnt; while(gets(str)) { if (!str[0]) { putchar(10); continue; } cnt=1; for(i=0; str[i]; i++) { if (str[i] == str[i+1]) { for ( j = i ; str[i] == str[i+1] && (i-j) < 8 ; i++ ); printf("%d%c", i-j+1, str[i]); } else { printf("1"); for(j=i; str[j]; j++) { if(str[j]!=str[j+1]) { if(str[j]=='1') printf("1"); printf("%c",str[j]); } else break; } printf("1"); i=j-1; } } printf("\n"); memset(str, 0, sizeof(str)); } return 0; }
相关文章推荐
- java异常处理机制
- 树的度
- Struts2 action的单例与多例
- 开发工具
- leetcode - Minimum Path Sum
- java中对象引用,特别作为参数时候注意事项
- FPGA奇数分频
- appium向右滑动
- 牛客OJ——[编程题]A+B和C__如何输入多组测试数据(测试OK)
- oracle移植至mysql相关sql语句
- 让自己的scrollview中的button 也能有点击效果
- vsphere---vmotion
- poj1580 String Matching (枚举,gcd)
- 大数据中数据挖掘技术的挑战
- Cordova 开发属于自己的插件(plugin)
- Android TextView 状态切换时变化文字颜色
- iOS 8 widget icon is missing for first time after installation (需要翻墙)
- Mirth连接Sybase数据库
- DataInputStream和DataOutputStream类
- 只有IE能识别的代码