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

…不知道为什么第一题就出现奇怪的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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值