算法竞赛入门经典的java实现之最长回文子串->Demo20.java

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36737934/article/details/80192093

下面贴出源码:

package cn.zimo.algorithm;

import java.util.Scanner;

/**
 * 最长回文子串
 * @author 子墨
 * @date 2018年5月2日 下午4:35:00
 */
public class Demo20 {
    public static void main(String[] args) {
        String str=new Scanner(System.in).nextLine();
        for(int i=0;i<str.length();i++) {
            String sub=str.substring(i).toLowerCase().replaceAll("\\p{P}", "").replaceAll(" ", "");
            StringBuffer revarsesub=new StringBuffer(sub);
            if(sub.contentEquals(revarsesub.reverse())&&sub.length()>1) {
                String out=str.substring(i);
                if(str.substring(i).startsWith(":")) {
                    out=str.substring(i+1);
                }
                if(out.endsWith(".")) {
                    out=out.substring(0, out.length()-1);
                }
                System.out.println(out.trim());
                break;
            }
        }
    }
}


猜你喜欢

转载自blog.csdn.net/qq_36737934/article/details/80192093