hihoCoder 字符消除

题目地址:http://hihocoder.com/problemset/problem/1039

1039 : 字符消除

时间限制:1000ms
单点时限:1000ms
内存限制:256MB
描述
小Hi最近在玩一个字符消除游戏。给定一个只包含大写字母”ABC”的字符串s,消除过程是如下进行的:

1)如果s包含长度超过1的由相同字母组成的子串,那么这些子串会被同时消除,余下的子串拼成新的字符串。例如”ABCCBCCCAA”中”CC”,”CCC”和”AA”会被同时消除,余下”AB”和”B”拼成新的字符串”ABB”。

2)上述消除会反复一轮一轮进行,直到新的字符串不包含相邻的相同字符为止。例如”ABCCBCCCAA”经过一轮消除得到”ABB”,再经过一轮消除得到”A”

游戏中的每一关小Hi都会面对一个字符串s。在消除开始前小Hi有机会在s中任意位置(第一个字符之前、最后一个字符之后以及相邻两个字符之间)插入任意一个字符(‘A’,’B’或者’C’),得到字符串t。t经过一系列消除后,小Hi的得分是消除掉的字符的总数。

请帮助小Hi计算要如何插入字符,才能获得最高得分。

输入
输入第一行是一个整数T(1<=T<=100),代表测试数据的数量。

之后T行每行一个由’A”B”C’组成的字符串s,长度不超过100。

输出
对于每一行输入的字符串,输出小Hi最高能得到的分数。

提示
第一组数据:在”ABCBCCCAA”的第2个字符后插入’C’得到”ABCCBCCCAA”,消除后得到”A”,总共消除9个字符(包括插入的’C’)。

第二组数据:”AAA”插入’A’得到”AAAA”,消除后得到”“,总共消除4个字符。

第三组数据:无论是插入字符后得到”AABC”,”ABBC”还是”ABCC”都最多消除2个字符。

样例输入
3
ABCBCCCAA
AAA
ABC
样例输出
9
4
2

我的思路:
我采用的是简单的暴力遍历,插入3种字符,插入所有可能位置,从而获取所有可能分数,并从中取出做大分数作为结果。

我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace test1039
{
class Program
{
// hihoCoder题库
// #1039:字符消除
// http://hihocoder.com/problemset/problem/1039
// 2015-07-22 : AC

    static void Main(string[] args)
    {
        string readline = "";
        int inputLines = 0;
        while ((readline = Console.ReadLine()) != null)
        {
            //Porgram start here
            inputLines = Convert.ToInt32(readline);
            string[] readlines = new string[inputLines];
            for (int i = 0; i < inputLines; i++)
            {
                readlines[i] = Console.ReadLine();
            }
            for (int i = 0; i < readlines.Length; i++)
            {
                Console.WriteLine(searchHighestScore(readlines[i]).ToString());
            }
        }
        Console.ReadKey();
    }

    /// <summary>
    /// 将输入字符串从尾到从遍历
    /// 当存在相邻字符重复的,删除重复的字符
    /// 同时得分增与删除字符相同分数
    /// </summary>
    /// <param name="str">待遍历字符串</param>
    /// <param name="score">得分</param>
    /// <returns>true:有字符被删除</returns>
    static bool deleteFromLast(ref List<char> str, ref int score)
    {
        int count = 0;
        bool delete = false;
        if (str.Count <= 1) return delete;
        for (int index = str.Count - 1; index > 0; index--)
        {
            if (str[index] == str[index - 1])
            {
                count = 2;
                while (index - count >= 0 && str[index] == str[index - count]) count++;
                str.RemoveRange(index + 1 - count, count);
                score += count;
                index = index + 1 - count;
                delete = true;
            }
        }
        return delete;
    }


    static int searchHighestScore(string str)
    {
        char[] compairedChar = new char[3] { 'A', 'B', 'C' };
        List<char> temp = str.ToList();
        int maxScore = 0;
        int currentScore = 0;

        for (int charIndex = 0; charIndex < compairedChar.Length; charIndex++)
        {
            for (int i = 0; i < str.Length; i++)
            {
                temp = str.ToList();
                currentScore = 0;
                temp.Insert(i, compairedChar[charIndex]);
                while (deleteFromLast(ref temp, ref currentScore)) ;
                if (currentScore > maxScore) maxScore = currentScore;
            }
        }
        return maxScore;
    }
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值