Sicily 1639. Run Length Encoding
2016-01-08 18:13
507 查看
Time Limit: 1 secs, Memory Limit: 32 MB
Description
Run-length encoding is a simple compression technique which compresses strings of letters by replacing repeated consecutive letters (called runs) by the number of occurrences of the letter, followed by that letter. For example, AAAABBBCDDE compresses to 4A3BC2DE. The number 1 may be omitted in runs consisting of a single letter, as with letters ‘C’ and ‘E’ in the previous example.
Any string consisting of uppercase letters where each letter is optionally preceded by a positive integer is called a properly encoded string.
Input
Input may contain many cases, each is a properly encoded string in one line.
Each string will contain between 0 and 50 characters (‘0’-‘9’, ‘A’-‘Z’), inclusive.
Each string will be a properly encoded string: all numbers contained will be positive integers with no leading zeros and each number will precede a letter.
Output
For each case, output the decoded string in one line. If the decoded string would be more than 50 characters long, return “TOO LONG” (without the quotes).
Sample Input
4A3BC2DE
1A1B1C1D1E
1A3A5A4BCCCC
50A
21Z13S9A8M
Sample Output
AAAABBBCDDE
ABCDE
AAAAAAAAABBBBCCCC
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
TOO LONG
<( ̄︶ ̄)> 别想得太复杂,每一步都想清楚,简单才是美。Just do it!
Description
Run-length encoding is a simple compression technique which compresses strings of letters by replacing repeated consecutive letters (called runs) by the number of occurrences of the letter, followed by that letter. For example, AAAABBBCDDE compresses to 4A3BC2DE. The number 1 may be omitted in runs consisting of a single letter, as with letters ‘C’ and ‘E’ in the previous example.
Any string consisting of uppercase letters where each letter is optionally preceded by a positive integer is called a properly encoded string.
Input
Input may contain many cases, each is a properly encoded string in one line.
Each string will contain between 0 and 50 characters (‘0’-‘9’, ‘A’-‘Z’), inclusive.
Each string will be a properly encoded string: all numbers contained will be positive integers with no leading zeros and each number will precede a letter.
Output
For each case, output the decoded string in one line. If the decoded string would be more than 50 characters long, return “TOO LONG” (without the quotes).
Sample Input
4A3BC2DE
1A1B1C1D1E
1A3A5A4BCCCC
50A
21Z13S9A8M
Sample Output
AAAABBBCDDE
ABCDE
AAAAAAAAABBBBCCCC
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
TOO LONG
<( ̄︶ ̄)> 别想得太复杂,每一步都想清楚,简单才是美。Just do it!
#include <iostream> #include <string> using namespace std; int main() { int num; string str, strp; while (getline(cin, str)) { strp.clear(); /// 循环更新 for (int i = 0; i < str.size(); i++) { if (str[i] >= '0' && str[i] <= '9') { num = 0; a1f5 /// 注意num的范围,要是不加限制有可能会很大,甚至超过int的范围 while (str[i] >= '0' && str[i] <= '9' && num <= 50) { num = 10 * num + str[i] - 48; i++; } strp += string(num, str[i]); } else strp += str[i]; } /// 输出 if (strp.size() <= 50) cout << strp << endl; else cout << "TOO LONG" << endl; } return 0; }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- ASP编码必备的8条原则
- C++联合体转换成C#结构的实现方法
- XML指南——XML编码
- C#中字符串编码处理
- ExtJS中文乱码之GBK格式编码解决方案及代码
- 程序员趣味读物 谈谈Unicode编码
- 文本文件编码方式区别
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题