题解 | #公共子串计算#

公共子串计算

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

//本大神的思考
//将输入的字符串分别用长串和短串存储
//将短串的长度依次减小, 并遍历固定长度下的子串剪切可能
//如果长串包含剪切的子串就返回长度
//否则最后返回0

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        String str1 = sc.nextLine();
        String str2 = sc.nextLine();
        String longStr ;
        String shortStr ;
        if(str1.length() > str2.length())
        {
             longStr = str1;
             shortStr = str2;
        }
        else
        {
             longStr = str2;
             shortStr = str1;
        }
        //len=8   i=5    7 6 5 4 3
        int len = shortStr.length();
        for (int i = len; i >0 ; i--)
        {//长度依次减小
            for (int j = 0; j <= len-i ; j++)
            {
                String tmp = shortStr.substring(j,j+i);
                if (longStr.contains(tmp))
                {
                    System.out.println(i);
                    return;
                }
            }
        }
        System.out.println(0);
    }
}

华为机试题解 文章被收录于专栏

华为机试题解

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-08 13:15
点赞 评论 收藏
分享
程序员小白条:这比例牛逼,750:1
点赞 评论 收藏
分享
06-12 17:46
门头沟学院 Java
运营你豪哥:来说重点: ​1.项目前置,时间倒序。​​ 2.​项目描述强化结果与量化效果(STAR原则里的R)。​​ ​3.个人技能精炼,明确掌握程度,突出核心。​​ ​4.增加强有力开头的个人总结部分。​​ 5.​优化教育背景(成绩排名)、合并奖项与活动。​​
听劝,我这个简历该怎么改...
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-10 11:55
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务