Topcoder题代码

本文分享了一次在Topcoder平台上完成的编码挑战,耗时45分钟,创下了个人最快记录。文章重点介绍了实现的二进制解码算法,通过解析输入字符串,将二进制数转化为十进制并处理特殊情况。作者强调了清晰的算法思路、全面考虑问题以及充分利用STL库的重要性,并从错误中学习到数字字符转换、边界条件处理和挑战策略的教训。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Statement

    

Let's say you have a binary string such as the following:

011100011

One way to encrypt this string is to add to each digit the sum of its adjacent digits. For example, the above string would become:

123210122

In particular, if P is the original string, and Q is the encrypted string, then Q[i] = P[i-1] + P[i] + P[i+1] for all digit positions i. Characters off the left and right edges of the string are treated as zeroes.

An encrypted string given to you in this format can be decoded as follows (using 123210122 as an example):

  1. Assume P[0] = 0.
  2. Because Q[0] = P[0] + P[1] = 0 + P[1] = 1, we know that P[1] = 1.
  3. Because Q[1] = P[0] + P[1] + P[2] = 0 + 1 + P[2] = 2, we know that P[2] = 1.
  4. Because Q[2] = P[1] + P[2] + P[3] = 1 + 1 + P[3] = 3, we know that P[3] = 1.
  5. Repeating these steps gives us P[4] = 0, P[5] = 0, P[6] = 0, P[7] = 1, and P[8] = 1.
  6. We check our work by noting that Q[8] = P[7] + P[8] = 1 + 1 = 2. Since this equation works out, we are finished, and we have recovered one possible original string.

Now we repeat the process, assuming the opposite about P[0]:

  1. Assume P[0] = 1.
  2. Because Q[0] = P[0] + P[1] = 1 + P[1] = 0, we know that P[1] = 0.
  3. Because Q[1] = P[0] + P[1] + P[2] = 1 + 0 + P[2] = 2, we know that P[2] = 1.
  4. Now note that Q[2] = P[1] + P[2] + P[3] = 0 + 1 + P[3] = 3, which leads us to the conclusion that P[3] = 2. However, this violates the fact that each character in the original string must be '0' or '1'. Therefore, there exists no such original string P where the first digit is '1'.

Note that this algorithm produces at most two decodings for any given encrypted string. There can never be more than one possible way to decode a string once the first binary digit is set.

Given a string message, containing the encrypted string, return a vector <string> with exactly two elements. The first element should contain the decrypted string assuming the first character is '0'; the second element should assume the first character is '1'. If one of the tests fails, return the string "NONE" in its place. For the above example, you should return {"011100011", "NONE"}.

Definition

    
Class: BinaryCode
Method: decode
Parameters: string
Returns: vector <string>
Method signature: vector <string> decode(string message)
(be sure your method is public)
    
 

Constraints

- message will contain between 1 and 50 characters, inclusive.
- Each character in message will be either '0', '1', '2', or '3'.

Examples

0)  
    
"123210122"
Returns: { "011100011",  "NONE" }

The example from above.

1)  
    
"11"
Returns: { "01",  "10" }

We know that one of the digits must be '1', and the other must be '0'. We return both cases.

2)  
    
"22111"
Returns: { "NONE",  "11001" }

Since the first digit of the encrypted string is '2', the first two digits of the original string must be '1'. Our test fails when we try to assume that P[0] = 0.

3)  
    
"123210120"
Returns: { "NONE",  "NONE" }

This is the same as the first example, but the rightmost digit has been changed to something inconsistent with the rest of the original string. No solutions are possible.

4)  
    
"3"
Returns: { "NONE",  "NONE" }
 
5)  
    
"12221112222221112221111111112221111"
Returns: 
{ "01101001101101001101001001001101001",
  "10110010110110010110010010010110010" }

我的代码:

 

     #include <iostream>
#include <string>
#include <list>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <bitset>
using  namespace  std;
class  BinaryCode{
public:
 vector<string>  decode(string  message)
 {
           vector<int>  vFirstq,vFirstp,vSecondq,vSecondp;
     vector<string>vString;
     string   strA,strB;
     bool    bFirst=false,bSecond=false;
     if (message.length()==1&&(message[0]=='0'||message[0]=='1'))
     {
      if (message[0]=='0')
      {
       vString.push_back("0");
       vString.push_back("NONE");
      }
      if (message[0]=='1')
      {
       vString.push_back("NONE");
       vString.push_back("1");
      }
     }
     else
     {
     for (int i=0;i<message.length();++i)
     {
      vFirstq.push_back((int)(message[i])-48);
     }
   
     for (int i=0;i<vFirstq.size();++i)
     {
      if (i==0)
      {
                    vFirstp.push_back(0);
      }
      if (i==1)
      {
       if (vFirstq[0]-vFirstp[0]<0)
       {
        bFirst =true;
       }
       else
                    vFirstp.push_back(vFirstq[0]-vFirstp[0]);
      }
      if (i>1)
      {
                     vFirstp.push_back(vFirstq[i-1]-vFirstp[i-1]-vFirstp[i-2]);
      }
   }
     if (bFirst)
     {
               strA = "NONE";
     }
     else
     {
     if (*(max_element(vFirstp.begin(),vFirstp.end()))>1||vFirstq[vFirstq.size()-1]!=vFirstp[vFirstp.size()
      -1]+vFirstp[vFirstp.size()-2])
     {
      strA = "NONE";
     }
     else
     {
           for (int i=0;i<vFirstp.size();++i)
           {
      strA.push_back((char)(vFirstp[i]+48));
           }
     }
     }
     vString.push_back(strA);
     for (int i=0;i<message.length();++i)
     {
      vSecondq.push_back((int)(message[i])-48);
     }
    
   
     for (int i=0;i<vSecondq.size();i++)
     {
      if (i==0)
      {
                   vSecondp.push_back(1);
      }
      if (i==1)
      {
       if (vSecondq[0]-vSecondp[0]<0)
       {
        bSecond =true;
        break;
       }
       else
                   vSecondp.push_back(vSecondq[0]-vSecondp[0]);
      }
      if (i>1)
      {
                  vSecondp.push_back(vSecondq[i-1]-vSecondp[i-1]-vSecondp[i-2]);
      }
     }
     if (bSecond)
     {
                strB = "NONE";
     }
     else
     {
     if (*(max_element(vSecondp.begin(),vSecondp.end()))>1||(vSecondq[vSecondq.size()-1]!=vSecondp[vSecondp.size()
      -1]+vSecondp[vSecondp.size()-2]))
     {
      strB = "NONE";
     }
     else
     {
     for (int i=0;i<vSecondp.size();++i)
     {
      strB.push_back((char)(vSecondp[i]+48));
     }
     }
     }
     vString.push_back(strB);
           }
     return  vString;
     }
 
};

总结:

 

   此次做题耗时45分钟,是做topcoder以来耗时最短的一次。经验是:头脑中一定要有清晰的算法,要考虑

 

  全面,还要善于利用STL提供的算法。教训是:数字和字符的转换要弄清楚,写代码时要考虑全面,边界点和特殊点一定要

 

  考虑,挑战时要谨慎,可用边界点和特殊点挑战。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值