【算法题】字符串比较

246次阅读
没有评论

共计 1096 个字符,预计需要花费 3 分钟才能阅读完成。

import java.util.Scanner;

/**
 * 标题:字符串比较 | 时间限制:1秒 | 内存限制:65536K | 语言限制:不限
 * 给定字符串A、B和正整数V,A的长度与B的长度相等, 请计算A中满足如下条件的最大连续子串的长度:
 * 1、该连续子串在A和B中的位置和长度均相同。
 * 2、该连续子串|A[i] – B[i]|之和小于等于V。其中|A[i] – B[i]|表示两个字母ASCII码之差的绝对值。
 * <p>
 * 第一行为字符串A,仅包含小写字符,1 <= A.length <=1000。
 * 第二行为字符串B,仅包含小写字符,1 <= B.length <=1000。
 * 第三行为正整数V,0<= V <= 10000。
 * 输出描述:
 * 字符串最大连续子串的长度,要求该子串|A[i] – B[i]|之和小于等于V。
 * <p>
 * 示例1
 * 输入
 * xxcdefg
 * cdefghi
 * 5
 * 输出
 * 2
 * 说明
 * 字符串A为xxcdefg,字符串B为cdefghi,V=5。
 * 它的最大连续子串可以是cd->ef,de->fg,ef->gh,fg->hi,所以最大连续子串是2。
 *
 * @since 2022年4月30日
 */
public class M_N_T_15 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String A = scanner.nextLine();
        String B = scanner.nextLine();
        String V = scanner.nextLine();

        char[] As = A.toCharArray();
        char[] Bs = B.toCharArray();
        int v = Integer.parseInt(V);

        int first = 0;
        int len = 0;
        int cha = 0;
        for (int i = 0; i < As.length - 1; i++) {
            int tempLast = 0;
            if (As[i + 1] == As[i] + 1) {
                tempLast = i + 1;
            } else {
                first = i + 1;
                continue;
            }

            for (int j = first; j <= tempLast - 1; j++) {
                if (Bs[j + 1] != Bs[j] + 1) {
                    cha = 0;
                    break;
                }

                if (j == first) {
                    cha += Math.abs(As[j] - Bs[j]);
                }
                cha += Math.abs(As[j + 1] - Bs[j + 1]);
            }

            int tempLen = tempLast - first + 1;
            if (cha <= v && tempLen > len) {
                len = tempLen;
                cha = 0;
            }
        }

        System.out.println(len);
    }
}
正文完
 0
裴先生
版权声明:本站原创文章,由 裴先生 2022-04-30发表,共计1096字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
本站勉强运行: