import java.io.BufferedInputStream;
import java.util.Scanner;
public class Main {
private static Scanner cin;
public static int Check(String s) {
StringBuffer sb = new StringBuffer();
sb.append(s);
sb.reverse();
String s1 = sb.toString();
int n = s.length();
int [][] in = new int[n+1][n+1];
for (int i=1;i<n+1;i++) {
for(int j = 1;j<n+1;j++) {
if(s.charAt(i-1) == s1.charAt(j-1)) {
in[i][j] = in[i-1][j-1]+1;
}
else{
in[i][j]=Math.max(in[i-1][j],in[i][j-1]);
}
}
}
return in[n][n];
}
public static void main(String[] args) {
cin = new Scanner(new BufferedInputStream(System.in));
while(cin.hasNext()) {
String s = cin.nextLine();
System.out.println(s.length()-Check(s));
}
}
}
- 小Q最近遇到了一个难题:把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,且不能申请额外的空间。你能帮帮小Q吗?
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean check = false;
while(sc.hasNext()){
String s = sc.nextLine();
char[] c =s.toCharArray();
for(int i=s.length()-1;i>=0;i--){
check=false;
if(Character.isLowerCase(c[i])){
for(int j=i-1;j>=0;j--){
if(Character.isUpperCase(c[j])){
char temp = c[j];
for(int k=j;k<i;k++){
c[k]=c[k+1];
}
c[i] = temp;
check=true;
}
if(check){
break;
}
}
}
}
System.out.println(String.valueOf(c));
}
}
}