华为OD机试算法:字符串比较

题目描述

给定字符串A、B和正整数V,A的长度与B的长度相等, 请计算A中满足如下条件的最大连续子串的长度:

  • 1.

    该连续子串在A和B中的位置和长度均相同。

  • 2.

    该连续子串|A[i] – B[i]|之和小于等于V。其中|A[i] – B[i]|表示两个字母ASCII码之差的绝对值

输入描述

输入为三行:

  • 第一行为字符串A,仅包含小写字符,1 <= A.length <=1000。

  • 第二行为字符串B,仅包含小写字符,1 <= B.length <=1000。

  • 第三行为正整数V,0<= V <= 10000。

输出描述

字符串最大连续子串的长度,要求该子串|A[i] – B[i]|之和小于等于V。

用例

输入

xxcdefg cdefghi 5

输出

2

说明

字符串A为xxcdefg,字符串B为cdefghi,V=5。

题目解析

  • 1.

    首先,我们需要遍历字符串A和B,计算每个位置上字符的ASCII码之差的绝对值。

  • 2.

    然后,我们需要找到满足条件的连续子串,即该子串的|A[i] - B[i]|之和小于等于V。

  • 3.

    最后,我们需要找到满足条件的最大连续子串的长度。

Java算法源码
import java.util.Scanner;
import java.util.stream.IntStream;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    String a = sc.nextLine();
    String b = sc.nextLine();
    int v = Integer.parseInt(sc.nextLine());

    System.out.println(getResult(a, b, v));
  }

  public static int getResult(String a, String b, int v) {
    int n = a.length();

    // a,b字符串的各位字符的ascii绝对值差距数组
    int[] preSum = new int[n + 1];
    for (int i = 1; i <= n; i++) {
      preSum[i] = preSum[i - 1] + Math.abs(a.charAt(i - 1) - b.charAt(i - 1));
    }

    // 记录题解
    int ans = 0;

    // 使用Java 8的流API来简化代码
    IntStream.range(0, n).forEach(l -> {
      IntStream.range(l + 1, n + 1).filter(r -> preSum[r] - preSum[l] <= v).forEach(r -> {
        ans = Math.max(ans, r - l);
      });
    });

    return ans;
  }
}
JS算法源码
const readline = require("readline");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

let lines = [];
rl.on("line", (line) => {
  lines.push(line);

  if (lines.length === 3) {
    const [a, b, v] = lines;
    console.log(getResult(a, b, parseInt(v)));
    lines = [];
  }
});

const getResult = (a, b, v) => {
  const n = a.length;
  const preSum = new Array(n + 1).fill(0);
  for (let i = 1; i <= n; i++) {
    preSum[i] = preSum[i - 1] + Math.abs(a[i - 1].charCodeAt() - b[i - 1].charCodeAt());
  }

  let ans = 0;
  for (let l = 0; l <= n - 1; l++) {
    let r = l;
    while (r <= n && preSum[r] - preSum[l] <= v) {
      ans = Math.max(ans, r - l);
      r++;
    }
  }

  return ans;
};
Python算法源码
# 输入获取
a = input()
b = input()
v = int(input())

# 算法入口
def getResult():
    n = len(a)

    # a, b字符串的各位字符的ASCII绝对值差距数组
    preSum = [0] * (n + 1)
    for i in range(1, n + 1):
        preSum[i] = preSum[i - 1] + abs(ord(a[i - 1]) - ord(b[i - 1]))

    # 记录题解
    ans = 0

    for l in range(n):
        for r in range(l + 1, n + 1):
            # 区间 [l+1, r]的和 = preSum[r] - preSum[l]
            if preSum[r] - preSum[l] <= v:
                ans = max(ans, r - l)

    return ans

# 调用算法
print(getResult())

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一剑破天门

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值