table of Contents
Foreword
I have been interviewing recently. Many of the written code are too lazy to post to the blog. It is now filled in, but there may be fewer comments.
The basic cost of practice exercises
Resource limit
Time limit: 1.0s Memory limit: 512.0MB
Problem description
Palindrome is a special kind of character string. It reads from left to right and right to left. Xiao Longlong believes that the palindrome is perfect. Now give you a string, it is not necessarily a palindrome, please calculate the minimum number of exchanges so that the string becomes a perfect palindrome string.
The definition of exchange is: exchange two adjacent characters.
For example, mamad
exchanges ad for the first time ad: mamda
exchanges for the second time md: madma
exchanges for the third time ma: madam (palindrome! Perfect!)
Input format The
first line is an integer N, indicating the length of the next string (N <= 8000) The
second line is a string, the length is N. Only lowercase letters are included
Output format
If possible, output the minimum number of exchanges.
Otherwise output Impossible
Sample input
5
mamad
Sample output
3
Code for this question (JAVA)
import java.util.Scanner;
public class ThePriceOfPerfection3 {
private static int count = 0;
private static boolean haveMiddle = false;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
String str = sc.next();
char[] charArr = str.toCharArray();//将输入字符串转换为char数组
if (palindrome(charArr, 0, N - 1)) {
System.out.println(count);
} else {
System.out.println("Impossible");
}
}
private static boolean palindrome(char[] charArr, int a, int b) {
if (b <= a) {
return true;
}
// 从最后的位置开始遍历字符串
for (int i = b; i > a; i--) {
if (charArr[a] == charArr[i]) {
exchangeTo(charArr, i, b);
count += b - i;
return palindrome(charArr, a + 1, b - 1);
}
}
// 如果没有出现过中间字符
if (!haveMiddle) {
haveMiddle = true;
count += charArr.length / 2 - a;
return palindrome(charArr, a + 1, b);
}
return false;
}
private static void exchangeTo(char[] charArr, int a, int b) {
//字符交换方法1
for (int i = a; i < b; i++) {
char tmp = charArr[i];
charArr[i] = charArr[i + 1];
charArr[i + 1] = tmp;
}
/*交换字母方法2
char temp = charArr[a];
for (int i = a; i < b; i++) {
charArr[i] = charArr[i + 1];
}
// if (b - a >= 0) System.arraycopy(charArr, a + 1, charArr, a, b - a);
charArr[b] = temp;*/
}
}
Code for this question (Python)
def move(l, a, b):
buf = l[a]
if b > a:
l[a:b] = l[a + 1:b + 1]
else:
l[b + 1:a + 1] = l[b:a]
l[b] = buf
n = eval(input())
s = input()
cl = []
side = []
for c in s:
have = False
for i in range(len(cl)):
if cl[i] == c:
side.append(c)
del cl[i]
have = True
break
if not have:
cl.append(c)
if len(cl) > 1:
print("Impossible")
else:
s = list(s)
num = 0
mid = False
for i in range(int(n / 2)):
if mid:
move(s, n - 1 - i, n - 2 - i)
num += 1
if s[i] != s[n - 1 - i]:
omid = True
for j in range(i + 1, n - i - 1):
if s[j] == s[n - 1 - i]:
omid = False
move(s, j, i)
num += j - i
break
if omid and not mid:
mid = True
move(s, n - 1 - i, n - 2 - i)
num += 1
for j in range(i + 1, n - i - 1):
if s[j] == s[n - 1 - i]:
move(s, j, i)
num += j - i
break
print(num)