`
JokerT
  • 浏览: 22979 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

字符串与数字转换(续)

 
阅读更多

(SRM 144 DIV2) 550分题:

 

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] = 0, 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)
 
 

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" }
 

 

花了一个小时左右,发现仍然纠结于字符串与数字之间的转换,先研究下提交平台上一个比较好的做法:

String[] s={"",""};

char[] c=message.toCharArray();

int[] q=new int[message.length()];

for(int i=0;i<message.length();i++)

{

q[i]=Integer.parseInt(c[i]+"");

}

以下是我的方法:

String[] result = new String[2];
for(int k=0;k<2;k++){
result[k]="";
}

int length = estring.length();
int[] e = new int[length];//encrypted string
int[] d = new int[length];//decrypted string
for (int i = 0; i < length; i++) {
String temp;
temp = String.valueOf(estring.charAt(i));
e[i] = Integer.valueOf(temp);

}

对比红色部分,我的代码显然繁琐且容易出错。另外数组的初始化也显得比较笨拙。

本题源代码:

public class BinaryCode {

public static String[] decode(String estring) {
String[] result = new String[2];
for(int k=0;k<2;k++){
result[k]="";
}
int length = estring.length();
int[] e = new int[length];//encrypted string
int[] d = new int[length];//decrypted string
for (int i = 0; i < length; i++) {
String temp;
temp = String.valueOf(estring.charAt(i));
e[i] = Integer.valueOf(temp);
}
int m = 0;//for e
int n = 0;//for
for (int k = 0; k < 2; k++) {
d[0] = k;
d[1] = e[0] - d[0];
result[k]+=d[0]+""+d[1]+"";
m = 1;
n = 2;
while (n < length) {
d[n] = e[n - 1] - d[n - 2] - d[n - 1];
if (d[n] < 2) {
result[k] += d[n] + "";
} else {
result[k] = "NONE";
break;
}
n++;
}

}
return result;
}

public static void main(String[] args) {
String a = "12221112222221112221111111112221111";
String[] aresult = new String[a.length()];
aresult = decode(a);
System.out.println(aresult[1]);
}
}

分享到:
评论

相关推荐

    模块7Python字符串与正则表达式.pptx

    字符串编码是指将字符串转换为数字的过程,因为计算机只能处理数字。常见的字符串编码有 ASCII 码、GB2312 编码、Unicode 编码和 UTF-8 编码等。 * ASCII 码:美国标准信息交换码,仅对 10 个数字、26 个英文字母的...

    python 个人学习笔记

    - **整型转换**:使用`int()`函数将字符串或数字转换为整数。例如`int("123")`将返回整数123。如果没有提供参数,则返回0。默认情况下,`int()`函数假设输入的字符串是十进制的。 - **随机数生成**: - `random`...

    2014华为机试题目

    本题目要求实现一个字符串压缩功能,即将字符串中连续出现的相同字符压缩成数字加字符的形式。例如,对于字符串 "xxxyyyyyyz",压缩后应得到 "3x6yz"。 **函数接口**: ```cpp void stringZip(const char *...

    oracle新手入门指导之三续——精简ORACLE常用函数大全.txt

    - **TO_NUMBER**:将字符串转换为数字。 - 示例:`TO_NUMBER('99f', 'xxxx')` 尝试将 `'99f'` 转换为数字,但结果取决于输入的格式。 - **NVL**:当第一个参数为空时返回第二个参数的值。 - 示例:`NVL(comm, 0)` ...

    vc2008ip进制转换2

    5. **字符串操作**:当涉及将数值转换成字符串表示时,如显示在用户界面或保存到文件中,就需要使用到字符串操作。C++的`std::string`类和`std::stringstream`类在此处发挥重要作用。 6. **编程实践**:在实际项目...

    二级C上机整理版答案

    10. **字符串处理与字符操作**(续): 删除非尾部的*号,需要遍历字符串两次,第一次找出非尾部的*号的位置,第二次进行删除操作。 这些题目覆盖了C语言的基础知识点,包括数值计算、字符串处理、文件操作、数组...

    2020年《python程序设计》基础知识及程序设计598题PJ[含参考答案].pdf

    【字符串编码与长度(续)】 21. `\x41b`是十六进制表示的字符,'0x41'对应ASCII码65,即字符'A',所以'\x41b'的值是'Ab'。 22. 'abc 你好'.encode('gbk')将字符串编码为GBK格式,'abc'占3个字节,'你好'占4个字节...

    python最强 基础学习文档.pdf

    `\n`用于插入一个换行符,`\`是转义字符,用于在字符串中插入单引号或双引号,`\ `在行尾时用于续行,`\t`代表制表符,可以用来快速创建对齐的文本,`\n\t`组合起来可以实现换行并缩进。 通过深入学习这些基础知识...

    MATLAB PPT学习教案.pptx

    * 字符串操作:MATLAB 提供了多种字符串操作方式,例如字符串连接、字符串提取、字符串替换等。 MATLAB 是一个功能强大且广泛应用的计算机程序设计语言和开发环境,具有广泛的应用前景和发展潜力。

    Python总结精简.pdf

    在处理数字输入时,`()`可能会进行类型转换,而`raw_()`则始终将其视为字符串。 以上就是Python编程的一些基本编码风格和规范,遵循这些规则可以使代码更加整洁、专业,有利于团队协作和长期维护。

    VB编码基础复习.ppt

    转换函数如`Chr$`和`Asc`用于字符和ASCII码的转换,`StrVal`将数值转换为字符串。字符串操作函数如`Left$`、`Mid$`、`Right$`用于提取字符串部分,`Len`获取字符串长度,`InStr`查找子字符串的位置。 赋值语句是将...

    精品专题(2021-2022年收藏)VisualBasic程序设计形成性考核册作业答案二.docx

    4. **数据类型转换**:`Val`函数将字符串转换为数值,`Str`函数则将数值转换为字符串。 5. **语句书写规则**:VB允许一条语句跨多行书写,但每行可以使用续行字符(一个空格后跟一个下划线 `_`)来表示未结束的语句...

    记录:python期末复习题

    5. **函数**:`eval()`函数可以计算字符串表示的数学表达式,`chr()`和`ord()`分别用于字符和其对应的ASCII码值之间的转换。`print()`函数可以将数据输出到文件,输出多个数据时可以用逗号分隔,不一定要用空格。 6...

    VB期末考试复习书本重点总结.doc

    - 转换函数:`Chr$`将ASCII码转换为字符,`Asc`反之,`Ucase$`将字符串转为大写,`StrVal`将数值转换为字符串。 - 字符串操作函数:`Left$`、`Mid$`、`Right$`分别截取字符串的左侧、中间指定长度和右侧,`Len$`...

    VB试题及答案.docx

    5. 表达式合法性:在VB中,字符串与数字的连接需要使用连接符`&`,因此选项C是非法的,因为它尝试将字符串与整数直接相加。 6. `Enabled` 属性:如果要使命令按钮不可操作,需将`Enabled`属性设置为False。 7. ...

    python选择题word打印版.pdf

    `eval()`可以接受字面量字符串,如`"'Hello'"`,但执行时会将其作为字符串处理,不会与`"Hello"`相同。 7. **注释**:Python有单行注释(以`#`开头)和多行注释(以`'''`开头和结尾)。单行注释不能以单引号 `'` ...

    python学习笔记第五天续

    `isdecimal()` 确认字符串是否仅包含十进制数字,`isalpha()` 检查字符串是否只由字母组成,`isalnum()` 则验证字符串是否只含有字母或数字。`isidentifier()` 方法则用来判断字符串是否符合Python的标识符规则。 ...

Global site tag (gtag.js) - Google Analytics