開発メモ

開発用のメモです。

最短の回文を検索する。

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        try (
            InputStreamReader ir = new InputStreamReader(System.in);
            BufferedReader in = new BufferedReader(ir)
            ) 
        {
            String line= null;
            while ((line = in.readLine()) != null) {
                System.out.println();
                System.out.println("TARGET:" + line);
                if (line.length() < 2) {
                    System.out.println("LENGTH ERROR");
                    continue;
                }
                int x = line.length();
                int min = x+1;
                
             length:
                for (int l = 2; l <= x; l++) {
                    for (int s = 0; s <= x - l; s++) {
                        String t = line.substring(s, s+l);
                        StringBuffer b1 = new StringBuffer(t);
                        StringBuffer b2 = new StringBuffer(t);
                        b2.reverse();
                        
                        if (Objects.equals(b1.toString(), b2.toString())) {
                            System.out.printf("L:%2d,S:%2d,A:%2d,T:%s %n", l, s, x, t);
                            min = x;
                            break length;                           
                        }
                    }
                }
            }
        }
    }
}
Twitter: @asahina_alice