palindrome sequence

Topic description

If a sequence of numbers is the same as the original sequence after inversion, such a sequence of numbers is called a palindrome sequence. For example:
{1, 2, 1}, {15, 78, 78, 15} , {112} are palindromic sequences, {1, 2, 2}, {15, 78, 87, 51} ,{112, 2 , 11} is not a palindrome sequence. Now given a sequence of numbers, a transformation operation is allowed: choose any two adjacent numbers, then remove the two numbers from the sequence, and insert the sum of the two numbers at the position before the two numbers ( Insert only one and ). Now ask for the minimum number of operations required for a given sequence to turn it into a palindrome.  




Enter description:

The input is two lines, the first line is the sequence length n ( 1 ≤ n ≤ 50) and the second line is the n integer item[i] (1 ≤ itam[i] ≤ 1000) in the sequence, separated by spaces.

Output description:

Output a number representing the minimum number of conversions required
Example 1

enter

4 1 1 1 3

output

2

1  import java.util.LinkedList;
 2  import java.util.Scanner;
 3  
4  /** 
5  * The number of palindromes to find the minimum number of operation steps
 6  * It is not possible to compare the two sides and replace the number operation inside, if they are equal, continue to compare The number in it until i is more than half 
 7  * @author Dell
 8  *
 9   */ 
10  public  class Main {
 11      
12  public  static  void main(String[] args) {
 13      Scanner sc = new Scanner(System.in);
 14      int n = sc.nextInt();
 15      LinkedList<Integer> list =new LinkedList<Integer>();
16     for (int i = 0; i < n; i++) {
17         list.add(sc.nextInt());
18     }
19     int step = 0;
20     int i =0; 
21     while(i<=(list.size()/2)) {
22         int j = list.size()-1-i;
23         if (list.get(i)<list.get(j)) {
24             list.set(i,list.get(i)+list.get(i+1));
25             list.remove(i+1);
26             step++;
27         }else if (list.get(i)>list.get(j)) {
28             list.set(j,list.get(j)+list.get(j-1));
29             list.remove(j-1);
30             step++;
31         }else {
32             i++;
33         }
34     }
35     System.out.println(step);
36 }
37 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325121378&siteId=291194637