Xâu đối xứng

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**
 *
 * @author Le Thi Tuyet
 */
public class Solution {

    /**
     * @param args the command line arguments
     */
   
    public static void main(String[] args) throws FileNotFoundException {
        // TODO code application logic here
        System.setIn(new FileInputStream("input.txt"));
        int testcase = 2;
       
        Scanner sc = new Scanner(System.in);
        for(int i=0; i<testcase; i++){
            String s = sc.nextLine();
            int n = s.length();
           
            int table[][] = new int[n][n];
           
            //1
            for(int h=0; h<n; h++) {
            table[h][h]=1;
            }
           
            //2
            int maxlen = 0;
            int start = 0;
            for(int h=0; h<n-1; h++) {
            if(s.charAt(h)==s.charAt(h+1)) {
            table[h][h+1]=1;
            maxlen=2;
            start = h;
            }
            }
            //System.out.println(start);
           
            //3
            for(int k=3; k<n; k++) {
            for(int h = 0; h<n-k+1; h++) {
                int c=h+k-1;
                if(s.charAt(h)==s.charAt(c) && table[h+1][c-1]==1) {
                table[h][c]=1;
                if(maxlen<k) {
                start=h;
                maxlen=k;
                }
                }
                }
            }
           // System.out.println(maxlen);
            System.out.println("#"+(i+1)+" "+s.substring(start, start+maxlen));
        }
    }
   
}

Nhận xét

Bài đăng phổ biến từ blog này

ASP.NET MVC 5- Ứng dụng quản lý sinh viên

Nhận dạng chữ cái viết tay

[Phần 3]: Controllers