Given an input string, not necessarily a palindrome, compute the number of swaps necessary to transform the string into a palindrome. By swap we mean reversing the order of two adjacent symbols (UVA 10716). I've found an AC solution with the following greedy algorithm (from UVA board): For each letter find it's first and last occurence in current string. Select letter for which sum of distances from its first occurence to beginning and from last one to the end is smallest. Swap its first occurence to beginning and last to the end. And eliminate them --> now you have the word which is shorter. You have to repeat until you have <=1 letters left.But I can't find the proof (and I can't construct it by myself). The question: how to prove the correctness of the algorithm?
Greedy algorithm correctness proof (UVA 10716)
algorithms;correctness proof;greedy algorithms