字符串与数字转换(续)

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

本项目采用C++编程语言结合ROS框架构建了完整的双机械臂控制系统,实现了Gazebo仿真环境下的协同运动模拟,并完成了两台实体UR10工业机器人的联动控制。该毕业设计在答辩环节获得98分的优异成绩,所有程序代码均通过系统性调试验证,保证可直接部署运行。 系统架构包含三个核心模块:基于ROS通信架构的双臂协调控制器、Gazebo物理引擎下的动力学仿真环境、以及真实UR10机器人的硬件接口层。在仿真验证阶段,开发了双臂碰撞检测算法和轨迹规划模块,通过ROS控制包实现了末端执行器的同步轨迹跟踪。硬件集成方面,建立了基于TCP/IP协议的实时通信链路,解决了双机数据同步和运动指令分发等关键技术问题。 本资源适用于自动化、机械电子、人工智能等专业方向的课程实践,可作为高年级课程设计、毕业课题的重要参考案例。系统采用模块化设计理念,控制核心硬件接口分离架构便于功能扩展,具备工程实践能力的学习者可在现有框架基础上进行二次开发,例如集成视觉感知模块或优化运动规划算法。 项目文档详细记录了环境配置流程、参数调试方法和实验验证数据,特别说明了双机协同作业时的时序同步解决方案。所有功能模块均提供完整的API接口说明,便于使用者快速理解系统架构并进行定制化修改。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值