topcoder(BinaryCode)
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 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 |
|||||||||||||
|
|||||||||||||
Limits |
|||||||||||||
|
|||||||||||||
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) | |||||||||||||
|
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)的更多相关文章
- TopCoder SRMS 1 字符串处理问题 Java题解
Problem Statement Let's say you have a binary string such as the following: 011100011 One way to e ...
- TopCoder kawigiEdit插件配置
kawigiEdit插件可以提高 TopCoder编译,提交效率,可以管理保存每次SRM的代码. kawigiEdit下载地址:http://code.google.com/p/kawigiedit/ ...
- 记第一次TopCoder, 练习SRM 583 div2 250
今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...
- TopCoder比赛总结表
TopCoder 250 500 ...
- Topcoder几例C++字符串应用
本文写于9月初,是利用Topcoder准备应聘时的机试环节临时补习的C++的一部分内容.签约之后,没有再进行练习,此文暂告一段落. 换句话说,就是本文太监了,一直做草稿看着别扭,删掉又觉得可惜,索性发 ...
- TopCoder
在TopCoder下载好luncher,网址:https://www.topcoder.com/community/competitive%20programming/ 选择launch web ar ...
- TopCoder SRM 596 DIV 1 250
body { font-family: Monospaced; font-size: 12pt } pre { font-family: Monospaced; font-size: 12pt } P ...
- 求拓扑排序的数量,例题 topcoder srm 654 div2 500
周赛时遇到的一道比较有意思的题目: Problem Statement There are N rooms in Maki's new house. The rooms are number ...
- TopCoder SRM 590
第一次做TC,不太习惯,各种调试,只做了一题...... Problem Statement Fox Ciel is going to play Gomoku with her friend ...
随机推荐
- 基于THINKPHP+layui+Ajax无刷新实现图片上传预览
<fieldset class="layui-elem-field" style="width:500px;margin:50px 0 0 300px;" ...
- 谈一谈如何远程访问MySQL(腾讯云,云主机)
连接MySQL (其他的sql 基本相同套路) 腾讯云不管怎么设置端口和MySQL权限以及监听端口就是不能连接? 远程访问MySQL数据库的几个关键点 端口设置 数据库权限设置 数据库的监听端口设置 ...
- [BZOJ2427][HAOI2010]软件安装(tarjan+树形DP)
如果依赖关系出现环,那么对于一个环里的点,要么都选要么都不选, 所以每个环可以当成一个点,也就是强连通分量 然后就可以构造出一颗树,然后树形背包瞎搞一下就行了 注意要搞一个虚拟节点当根节点 Code ...
- ABAP 7.51 構文書き方変換について
便利になったなぁと感じたコマンドのメモです.NetWeaver7.40 SP2から使えます. SAP NetWeaver AS for ABAP 7.51 Innovation Package – O ...
- TensorFlow验证码识别
本节我们来用 TensorFlow 来实现一个深度学习模型,用来实现验证码识别的过程,这里我们识别的验证码是图形验证码,首先我们会用标注好的数据来训练一个模型,然后再用模型来实现这个验证码的识别. 验 ...
- android Intent onNewIntent 什么时候调用
1.activity A 的lanch model 为singleTop 此刻,A在activity 栈顶,那么就会调用A 的onNewIntent 如果A不在栈顶,则不会调用. 2.activity ...
- Kafka写入流程和副本策略
Kafka写入流程: 1.producer 先从 zookeeper 的 "/brokers/.../state" 节点找到该 partition 的 leader 2. prod ...
- laravel5.5事件广播系统
目录 1. 定义广播事件 1.1 广播名称 1.2 广播数据 1.3 广播队列 1.4 广播条件 2. 频道授权 2.1 定义授权路由 2.2 定义授权回调 3. 对事件进行广播 3.1 可以使用ev ...
- Android保持屏幕常亮唤醒状态
第一步: 首先添加权限: <uses-permission android:name="android.permission.WAKE_LOCK"></uses ...
- Object Pascal中文手册 经典教程
Object Pascal 参考手册 (Ver 0.1)ezdelphi@hotmail.com OverviewOverview(概述)Using object pascal(使用 object p ...