• 两个字符串的最大相同子串


     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 }
  • 相关阅读:
    ORACLE 11g RACRAC DG Duplicate 搭建(生产操作文档)
    oracle跨数据库
    Linux查看网络流量
    数据库性能监控脚本
    三节点DG环境主库单机转RAC(DG主备切换)
    Oracle dblink介绍
    script 加载失败触发onerror事件
    流程图
    The State of JavaScript 2019
    throw , console.error, new Error
  • 原文地址:https://www.cnblogs.com/archimedes-euler/p/9975848.html
Copyright © 2020-2023  润新知