JAVA 机试题

因为一道机试题,第一次机试就被刷了!当时没做出来,回来才想到怎么做!命途多舛啊!

求两字符串中的最大公共子字符串,并输出长度。例如"abbcgre","abcgra"中最大子字符串"bcgr"。

public class GetMax
{
       /*采用二分法查找最大子字符串*/

       static String s1 = "abbcgre";
       static String s2 = "abcgra";
       static int ruler = s2.length();
       public static void main(String arg[])
       {
              ToCompare com = new ToCompare(s1,s2,ruler);
              for(;ruler>0;ruler = ruler/2)
              {
                     com.Setcur(ruler);
                     if(com.Compare())
                     {
                             break;
                     }
                    
              }

              if(ruler>0)
              {
                     for(;ruler<=s2.length();ruler++)
                     {
                             com.Setcur(ruler);
                             if(com.Compare())
                             {      
                                     if(ruler==s2.length())
                                     {
                                                                  

                                       System.out.println(ruler);
                                              com.OutCompare();
                                              break;
                                      }    
                             }
                             else
                             {
                                     ruler--;
                                     com.Setcur(ruler);
                                                                  

                     System.out.println(ruler);
                                     com.OutCompare();
                                     break;
                             }
                     }
                            
              }
              else
              {
                     System.out.println("没有匹配的字符串!");
              }
             
       }
}

public class ToCompare
{
       static String t1;
       static String t2;
       static int cur;
       static int i;
       static int j;
       static boolean temp = false;
       public ToCompare(String a,String b,int c)
       {
              t1 = a;
              t2 = b;
              cur = c;
       }
       public int Setcur(int c)
       {
              cur = c;
              return 0;
       }
       public boolean Compare()   /*比较两字符串中是否有公共字符串

,有的话返回true,否则返回false*/
       {
              a :{ for(i=0;i<t1.length();i++)
                      for(j=0;j<t2.length();j++)
                              {
                              temp = t1.regionMatches(i,t2,j,cur);
                                     if(temp)
                                     {
                                            break a;
                                     }
                              }
                 }
              return temp;
       }
       public boolean OutCompare()   /*输出两字符串中指定长度的全

部公共字符串*/
       {
              a :{ for(i=0;i<t1.length();i++)
                      for(j=0;j<t2.length();j++)
                              {
                              temp = t1.regionMatches(i,t2,j,cur);
                                     if(temp)
                                     {
                                            System.out.println

(t1.substring(i,i+cur));
                      
                                     }
                              }
                 }
              return temp;
       }

}

你可能感兴趣的:(java)