poj 1782 Run Length Encoding

这道题目不难,但是有很多细节是需要注意的

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: zhyh2010
Memory: 228K Time: 16MS
Language: 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))
    	{
    		// initialize
    		temp = input[0];
    		count = 1;
    		diff_count = 1;
    		diff_temp[0] = temp;
    
    		// main body
    		for (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   AA
    				max_length_handle(diff_count, diff_temp);
    
    				// body
    				++count;
    				
    			}
    			else
    			{
    				// 前后不同字符处理
    				// 前面的字符已经可以输出时 AAAB ==> 3A
    				if (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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值