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 ...
随机推荐
- linux文件访问过程和权限
第1章 文件访问过程详解 1.1 文件访问过程 第2章 权限 2.1 对于文件rwx含义 r读取文件内容 w修改文件内容 需要r权限配合 只有w权限的时候,强制保存退出会导致源文件内容丢失 x权限表示 ...
- 4W条人才表循环处理业务sql优化过程
场景: 使用windows服务定时更新合同数据:执行存储过程(pas_RefreshContractStatus),但存储过程里面有一个需要更新4W条人才表循环处理业务 问题: 循环更新4W条人才表状 ...
- 学习python第十二天,函数4 生成器generator和迭代器Iterator
在Python中,这种一边循环一边计算的机制,称为生成器:generator 要创建一个generator,有很多种方法.第一种方法很简单,只要把一个列表生成式的[]改成(),就创建了一个genera ...
- python scrapy 实战简书网站保存数据到mysql
1:创建项目 2:创建爬虫 3:编写start.py文件用于运行爬虫程序 # -*- coding:utf-8 -*- #作者: baikai #创建时间: 2018/12/14 14:09 #文件: ...
- mybatis <forEach>标签的使用
MyBatis<forEach>标签的使用 你可以传递一个 List 实例或者数组作为参数对象传给 MyBatis.当你这么做的时候,MyBatis 会自动将它包装在一个 Map 中,用名 ...
- mysql sum 为 0 的解决方法
使用SQL语句SUM函数的时候,默认查询没有值的情况下返回的是null,而实际可能我们要用的是返回0. 解决方法:SELECT SUM(count) FROM test_table 改成: SELEC ...
- CodeForces 785E Anton and Permutation 分块
题意: 有一个\(1 \sim n\)的排列\(A\),有\(q\)个询问: 交换任意两个元素的位置,求交换之后排列的逆序数 分析: 像这种不太容易用线段树,树状数组维护的可以考虑分块 每\(\sqr ...
- python和matlab
一.python简介 python是一种面向对象的解释型计算机程序设计语言.python是纯粹的自由软件,源代码和解释器CPython遵循GPL协议.Python语法简介清晰,特色之一是强制用空白符作 ...
- 用@property声明的NSString(或NSArray,NSDictionary)经常使用copy关键字,为什么?如果改用strong关键字,可能造成什么问题?
因为父类指针可以指向子类对象,使用 copy 的目的是为了让本对象的属性不受外界影响,使用 copy 无论给我传入是一个可变对象还是不可对象,我本身持有的就是一个不可变的副本. 如果我们使用是 str ...
- 为什么rows这么大,在mysql explain中---写在去acumg听讲座的前一夜
这周五下班前,发现了一个奇怪问题,大概是这个背景 一张表,结构为 Create Table: CREATE TABLE `out_table` ( `id` ) NOT NULL AUTO_INCRE ...