题解 | #公共子串计算#

公共子串计算

http://www.nowcoder.com/practice/98dc82c094e043ccb7e0570e5342dd1b

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        char[] chrs1 = scan.nextLine().toCharArray();
        char[] chrs2 = scan.nextLine().toCharArray();
        int len1 = chrs1.length;
        int len2 = chrs2.length;
        int ans = 0;
        for (int i = 0; i < len1; i++) {
            for (int j = 0; j < len2; j++) {
                if (chrs1[i] == chrs2[j]) {
                    int p = 0;
                    while (i + p < len1 && j + p < len2 && chrs1[i + p] == chrs2[j + p]) {
                        p++;
                    }
                    ans = Math.max(ans, p);
                }
            }
        }
        System.out.println(ans);
    }
}
全部评论
该牛油正在参与牛客写题解薅羊毛的活动,牛币,周边,京东卡超多奖品放送,活动进入倒计时!快来捡漏啦https://www.nowcoder.com/discuss/888949?source_id=profile_create_nctrack&channel=-1
点赞
送花
回复
分享
发布于 2022-04-20 17:00

相关推荐

点赞 2 评论
分享
牛客网
牛客企业服务