poj 1782 Run Length Encoding
来源:互联网 发布:java开发android应用 编辑:程序博客网 时间:2024/05/19 16:03
这道题目不难,但是有很多细节是需要注意的
1.读入一个带有空格的字符串string,可以采用string类中的getline方法
2.这道题目理解起来有些问题,这里做一些解释:理解题目出了问题 走了很多弯路!!!
run length encoding 变长编码,
要求 2- 9 个相同字符转化为 count + X 形式表示,
同时提取最大子串 前后加上1,亦即 1 str 1如果子串中含有1 需要 额外增加一个 1
3.最后一段子串的处理,以为这时没有后续字符了,操作时候有些问题,调了很久,特别是字符串末尾的那个1,
4.这个是测试数据http://download.csdn.net/detail/zhyh1435589631/8370537
5. 通过与官方给出的源代码比较后发现,自己想的太过复杂
Source Code
Problem: 1782 User: zhyh2010Memory: 228K Time: 16MSLanguage: C++ Result: Accepted- Source Code
#include <iostream>#include <string>using namespace std;void max_length_handle(int & diff_count,char * diff_temp){if (diff_count > 1){cout << 1;for (int jj = 0; jj != diff_count - 1; ++jj){cout << diff_temp[jj];}cout << 1;diff_count = 0;}}int main(int argc, char ** argv){string input;char temp;int count;char * diff_temp = new char[1000];int diff_count;while (getline(cin,input)){// initializetemp = input[0];count = 1;diff_count = 1;diff_temp[0] = temp;// main bodyfor (int ii = 1; ii != input.length() + 1; ++ii){// 数据全部处理完if (ii == input.length()){if (count > 1){// 最大相同输出cout << count << temp;diff_count = 0;}else{// 最大序列输出// 使用++ 处理最后单个字符// **************若最后单个数字为 1 时候!!!!**********if (input[ii - 1] == '1'){diff_temp[diff_count] = '1';++diff_count;}max_length_handle(++diff_count, diff_temp);}// 跳出break;}// 逐步处理数据if (temp == input[ii]){// 前后相同字符处理// 最大9个一起处理if (count == 9){cout << count << temp;temp = input[ii];count = 0;}// 得到最大序列ABCDEE AAmax_length_handle(diff_count, diff_temp);// body++count;}else{// 前后不同字符处理// 前面的字符已经可以输出时 AAAB ==> 3Aif (count > 1){cout << count << temp;diff_count = 0;temp = '\0'; // attention!!! 注意 1 操作}// 序列中有1时候的处理if (temp == '1'){diff_temp[diff_count] = '1';++diff_count;}// 将不同的字符加入 diff_temp 序列中去diff_temp[diff_count] = input[ii];++diff_count;count = 1;temp = input[ii];}}cout << endl;}return 0;}
这时官方给出的源代码:
// Problem Run Length Encoding// Algorithm Straight-Forward// Runtime O(n)// Author Walter Guttmann// Date 2003.12.07#include <fstream>#include <iostream>#include <string>using namespace std;ifstream in("encoding.in");int main(){ string line; while (getline(in, line)) { for (string::iterator it = line.begin() ; it != line.end() ; ) { int rep = 1; while (rep < 9 && (it+rep) != line.end() && *it == *(it+rep)) ++rep; // A sequence of min(rep,9) identical characters starts at *it. if (rep > 1) { cout << rep << *it; it += rep; } else { cout << 1; for ( ; it != line.end() && ((it+1) == line.end() || *it != *(it+1)) ; ++it) { cout << *it; if (*it == '1') cout << *it; } // Either a repetitive sequence begins at *it, or it reached the end of line. cout << 1; } } cout << endl; } return 0;}
0 0
- poj 1782 Run Length Encoding
- POJ--1782 Run Length Encoding
- POJ 1782 Run Length Encoding
- POJ 1782 Run Length Encoding 笔记
- POJ-1782 Run Length Encoding-相同字符个数
- poj 1782 Run Length Encoding 自动机处理字符串
- Run-Length Encoding
- 1639. Run Length Encoding
- 1639. Run Length Encoding
- POJ1782:Run Length Encoding
- Run-Length Encoding
- Run Length Encoding
- POJ 1782/ ZOJ 2240 : Run Length Encoding - 字符串处理(模拟)
- Sicily 1639. Run Length Encoding
- ZOJ-2240 Run Length Encoding
- 【FOJ 1360】 Run Length Encoding
- P11 (*) Modified run-length encoding.
- Sicily 1639. Run Length Encoding
- 统计MySQL中某个数据库中有几张表、表所含的列
- Android Studio 快捷键大全
- Java核心技术——对象与类
- android系统启动过程
- 迅雷2014校招笔试编程题--求单链表的差集
- poj 1782 Run Length Encoding
- ucos 整体框架
- linux mint安装网络流量条显示上下行网速
- 理解JMS规范中消息的传输模式和消息持久化
- kindle资源Get去处
- 复制备份表
- Windows Phone 8.1 驱动开发——GPIO 简介
- IOS线程数据篇12之Sqlite3其他属性:排列约束、外键约束、表连接
- michael kors australia gSPyc Ttlf UC71