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):
Now we repeat the process, assuming the opposite about P[0]:
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 | |||||||||||||
| |||||||||||||
Constraints | |||||||||||||
| - | message will contain between 1 and 50 characters, inclusive. | ||||||||||||
| - | Each character in message will be either '0', '1', '2', or '3'. | ||||||||||||
Examples | |||||||||||||
| 0) | |||||||||||||
| |||||||||||||
| 1) | |||||||||||||
| |||||||||||||
| 2) | |||||||||||||
| |||||||||||||
| 3) | |||||||||||||
| |||||||||||||
| 4) | |||||||||||||
| |||||||||||||
| 5) | |||||||||||||
| |||||||||||||
我的代码:
#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提供的算法。教训是:数字和字符的转换要弄清楚,写代码时要考虑全面,边界点和特殊点一定要
考虑,挑战时要谨慎,可用边界点和特殊点挑战。
本文分享了一次在Topcoder平台上完成的编码挑战,耗时45分钟,创下了个人最快记录。文章重点介绍了实现的二进制解码算法,通过解析输入字符串,将二进制数转化为十进制并处理特殊情况。作者强调了清晰的算法思路、全面考虑问题以及充分利用STL库的重要性,并从错误中学习到数字字符转换、边界条件处理和挑战策略的教训。
1万+

被折叠的 条评论
为什么被折叠?



