Palindromic sequence is determined recursively
Ideas:
① First, if the empty string, a character , it is determined palindromic sequence
② not satisfied ①, then we need to string a recursive judgment, we thought to use the first character and the last character comparison, if the same is the second and penultimate compare, so the cycle , there will be an end
③, two things occur at the end, the first case: two different positions of characters are compared, the second case is: the character of the two identical positions compared. So we need to find the middle value , we pass sub parity manner, calculates the final left subscript numbers to be compared, and if it is odd, the left subscript of: len / 2 ; if for the even-numbered subscripts, is left : len / 2 ;
With recursive end condition ④, then the recursion conditions are: when the first character is the same, it is recursive: index +1 left, right subscript -1 if not met, then exit.
Code:
package com.java1; import java.util.Scanner; public class test { static String s=new String(); static int len=0; static int k; public static void f(int i,int j) { if(i==k&&s.charAt(i)==s.charAt(j))//判断结束的条件 { System.out.println("YES"); return; } if(s.charAt(i)==s.charAt(j))//If yes the next step is performed recursively F (I +. 1,. 1-J ); the else // not satisfied, the output { System.out.println ( "NO" ); return ; } } public static void main (String [ ] args) { // the TODO Auto-Generated Method Stub Scanner in = new new Scanner (the System.in); S = in.nextLine (); int K = 0 ; len = s.length (); // Get string length IF (len% 2 == 0) // to find the intermediate value by the number of parity K = len / 2-1; //Find the middle subscript the else K = len / 2; // find the middle subscript IF (len len == == 0 ||. 1) // If an empty string or only a character System.out.println ( "OK" ); the else { F ( 0,. 1-len); // recursive computation } } }