…不知道为什么第一题就出现奇怪的WA….
字符消除 这里是题目的传送门…
感觉就是输入一个字符数组str…然后new 一个字符数组(长度str.length+1),
再根据每个字符增殖一下 例如 ABC → ABBC
..然后再一个循环消除 长度超过1的由相同字母组成的子串 而已。
列出最优解 | ABCBCCC | ABABABA |
---|---|---|
1 | 0;ABCCBCCC | 0;ABABBABA |
2 | 5;ABB | 2;ABAABA |
3 | 7;A | 4;ABBA |
4 | 7;A | 6;AA |
5 | 7;A | 8; |
结果为:7 | 结果为:8 |
这里是代码…WA的..测试了很多组数据都发现不了错误…尴尬…最近越来越水了么…
import java.util.Scanner;
public class _2_1039 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
char[] str;
for(int i = 0 ; i<n ; i++){
str = input.next().toCharArray();
if(str.length == 0)
{
System.out.println(0);
continue;
}
byte state;
int index = 0;
char[] new_str = new char[str.length+1];
int[] res = new int[str.length];
for(int j = 0 ; j < str.length ; j++ ){
if( str[j] == 'D' ){
continue;
}else{
state = 0;
for(int q = 0; q<new_str.length ;q++){
if(q == j){
new_str[j] = str[j];
new_str[j+1] = str[j];
state = 1;
q++;
continue;
}
new_str[q] = str[q-state];
}
res[index++] = run(new_str);;
}
}
//求最大值
int max = -1;
for(int a : res)
if(a > max)
max = a;
System.out.println(max);
}
}
public static int run( char[] str ){
int temp = -1;
int count = 0;
int per_index;
int per = 'D';
while(temp != 0){
temp = 0;
per = 'D';
per_index = -1;
for(int j = 0 ; j<str.length ; j++ ){
if(str[j] == 'D'){
continue;
}else if(str[j] == per){
temp++;
str[j] = 'D';
count++;
if(per_index != -1 && str[per_index] != 'D'){
str[per_index] = 'D';
count++;
temp++;
per_index = -1;
}
}else{
per = str[j];
per_index = j;
}
}
}
return count;
}
}