SRM 144 div I 300pt (模拟)
来源:互联网 发布:linux 查看端口占用 编辑:程序博客网 时间:2024/05/01 08:09
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]:
7 Assume P[0] = 1.
8 Because Q[0] = P[0] + P[1] = 1 + P[1] = 0, we know that P[1] = 0.
9 Because Q[1] = P[0] + P[1] + P[2] = 1 + 0 + P[2] = 2, we know that P[2] = 1.
10 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
-
messagewill 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" }
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
题意:
一个二进制数 011100011 被加密成 123210122 ,规则是加密后的每一位是之前的每一位和其前后相邻位的和,即如果P被加密为Q,则有Q[i] = P[i-1]+P[i]+P[i+1]。现要通过Q找出P,方法是猜测第一位为0,找出一个答案,再猜测为1,找出第二个答案。无解则返回NONE。
YY:
今天心血来潮,上了一下TC,看了最早的一个practice :144.
额,题目真不想看。。。。简单的模拟。要是普通的acm题的话我还能搞定,但TC是c++的类实现额,c++我还不熟。...> _ < 弱爆了。 其实也差不多,在形参中输入数,然后return 结果。而且tc也可以编译,测试。不过偶现在不会多做tc,第一:acm羽翼未丰,很多算法类型还没搞。第二:c++也未精通,不过这个倒是小问题,这个学期的自学,做TC应该没问题,以后写代码要过渡到c++风格上。
在研究下本题,好为以后TC做个参考。
#include <cstdio>#include <iostream>#include <sstream>#include <cstring>#include <cmath>#include <string>#include <vector>#include <stack>#include <queue> #include <set>#include <map>#include <algorithm>using namespace std;class BinaryCode {public:vector<string> decode(string t) {vector <string > ret;string s="0";int n=t.size ();int i,k;for(i=1;i<n;i++){k=t[i-1]-'0'-s[i-1]+'0';if(i-2>=0) k-=s[i-2]-'0';if(k==0||k==1) s+='0'+k;else {s="NONE";break;}}if(s!=NONE) {if(n>1) {if(s[n-2]-'0'+s[n-1]!=t[n-1]) s="NONE";}else if(s[0]!=t[0]) s="NONE";}ret.push_back (s);s="1";for(i=1;i<n;i++){k=t[i-1]-'0'-s[i-1]+'0';if(i-2>=0) k-=s[i-2]-'0';if(k==0||k==1) s+='0'+k;else {s="NONE";break;}}if(s!="NONE"){if(n>1) {if(s[n-2]-'0'+s[n-1]!=t[n-1]) s="NONE";}else if(s[0]!=t[0]) s="NONE";}ret.push_back (s);return ret;}};
代码系practice room所存
- SRM 144 div I 300pt (模拟)
- SRM 497 div 2 1000pt
- SRM 814 Div 1 ElectionFraudDiv1(250pt)
- Topcoder SRM 709 DIV 2 250pt Robofactory solution
- Topcoder SRM 709 DIV 2 500pt Permatchd2 solution
- SRM 499 250pt
- SRM 596 1000pt
- srm 144 div 2 550
- TopCoder SRM 144 DIV 1
- TopCoder SRM 144 DIV 2
- TopCoder Practice SRMs -- SRM 144 DIV 1 -- 300p
- Topcoder SRM 544 1000pt
- SRM 499 DIV2 500pt
- TC SRM 519 600pt
- tc SRM 554 1000pt
- SRM 613 div1 500pt
- SRM 598 DIV1 500 pt
- Topcoder SRM 569 1000pt
- 2011年第一场雪
- 【分享】分享一本最新的HTML5和CSS书籍 《Pro HTML5 and CSS3 Design Patterns》
- eclipse下插件的安装
- wiki--typedeef
- vc++ error C2533:“*”构造函数不能有返回类型
- SRM 144 div I 300pt (模拟)
- C++ 虚函数表解析
- 一些对科学与宗教,神的感悟
- 为下拉式菜单(DropDownList)添加第一个选项
- 找实习悲剧的经验
- Apache + Tomcat 实现负载均衡(二)
- Erlang中的本地Pid/Socket和远程Pid/Socket
- 浙大第一愤青郑强教授语录
- 转载:SOCKET各种模型下并发数量