Lintcode-longest Common Substring

Source: Internet
Author: User
Tags lintcode

Given, strings, find the longest common substring.

Return the length of it.

Note

The characters in substring should occur continiously in original string. This was different with subsequnce.

Solution:

1  Public classSolution {2     /**3      * @paramA, b:two string.4      * @return: The length of the longest common substring.5      */6      Public intlongestcommonsubstring (String A, String B) {7         intLenA =a.length ();8         intLenB =b.length ();9         if(lena==0 | | LenB ==0)return0;Ten  One         int[] LCS =New int[Lena+1] [Lenb+1]; A          for(inti=0;i<=lena;i++) Lcs[i][0] = 0; -          for(inti=0;i<=lenb;i++) Lcs[0][i] = 0; -  the         intres = 0; -          for(inti=1;i<=lena;i++) -              for(intj=1;j<=lenb;j++) -                 if(A.charat (i-1) ==b.charat (j-1)){ +Lcs[i][j]=lcs[i-1][j-1]+1; -                     if(lcs[i][j]>res) res =Lcs[i][j]; +}ElseLcs[i][j]=0; A  at         returnRes; -     } -}

Lintcode-longest Common Substring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.