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 ...
随机推荐
- Spring Security 简介
本文引自:https://blog.csdn.net/xlecho/article/details/80026527 在 Web 应用开发中,安全一直是非常重要的一个方面.安全虽然属于应用的非功能性需 ...
- js表格打印自动分页demo
本文翻译自:How Does setState Know What to Do? 原作者:Dan Abramov 如果有任何版权问题,请联系shuirong1997@icloud.com 当你在组件中 ...
- c#常用数据结构解析【转载】
引用:http://blog.csdn.net/suifcd/article/details/42869341 前言:可能去过小匹夫博客的盆油们读过这篇对于数据结构的总结,但是小匹夫当时写那篇文章的时 ...
- python__系统 : socket_UDP相关
socket.socket() 可以创建一个套接字: from socket import * from threading import Thread udp_socket = None dest_ ...
- 关于 PHP 程序员技术职业生涯规划
原文地址:http://rango.swoole.com/archives/570 看到很多 PHP 程序员职业规划的文章,都是直接上来就提 Linux.PHP.MySQL.Nginx.Redis.M ...
- 对Neural Machine Translation by Jointly Learning to Align and Translate论文的详解
读论文 Neural Machine Translation by Jointly Learning to Align and Translate 这个论文是在NLP中第一个使用attention机制 ...
- Python中的str
str_lst = [ ('元素替换',), ('字符串切片',), ('字符串分割',), ('字符串连接',), ('元素计数',), ('寻找元素',), ('判断字符串的开头与结尾',), ( ...
- C语言进阶—— 逻辑运算符分析15
印象中的逻辑运算符: ---学生:老师,在我的印象中,逻辑运算符用在条件判断的时候,真挺简单的,还有必要深究吗? ---老师:逻辑运算符确实在条件判断的时候用的比较多,但是并不能说简单... 请思考下 ...
- Docker使用入门
docker images 查看本地镜像 docker ps -a 查询容器 docker ps -l 查询最近使用容器 docker rm CONTAINER_ID 删除容器 docker rm ...
- 笔记-爬虫-XPATH
笔记-爬虫-XPATH 1. xpath XPath是W3C的一个标准.它最主要的目的是为了在XML1.0或XML1.1文档节点树中定位节点所设计.目前有XPath1.0和XPath2.0两 ...