One will see - Blue Bridge Cup questions basic exercises perfect price - greedy method, 21 lines of code AC

Definition of greedy algorithm:

Greedy algorithm means that when problem solving, always made in the current appears to be the best choice. In other words, not be considered as a whole the best, only make sense in the local optimal solution. Greedy algorithm is not able to get the best overall solution to all the problems, the key is to choose the greedy strategy, greedy strategy chosen must have no after-effect, namely, a state before the process will not affect the state's future, only the current state related.

Greedy in the application of this title:

For this question, the application of the greedy algorithm idea is: each change, both to ensure that the standard answer from one step closer. For example: aabbm string after step (step might contain a number of operations) after the greedy, becomes: abbma, this is considered a success greedy .

Ideas:

1, the rules of this question is: Each character can only be exchanged with the adjacent characters . (Initially I thought I could jump exchange, So now they find wrong. Cry o (╥﹏╥) o)

2, the beginning of the idea of:
traversing the first half of each character string, if the character is the opposite is not equal to it, continue to traverse to find the equivalent characters into the opposite, increasing the number of steps corresponding to NUM.
If you do not find a character string and is an even number of values, it fails, and if you do not find surprising, it is determined whether it is the first time this happens, and if yes, this character into the middle of the string. If not, it fails. Final output num (because a palindrome is not possible to have a series of two or more characters appear too ).
But in the process of debugging, found a bug, if found early is only a character in a string, the string and move to the middle, but the other characters in the process of moving the match, this character will lead to displacement .
Such as: ffdejjellThe first step must be to greedy f moved to second last, it becomes: fdejjellf. But when traversed d, according to the original idea, it needs to be in the middle . Like fejjdellfthis: . But when we traverse the subsequent location j due to a need for a move, it will not result in intermediate d, the original operation for nothing.
The solution is: d in situ does not move, the subsequent traversal characters to be a match after their symmetrical position. Such as: fdejjllef, e is is a match. After all traverse, it becomes: fdejlljefFinally, the d move three times, becomes: fejldljef. carry out.

Code:

#include<iostream>
using namespace std;
int main() {
	int i, j, n, ans=0, flag = 0;
	string a;
	cin >> n >> a;
	for(i = 0; i < n; i++) {			  	//i从前往后遍历 
		for(j = n-1; j >= i; j--) {			//j从后往前遍历 
			if(i == j) {					//如果没找到可以匹配的。 
				if(a.size()%2==0 || flag==1) { cout <<"Impossible"; return 0; }
				flag++; ans+= a.size()/2-i;
			} else if(a[i]==a[j]) {			//如果找到了。 
				for(int k = j; k < (n-1); k++)
					swap(a[k], a[k+1]);
				ans += n-1-j;
				n--;  break; 	//break一定放在所有语句后面。反之则它后面的语句全部失效。
			}
		}
	} cout << ans; 
	return 0;
}
Published 73 original articles · won praise 61 · views 4750

Guess you like

Origin blog.csdn.net/weixin_43899069/article/details/104599762