5 Longest Palindromic Substring

Given a string S, find the longest palindromic(adj. 回文的;复发的) substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.

Tags:String

提交不成功,超时了。
还是先好好看看算法和数据结构的书吧。(┬_┬)

public class No5 {

    public static void main(String[] args) {

        double data1=System.currentTimeMillis();

        String str = "cyyoacmjwjubfkzrrbvquqkwhsxvmytmjvbborrtoiyotobzjmohpadfrvmx"
                + "uagbdczsjuekjrmcwyaovpiogspbslcppxojgbfxhtsxmecgqjfuvahzpgprscj"
                + "wwutwoiksegfreortttdotgxbfkisyakejihfjnrdngkwjxeituomuhmeiescty"
                + "whryqtjimwjadhhymydlsmcpycfdzrjhstxddvoqprrjufvihjcsoseltpyuayw"
                + "giocfodtylluuikkqkbrdxgjhrqiselmwnpdzdmpsvbfimnoulayqgdiavdgeii"
                + "layrafxlgxxtoqskmtixhbyjikfmsmxwribfzeffccczwdwukubopsoxliagenz"
                + "wkbiveiajfirzvngverrbcwqmryvckvhpiioccmaqoxgmbwenyeyhzhliusupmr"
                + "gmrcvwmdnniipvztmtklihobbekkgeopgwipihadswbqhzyxqsdgekazdtnamwz"
                + "bitwfwezhhqznipalmomanbyezapgpxtjhudlcsfqondoiojkqadacnhcgwkhax"
                + "mttfebqelkjfigglxjfqegxpcawhpihrxydprdgavxjygfhgpcylpvsfcizkfbq"
                + "zdnmxdgsjcekvrhesykldgptbeasktkasyuevtxrcrxmiylrlclocldmiwhuizh"
                + "uaiophykxskufgjbmcmzpogpmyerzovzhqusxzrjcwgsdpcienkizutedcwrmow"
                + "wolekockvyukyvmeidhjvbkoortjbemevrsquwnjoaikhbkycvvcscyamffbjyv"
                + "kqkyeavtlkxyrrnsmqohyyqxzgtjdavgwpsgpjhqzttukynonbnnkuqfxgaatpi"
                + "lrrxhcqhfyyextrvqzktcrtrsbimuokxqtsbfkrgoiznhiysfhzspkpvrhtewth"
                + "pbafmzgchqpgfsuiddjkhnwchpleibavgmuivfiorpteflholmnxdwewj";

        String s = longestPalindrome(str);

        System.out.println(s);

        double data2=System.currentTimeMillis();
        System.out.println(data2-data1);    //执行时间;
    }

    public static String longestPalindrome(String s) {

        int length = s.length();

        // 判断整体的字符串本身是不是一个回文;
        if (isPal(s).equals(s))
            return s;

        for (int i = length - 1; i >= 2; i--) { // i表示取得自字符串的长度;
            for (int j = 0; j <= length - i; j++) { // j表示从第下标j开始取;
                String subStr = s.substring(j, j + i); // 因为取子字符串时取得是[m,n);

                // System.out.println(subStr); //测试用,输出所有的自字符串;
                if (isPal(subStr).equals(subStr))
                    return subStr;
            }
        }
        return s.charAt(0)+"";
    }

    // 判断是否是回文,是就返回字符串,否则返回no;
        public static String isPal(String s) {
            int length = s.length();
// char[] arr = s.toCharArray();
            boolean breakout = false;// 用于标识是否是break跳出循环的;

            // 判断整体字符串是否是一个大回文;
            for (int i = 0; i < length / 2; i++) {
                if (s.charAt(i) != s.charAt(length - 1 - i)) {
                    breakout = true;
                    break;
                }
            }
            if (breakout == false)
                return s;
            else
                return "no";
        }
}

你可能感兴趣的:(5 Longest Palindromic Substring)