平方串,利用最大公共子序列的思想

https://www.nowcoder.com/practice/b43fb39898f448e39adbcffde5ff0dfc?tpId=90&tqId=30810&tPage=2&rp=2&ru=%2Fta%2F2018test&qru=%2Fta%2F2018test%2Fquestion-ranking

分割求最大子串

public class Main1 {
    public static void main(String[] args) {    
        Scanner scan=new Scanner(System.in);
        String str=scan.nextLine();
        int res=0;
        for(int i=1;i<str.length();i++) {
            res=Math.max(res, LCS(str.substring(0, i),str.substring(i)));
        }
        System.out.println(res);
    }
    public static int LCS(String str1,String str2) {
        int n=str1.length(),m=str2.length();
        int[][] dp=new int[n+1][m+1];
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++) {
                if(str1.charAt(i-1)==str2.charAt(j-1))
                    dp[i][j] = dp[i-1][j-1]+1;
                else
                    dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
            }
        }
        return dp[n][m]*2;
    }
}

 

posted @ 2019-05-19 11:01  LeeJuly  阅读(137)  评论(0)    收藏  举报