第八屆藍橋杯b組java第六題

來源:互聯網
上載者:User

標籤:標題   log   rar   bsp   oid   情況   char   劃線   ada   

標題:最大公用子串

最大公用子串長度問題就是:
求兩個串的所有子串中能夠匹配上的最大長度是多少。

比如:"abcdkkk" 和 "baabcdadabc",
可以找到的最長的公用子串是"abcd",所以最大公用子串長度為4。

下面的程式是採用矩陣法進行求解的,這對串的規模不大的情況還是比較有效解法。

請分析該解法的思路,並補全劃線部分缺失的代碼

/藍橋杯第六題 public class Main {    static int f(String s1, String s2) {        char[] c1 = s1.toCharArray();        char[] c2 = s2.toCharArray();        int[][] a = new int[c1.length + 1][c2.length + 1];        int max = 0;        for (int i = 1; i < a.length; i++) {            for (int j = 1; j < a[i].length; j++) {                if (c1[i - 1] == c2[j - 1]) {                    a[i][j]=a[i-1][j-1]+1;// 填空              //這個地方重點注意斜對角的二維數組 然後查看斜對角 那個更長                    if (a[i][j] > max)                        max = a[i][j];                }            }        }        return max;    }    public static void main(String[] args) {        int n = f("abcdekkk", "baabcdadabcd");        System.out.println(n);    }}

 

 

 

  

第八屆藍橋杯b組java第六題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.