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] = 1, 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 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: String[]
Method signature: String[] decode(String message)
(be sure your method is public)

Limits

    
Time limit (s): 2.000
Memory limit (MB): 64

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" }

AC code:

import java.util.Scanner;

public class BinaryCode
{ public String[] decode(String message)
{
String str1 = "";
String str2 = "";
char[] P = new char[message.length()];
int i;
boolean flag = true;
//first method
if(message.length() == 1)
flag = false;
for(i=0; i<message.length(); i++)
{
if(i==0)
{
P[i]='0';
//System.out.println(P[i]);
continue;
}
if(i==1)
{
P[i] = (char)(message.charAt(i-1)+48-P[i-1]);
if(P[i]>'1' || P[i]<'0')
break;
//System.out.println("1111===="+P[i]);
continue;
}
if(i == message.length()-1)
{
P[i] = (char)(message.charAt(i) +48-P[i-1]);
//System.out.println((int)P[i]);
if(P[i]>'1' || P[i]<'0'){
flag = false;
break;
}
//System.out.println("last: "+P[i]);
continue;
}
P[i] = (char) (message.charAt(i-1) +96 - P[i-1] - P[i-2]);
if(P[i]>'1' || P[i]<'0')
break;
//System.out.println("mid: "+P[i]);
}
if(i==message.length() && flag)
{
str1 = new String(P);
/*for(char c: P)
{
System.out.print(c+" , ");
}
System.out.println();*/
}else
str1="NONE"; //System.out.println("====================");
//second method char[] P2 = new char[message.length()];
flag = true;
if(message.length() == 1)
flag = false;
for(i=0; i<message.length(); i++)
{
if(i==0)
{
P2[i]='1';
//System.out.println(P[i]);
continue;
}
if(i==1)
{
P2[i] = (char)(message.charAt(i-1)+48-P2[i-1]);
if(P2[i]>'1' || P2[i]<'0')
break;
//System.out.println("1111===="+P[i]);
continue;
}
if(i == message.length()-1)
{
P2[i] = (char)(message.charAt(i) +48-P2[i-1]);
//System.out.println((int)P[i]);
if(P2[i]>'1' || P2[i]<'0'){
flag = false;
break;
}
//System.out.println("last: "+P[i]);
continue;
}
P2[i] = (char) (message.charAt(i-1) +96 - P2[i-1] - P2[i-2]);
if(P2[i]>'1' || P2[i]<'0')
break;
//System.out.println("mid: "+P[i]);
}
if(i==message.length() && flag)
{
str2 = new String(P2);
/*for(char c: P2)
{
System.out.print(c+" , ");
}
System.out.println();*/
}else
str2="NONE";
return new String[]{str1,str2};
}
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
BinaryCode bc = new BinaryCode();
String[] str = bc.decode(s);
System.out.println(str[0].toString());
System.out.println(str[1].toString());
}
}

  

topcoder(BinaryCode)的更多相关文章

  1. TopCoder SRMS 1 字符串处理问题 Java题解

    Problem Statement   Let's say you have a binary string such as the following: 011100011 One way to e ...

  2. TopCoder kawigiEdit插件配置

    kawigiEdit插件可以提高 TopCoder编译,提交效率,可以管理保存每次SRM的代码. kawigiEdit下载地址:http://code.google.com/p/kawigiedit/ ...

  3. 记第一次TopCoder, 练习SRM 583 div2 250

    今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...

  4. TopCoder比赛总结表

    TopCoder                        250                              500                                 ...

  5. Topcoder几例C++字符串应用

    本文写于9月初,是利用Topcoder准备应聘时的机试环节临时补习的C++的一部分内容.签约之后,没有再进行练习,此文暂告一段落. 换句话说,就是本文太监了,一直做草稿看着别扭,删掉又觉得可惜,索性发 ...

  6. TopCoder

    在TopCoder下载好luncher,网址:https://www.topcoder.com/community/competitive%20programming/ 选择launch web ar ...

  7. TopCoder SRM 596 DIV 1 250

    body { font-family: Monospaced; font-size: 12pt } pre { font-family: Monospaced; font-size: 12pt } P ...

  8. 求拓扑排序的数量,例题 topcoder srm 654 div2 500

    周赛时遇到的一道比较有意思的题目: Problem Statement      There are N rooms in Maki's new house. The rooms are number ...

  9. TopCoder SRM 590

     第一次做TC,不太习惯,各种调试,只做了一题...... Problem Statement     Fox Ciel is going to play Gomoku with her friend ...

随机推荐

  1. Java基础-方法区以及static的内存分配图

    转载自: https://blog.csdn.net/Wang_1997/article/details/52267688 前面的几篇都没有太明确地指出 方法区 是什么?现在通过一些资料的收集和学习, ...

  2. Linux - bashrc之alias

    1. cd ~ 2. touch .bashrc // 若该文件不存在的话 3. vim .bashrc ----------------复制粘贴如下文本--------------- # alias ...

  3. (转)数据库老兵:NewSQL才是未来

    编者按:在数据库技术领域,Michael Stonebraker几乎是无人不知无人不晓的人物.现年70岁的Stonebraker不仅是Ingres和PostgreSQL的创始人,同时在Informix ...

  4. 学习python第十六天,正则表达式

    正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配.采取动态模糊的匹配,最大的应用是爬虫. re 模块使 Python 语言拥有全部的正则表达式功能. compile 函 ...

  5. [BZOJ2527] [Poi2011]Meteors(整体二分)

    对于单个国家,可以对答案进行二分,每次找出此时的陨石数量,如果大于需要的那么答案就在[l,mid],否则就在[mid+1,r]里面 而对于很多国家,也可以进行二分,solve(l,r,L,R)表示询问 ...

  6. java实时监听日志写入kafka(多目录)

    目的 实时监听多个目录下的日志文件,如有新文件切换到新文件,并同步写入kafka,同时记录日志文件的行位置,以应对进程异常退出,能从上次的文件位置开始读取(考虑到效率,这里是每100条记一次,可调整) ...

  7. 9,Flask 中的蓝图(BluePrint)

    蓝图,听起来就是一个很宏伟的东西,在Flask中的蓝图 blueprint 也是非常宏伟的,它的作用就是将 功能 与 主服务 分开. 比如说,你有一个客户管理系统,最开始的时候,只有一个查看客户列表的 ...

  8. Android stadio 工具使用

    android staido 有logcat窗口,她可以显示log信息.还有run窗口. 我以前一直忽略了run窗口,其实蛮重要,蛮好用的.他只会显示你当前运行的进程的log,不用你再去设置filld ...

  9. BP神经网络的手写数字识别

    BP神经网络的手写数字识别 ANN 人工神经网络算法在实践中往往给人难以琢磨的印象,有句老话叫“出来混总是要还的”,大概是由于具有很强的非线性模拟和处理能力,因此作为代价上帝让它“黑盒”化了.作为一种 ...

  10. 网易考拉Android客户端网络模块设计

    本文来自网易云社区 作者:王鲁才 客户端开发中不可避免的需要接触到访问网络的需求,如何把访问网络模块设计的更具有扩展性是每一个移动开发者不得不面对的事情.现在有很多主流的网络请求处理框架,如Squar ...