码迷,mamicode.com
首页 > 其他好文 > 详细

取两个字符串的最大交集.

时间:2014-10-23 23:57:05      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   for   sp   div   on   log   

package com.itheima.net;

public class Game13
{
    public static void main(String[] args)
    {
        String s1 = "135adbfg67";
        String s2 = "125dbf59";
        String s3 = s2;
        int begin = 0;
        int end = s2.length();
        int i = 1;
        while (!s1.contains(s3))
        {
            if (end == s2.length())
            {
                begin = 0;
                end = (s2.length()) - (i++);
            }
            else
            {
                begin++;end++;
            }
            s3 = s2.substring(begin, end);
            System.out.println(s3);
            System.out.println("--------");
        }
        System.out.println(s3);
    }
}
 1 package com.itheima.net;
 2 
 3 public class Game15
 4 {
 5     public static void main(String[] args)
 6     {
 7         String s1 = "135adbfg67";
 8         String s2 = "125dbf59";
 9         method(s2, s1);
10     }
11     public static void method(String max, String min)
12     {
13         if (max.length() < min.length())
14         {
15             String s = max;
16             max = min;
17             min = s;
18         }
19         String subStr = min;
20         for (int begin = 0, end = min.length(), i = 1; !max.contains(subStr); subStr = min.substring(begin, end))
21         {
22             if (end == min.length())
23             {
24                 begin = 0;
25                 end = (min.length()) - (i++);
26             }
27             else
28             {
29                 begin++;
30                 end++;
31             }
32             System.out.println(subStr);
33             System.out.println("--------");
34         }
35         System.out.println(subStr);
36     }
37 }

 

取两个字符串的最大交集.

标签:style   blog   color   ar   for   sp   div   on   log   

原文地址:http://www.cnblogs.com/stone1022/p/4046999.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!