Topcoder题代码

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

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提供的算法。教训是:数字和字符的转换要弄清楚,写代码时要考虑全面,边界点和特殊点一定要

 

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

内容概要:本文为《科技类企业品牌传播白皮书》,系统阐述了新闻媒体发稿、自媒体博主种草与短视频矩阵覆盖三大核心传播策略,并结合“传声港”平台的AI工具与资源整合能力,提出适配科技企业的品牌传播解决方案。文章深入分析科技企业传播的特殊性,包括受众圈层化、技术复杂性与传播通俗性的矛盾、产品生命周期影响及2024-2025年传播新趋势,强调从“技术输出”向“价值引领”的战略升级。针对三种传播方式,分别从适用场景、操作流程、效果评估、成本效益、风险防控等方面提供详尽指南,并通过平台AI能力实现资源智能匹配、内容精准投放与全链路效果追踪,最终构建“信任—种草—曝光”三位一体的传播闭环。; 适合人群:科技类企业品牌与市场负责人、公关传播从业者、数字营销管理者及初创科技公司创始人;具备一定品牌传播基础,关注效果可量化与AI工具赋能的专业人士。; 使用场景及目标:①制定科技产品全生命周期的品牌传播策略;②优化媒体发稿、KOL合作与短视频运营的资源配置与ROI;③借助AI平台实现传播内容的精准触达、效果监测与风险控制;④提升品牌在技术可信度、用户信任与市场影响力方面的综合竞争力。; 阅读建议:建议结合传声港平台的实际工具模块(如AI选媒、达人匹配、数据驾驶舱)进行对照阅读,重点关注各阶段的标准化流程与数据指标基准,将理论策略与平台实操深度融合,推动品牌传播从经验驱动转向数据与工具双驱动。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值