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 ...
随机推荐
- 转:2018最全Redis面试题整理
Java面试----2018最全Redis面试题整理 1.什么是Redis? 答:Redis全称为:Remote Dictionary Server(远程数据服务),是一个基于内存的高性能key-va ...
- 微信公众号支付java版本
回调函数 @RequestMapping("/toPay") public String toPay(HttpServletRequest request, HttpServlet ...
- laravel 安装excel扩展
1,使用Composer安装依赖 在Laravel项目根目录下使用Composer安装依赖: composer require maatwebsite/excel ~2.1 ps:一定要加上~2.1! ...
- web worker,SSE,WebSocket,AJAX 与后端交互的方式
一 web worker web worker 是运行在后台的 JavaScript,独立于其他脚本,不会影响页面的性能.您可以继续做任何愿意做的事情:点击.选取内容等等,而此时 web worker ...
- MyFirstDay(附6篇python亲历面试题)
一直以来都是在看别人写的内容,学习前辈们的经验,总感觉自己好像没有什么值得拿出来分享和交流的知识,最近在准备换工作(python后端开发),坐标上海,2019年3月,半个月面了6家(感觉效率是真不高. ...
- python基础之正则表达式和re模块
正则表达式 就其本质而言,正则表达式(或 re)是一种小型的.高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现.正则表达式模式被编译成一系列的字节码,然后由用 ...
- Python os.walk() 简介
Table of Contents 1. os.walk目录遍历 1.1. os.walk 1.2. 例子 1.2.1. 测试topdown 1.2.2. 运行时修改遍历目录 2. 参考资料 os.w ...
- Android Studio的Log日志调试
本人菜鸟一枚,极大发挥了搜索的功能.现记录一番,以备后患. 用断点真的很烦,因为之前写linux的时候,就是用最蠢但是也是挺有帮助的printf()来进行调试. 其实用Log输出日志的原理也是差不多的 ...
- 20145202马超《Java程序设计》第十周学习总结
一.网络编程 1.网络概述 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定的位置,或者接收到指定的数据,这个就是狭义的网络编程范畴.在发送和接收数据 ...
- 一个简单的同步集群的shell脚本
编写一个xsync文件 然后放在/usr/local/bin 目录下面 xsync文件如下: #!/bin/bash #1 获取输入参数个数,如果没有参数,直接退出 pcount=$# if((pco ...