zoukankan      html  css  js  c++  java
  • 两个字符串的最大相同子串

     1 public class TestsonString {
     2     public static void main(String[] args) {
     3         String s1 = "asdfkljasdCCTVCCTVfioejsdfunbsdfhh";
     4         String s2 = "asdfkasdiCCTVCCTVufasdfjh";
     5         sonStringOperation(s1,s2);
     6         
     7         System.out.println("
    ================================================");
     8         String s3 = "abcdefgdcba";
     9         String s4 = "asdabcdfkasdiCCTVCCTVufasdfdcbajh";10         sonStringOperation(s3,s4);11     }     
    12     private static void sonStringOperation(String s1,String s2) { 
    13       if(s1 == null || s2 == null || s1.length() == 0 || s2.length() == 0)
    14           return;           15         String temp,s3; 
    16         if (s1.length() < s2.length()) {// 比较s1与s2长度,将较长的字符串赋给s1
    17             temp = s2;
    18             s2 = s1;
    19             s1 = temp;
    20         }
    21         int count = s2.length();// 将较短字符串的长度赋值给count
    22         int sum = (count + 1) * count / 2;// 计算得出所有可能结果的最大值sum
    23         String[] sonArray = new String[sum];// 分配sum个空间给sonArray数组
    24         int arrIndex = 0;// sonArray数组下标
    25         if (s1.contains(s2)) {
    26             System.out.println("最大子字符串为" + s2);
    27         } else if (!s1.contains(s2)) {
    28             for (int i = 0; i < s2.length(); i++) {
    29                 for (int j = s2.length(); j > i; j--) {
    30                     s3 = s2.substring(i, j);// 将截取后的字符串赋值给s3
    31                     if (s1.contains(s3)) {// 如果s3为s1的子字符串,则将s3置入sonArray数组中
    32                         sonArray[arrIndex] = s3;
    33                         arrIndex++;
    34                     }
    35                 }
    36             }
    37             if (sonArray[0] == null) {// 如果sonArray数组为空
    38                 System.out.println("两个字符串之间没有相同的子字符串!");
    39                 return;
    40             }
    41             temp = sonArray[0];
    42             count = 0;
    43             for (int i = 1; i < arrIndex; i++) {
    44                 if (temp.length() < sonArray[i].length()) {
    45                     temp = sonArray[i];
    46                     count = 1;
    47                 }else if(temp.length() == sonArray[i].length()) {
    48                     count++;
    49                 }
    50             }
    51             System.out.print("两个字符串的相同的最大子字符串为:");
    52             for (int i = 0; i < arrIndex; i++) {
    53                 if(temp.length()==sonArray[i].length())
    54                     System.out.print(sonArray[i]+" ");
    55             }
    56         }
    57     }
    58 }
  • 相关阅读:
    Visual Studio 2010 快捷键
    win7下安装vs2010报vs_setup访问不了的问题
    【技术应用】OneNote发送到cnblogs的方法
    Firefox 7中chrome://URL的基址
    独立安装SharePoint 2010 不允许所请求的注册表访问权 错误解决
    【jquery练习】返回顶部+伸缩显示
    都是function $(id){ return document.getElementById(id); }惹得祸
    【jquery练习】横向滚动导航菜单
    [转]免费api大全
    jpg格式的图片,在ie下不显示
  • 原文地址:https://www.cnblogs.com/archimedes-euler/p/9975848.html
Copyright © 2011-2022 走看看