计科院19软工范征远


私信TA

用户名:1954418428

访问量:1144

签 名:

等  级
排  名 20915
经  验 634
参赛次数 1
文章发表 8
年  龄 0
在职情况 学生
学  校 怀化学院
专  业

  自我简介:

解题思路: 直接从最长子串遍历到最短子串,第一个回文子串必为最长回文。
参考代码:

import java.util.Scanner;
/**
 * @author fzy
 * @create 2021/10/9 22:25
 **/
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String str;
        while(sc.hasNext()){
            str=sc.next();
            if(str.length()==1){
                System.out.println(1);
            }else{
                System.out.println(func(str));
            }
        }
    }

    public static  int  func(String str){
       int max=0;
       int left=0,right=str.length()-1;
       int jl=right-left;//下标距离

       while (left<=right){
           while (right< str.length()){
               String s1 = str.substring(left, right+1);//子串
//               System.out.println("子串:"+s1);
               String s2 = new StringBuffer(s1).reverse().toString();//反转子串
               if (s1.equals(s2)){
                   return s1.length();
               }
               left++;
               right++;
           }
           jl--;
           left=0;
           right=left+jl;
       }
       return 0;
    }
}


 

0.0分

2 人评分

  评论区

有些地方改了代码 忘记删注释了
2021-10-10 12:00:22
  • «
  • 1
  • »