您的位置:首页 > 其它

Hihocoder#1039 : 字符消除 解题记录

2016-07-07 15:21 316 查看
…不知道为什么第一题就出现奇怪的WA….

字符消除 这里是题目的传送门…

感觉就是输入一个字符数组str…然后new 一个字符数组(长度str.length+1),

再根据每个字符增殖一下 例如 ABC → ABBC

..然后再一个循环消除 长度超过1的由相同字母组成的子串 而已。

列出最优解ABCBCCCABABABA
10;ABCCBCCC0;ABABBABA
25;ABB2;ABAABA
37;A4;ABBA
47;A6;AA
57;A8;
结果为: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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: