2 条题解

  • 0
    @ 2024-1-7 13:30:36

    从右边枚举找到最短的回文串

    import java.io.*;
    
    public class Main {
        static BufferedReader bw = new BufferedReader(new InputStreamReader(System.in));
        static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        public static void main(String[] args) throws Exception{
            String str = bw.readLine();
           if(str.length() == 1) System.out.println(str);
           else {
               for (int i = str.length() - 1; i >= 0; i--) {
                   if(aaa(str, i)){
                       pw.print(new StringBuffer(str.substring(i + 1)).reverse());
                       break;
                   }
               }
               pw.println(str);
               pw.flush();
           }
    
        }
        public static boolean aaa(String str, int i){
            for (int j = 0; j <= i / 2; j++) {
                if(str.charAt(j) != str.charAt(i - j)) return false;
            }
            return true;
        }
    }
    

    信息

    ID
    591
    时间
    1000ms
    内存
    256MiB
    难度
    9
    标签
    (无)
    递交数
    172
    已通过
    16
    上传者