Perfect cost (greedy algorithm)

题目描述
回文串,是一种特殊的字符串,它从左往右读和从右往左读是一样的。小龙龙认为回文串才是完美的。现在给你一个串,它不一定是回文的,请你计算最少的交换次数使得该串变成一个完美的回文串。

交换的定义是:交换两个相邻的字符

例如mamad

第一次交换 ad : mamda

第二次交换 md : madma

第三次交换 ma : madam (回文!完美!)

输入
第一行是一个整数N,表示接下来的字符串的长度(N < = 8000)

第二行是一个字符串,长度为N.只包含小写字母
输出
如果可能,输出最少的交换次数。

否则输出Impossible
样例输入
5

mamad
样例输出
3

Knowledge points: greedy algorithm
1, greedy algorithm (also known as greedy algorithm) means that when solving problems, always make the best choice in the current view. That is to say, without considering the overall optimality, what he makes is only a partial optimal solution in a certain sense. The greedy algorithm does not obtain the overall optimal solution for all problems, but it can produce the overall optimal solution or the approximate solution of the overall optimal solution for many problems in a wide range.
2. Features
Most of the problems that greedy algorithms can solve have the following features:

⑴ There is a problem solved in an optimal way. In order to construct a solution to the problem, there is a collection of candidate objects: for example coins of different denominations.

⑵ As the algorithm progresses, two other sets will be accumulated: one contains candidates that have been considered and selected, and the other contains candidates that have been considered but discarded.

⑶ There is a function to check whether a set of candidate objects provides an answer to the question. This function does not consider whether the solution at this time is optimal.

⑷ There is also a function to check whether a set of candidate objects is feasible, that is, whether it is possible to add more candidate objects to the set to obtain a solution. As with the previous function, the optimality of the solution is not considered at this time.

⑸ The selection function can indicate which remaining candidate is the most promising solution to the problem.

⑹ Finally, the objective function gives the value of the solution.
3. Basic idea
⒈) Establish a mathematical model to describe the problem.
⒉) Divide the solved problem into several sub-problems.
⒊) Solve each sub-problem to get the local optimal solution of the sub-problem.
⒋) Combine the local optimal solution of the sub-problem into a solution of the original solution problem.

public class Daijia {
	public static void main(String[] args) throws IOException{
		Scanner scanner = new Scanner(System.in);
	        int len = scanner.nextInt();
	        char[] s = scanner.next().toCharArray();
	        if (palindrome(s, 0, len - 1)) 
	        {
	            System.out.println(cnt);
	        } 
	        else 
	        {
	            System.out.println("Impossible");
	        }
	    }
	     
	    private static int cnt = 0;
	    private static boolean haveMiddle = false;
	     
	    private static boolean palindrome(char[] s, int a, int b) 
	    {
	        if (b <= a) 
	        {
	            return true;
	        }
	                // 从最后的位置开始遍历字符串
	        for (int i = b; i > a; i--) 
	        {
	            if (s[a] == s[i]) 
	            {
	                exchangeTo(s, i, b);
	                cnt += getExchangeTimes(i, b);
	                return palindrome(s, a + 1, b - 1);
	            }
	        }
	                // 如果没有出现过中间字符
	        if (!haveMiddle) 
	        {
	            haveMiddle = true;
	            cnt += getExchangeTimes(a, s.length / 2);
	            return palindrome(s, a + 1, b);
	        }
	        return false;
	    }
	     
	    private static int getExchangeTimes(int a, int b) 
	    {
	        return b - a;
	    }
	     //交换从0位置开始,找到的第一个与0位置数,相同的 到最后一个
	    //交换从1位置开始,找到的第一个与1位置数,相同的 到最后一个 +1;
	    //if (palindrome(s, 0, len - 1)) 
	    //return palindrome(s, a + 1, b - 1);
	    private static void exchangeTo(char[] s, int a, int b) 
	    {
	        char temp = s[a];
	        for (int i = a; i < b; i++) 
	        {
	            s[i] = s[i + 1];
	        }
	        s[b] = temp;
        }         
}


Published 44 original articles · Likes2 · Visits 540

Guess you like

Origin blog.csdn.net/qq_43699776/article/details/103376564