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):

  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());
	}
}

  

时间: 2024-10-23 03:28:12

topcoder(BinaryCode)的相关文章

TopCoder SRM 144 DIV 2

200: Problem Statement   Computers tend to store dates and times as single numbers which represent the number of seconds or milliseconds since a particular date. Your task in this problem is to write a method whatTime, which takes an int, seconds, re

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

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 i

Topcoder 刷题之路_鶸的奋斗

最近碰到的题不是水题就是坑题,实在没意思,听说神犇们都在Topcoder上刷SRM,于是我决定将SRM的DIV 1刷个遍.由于是系列博客,所以我就不贴代码了,只写做法. 哎..好多转博客不注明出处的,这里给出本博客的出处:http://www.cnblogs.com/HarryGuo2012/p/4771117.html Topcoder SRM 144 DIV 1 BinaryCode 题意是:定义串P,Q,其中Q[i]=P[i-1]+P[i]+P[i+1],边界取0,并且P必须是01串.现在

TOPCODER SAM 686 div1 300

// TOPCODER SAM 686 div1 300 Problem Statement 带有小中括号的括号序列,问可以去掉多少子串,使得剩下的非空串是合法的. Constraints 字符串长度不超过 40. Examples // ans[i] = count(s[i]) string s[] = {"()[]", "())", "()()", "([)]", "())[]][]([]()]]()]]]&qu

topcoder srm656 1000分题

Problem Statement   You are given an int N and a int[] pos. We are interested in some permutations of the set {1,2,...,N}. A permutation p is called good if the following condition is satisfied: for each valid k, we have p(k) < p(k+1) if and only if

TopCoder SRM 625 Incrementing Sequence 题解

本题就是给出一个数k和一个数组,包括N个元素,通过每次增加数组中的一个数的操作,最后需要得到1 - N的一个序列,不用排序. 可以从暴力法入手,然后优化. 这里利用hash表进行优化,最终得到时间效率是O(n*n)的算法,而且常数项应该很低,速度还挺快的. 思路: 1 如果数组A[i]在1 -N 范围内,就利用bool B[]记录,这个数已经找到了: 2 如果A[i]的值之前已经找到了,那么就增加k操作,得到新的值A[i]+k,看这个值是否找到了,如果没找到,就使用B记录,如果之前已经找到了,那

Topcoder SRM625 题解

给出一个字符串求是palindrome和anagram的比率是多少. 知识点: 1 DBL_MAX 64位double的最长数大概是1.7E308,很大很大,比long long大上不知多少倍,故此大概能容纳150!的数值,不能容纳200!的数值 2 偶数的时候,不能有字母重复为基数次,否则不能组成palindrome 3 基数的时候,只能有且只有有一个字母重复为基数次,用于放在中间的,否则也不能组成palindrome 4 计算带重复数的全排列公式:P(N) / P(M1)/P(M2)...P

Topcoder SRM 刷题企划

1.本文用来记录自己在Topcoder上刷过的题目.不过重点是记录心得,记录自己的思路问题. 2.刷的题目全部都是Div2 1000分的题目,小概率会去做Div1 的进阶题. 3.基本上自己写出来的题目都不会另开一篇来写. 4.Topcoder使用: [教程1][教程2] SRM 508 Div2 YetAnotherORProblem (Div2 Hard) 题意:构造长度为N,各元素上限为R的序列,并且满足$A_1+A_2+\cdots+A_n=A_1|A_2|\cdots|A_n$,求方案

Topcoder SRM 144 DIV 1

BinaryCode 模拟 题意是:定义串P,Q,其中Q[i]=P[i-1]+P[i]+P[i+1],边界取0,并且P必须是01串.现在给你Q,让你求出P. 做法是:枚举第一位是1还是0,然后就可以推到出P[i]=Q[i-1]-P[i-1]-P[i-2],需要注意一下边界就好. Lottery 组合数学 题意是:给你四种买彩票,将他们的中奖概率排序,这四种彩票都是从1到a中取b个数字,第一种是随便取,第二种是选取的必须是有序的,第三种是选取的必须是不同的,第四种是选取的必须是有序且不同的. 做法