Description

小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计算要如何插入字符,才能获得最高得分。

Sample Input

3
ABCBCCCAA
AAA
ABC

Sample Output

9
4
2 自己写的代码,运行AC。
 #include <iostream>
#include <string>
#include <stdio.h>
using namespace std; int EatLetter(char* word,char* newWord);
int EliminateWord(char* word);
void InputLetter(char* word,char* newWord,char c,int loc);
int StartWork(char* word); int main()
{
/*
freopen("data.txt","r",stdin);
freopen("out.txt","w",stdout);
*/ int round;
cin>>round;
for(int i=;i<round;i++)
{
char* k=new char[];
cin>>k;
cout<<StartWork(k)<<endl;
delete[] k;
} /*
fclose(stdin);
fclose(stdout);
*/
} int EatLetter(char* word,char* newWord)
{
char temp;
int index=;
int len=strlen(word);
for(int i=;i<len;i++)
{
temp=word[i];
bool isSingle=true;
while(temp==word[i+]&&i<len)
{
i++;
isSingle=false;
}
if(isSingle)
newWord[index++]=temp;
}
newWord[index]='\0';
return len-index;
} int EliminateWord(char* word)
{
int count=;
int m=;
while(m=EatLetter(word,word))
{
count+=m;
}
return count;
} void InputLetter(char* word,char* newWord,char c,int loc)
{
int len=strlen(word);
int index=;
for(int i=;i<len+;i++)
{
if(i==loc)
newWord[index++]=c;
newWord[index++]=word[i];
}
} int StartWork(char* word)
{
int count=;
char* newWord=new char[strlen(word)+];
for(int i=;i<strlen(word)+;i++)
{
for(int j='A';j<='C';j++)
{
InputLetter(word,newWord,(char)j,i);
int temp=EliminateWord(newWord);
count=count>temp?count:temp;
}
}
return count;
}

[hihoCoder]#1039 : 字符消除的更多相关文章

  1. hihoCoder 1039:字符消除(字符串处理)

    #1039 : 字符消除 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi最近在玩一个字符消除游戏.给定一个只包含大写字母"ABC"的字符串s,消 ...

  2. hihocoder #1039 : 字符消除 ( 字符串处理类 ) 好久之前做的题目,具体的算法代码中阅读吧

    #1039 : 字符消除 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi最近在玩一个字符消除游戏.给定一个只包含大写字母"ABC"的字符串s,消 ...

  3. hihoCoder 1039 字符消除 最详细的解题报告

    题目来源:字符消除 解题思路: 1.在给定字符串中的任意位置插入'A'.'B'.'C'中的任意一个字符,然后计算插入后的字符经过消除后最短的字符串长度: 2.在计算字符消除后最短长度时,智能一遍一遍的 ...

  4. hihoCoder 1039字符消除 (字符串处理)

    http://hihocoder.com/problemset/problem/1039 因为字符串只由3种字母组成,并且插入的字符也只能是这三种字符的其中一个,那么可以考虑枚举这三个字符其中一个字符 ...

  5. 【hihoCoder】#1039 : 字符消除 by C solution

    #1039 : 字符消除 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi最近在玩一个字符消除游戏.给定一个只包含大写字母"ABC"的字符串s,消 ...

  6. 【hihoCoder】1039 : 字符消除

    题目:http://hihocoder.com/problemset/problem/1039 给定一个字符串s,只包含'A', 'B', 'C'三种字符 1. 向 s 的任意位置 (包括头和尾) 中 ...

  7. 【hihocoder 1039 字符串消除】模拟

    题目链接:http://hihocoder.com/problemset/problem/1039 题意:给定一个只由{A, B, C}组成的字符串s,长度为n, 故包含n+1个空隙:现要求在某个空隙 ...

  8. 【HIHOCODER 1039】 字符消除

    链接 问题描述 小Hi最近在玩一个字符消除游戏.给定一个只包含大写字母"ABC"的字符串s,消除过程是如下进行的: 1)如果s包含长度超过1的由相同字母组成的子串,那么这些子串会被 ...

  9. hihoCoder#1039

    刚开始学习C语言,准备在做hiho的题目的过程中来学习,在此进行记录,如果代码中有错误或者不当的地方还请指正. 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi最近在 ...

随机推荐

  1. Hdu 1521 排列组合

    a1 n1 a2 n2 ... ak nkn=n1+n2+...+nk从n个数中选r个排列(不是组合噢)// 指数型母函数// 模板#include <iostream> #include ...

  2. 四 AndEngine 画线

    package com.example.AndEngineExample02; import org.anddev.andengine.engine.Engine;import org.anddev. ...

  3. How do I list all tables/indices contained in an SQLite database

    How do I list all tables/indices contained in an SQLite database If you are running the sqlite3 comm ...

  4. DOS攻击和DDOS攻击有啥区别啊

    DDOS是DOS攻击中的一种方法. DoS:是Denial of Service的简称,即拒绝服务,不是DOS操作系统,造成DoS的攻击行为被称为DoS攻击,其目的是使计算机或网络无法提供正常的服务. ...

  5. 数学概念 z

    数学是很难的科学,但因为它是科学家用数学来解释宇宙的语言,我们无可避免的要学习它.看看下面的这些 GIF 动图,它们提供了视觉的方式来帮助你理解各种数学技巧. 1.椭圆的画法 2.杨辉三角问题(Pas ...

  6. 跨平台音乐播放器qmmp(Cross-Platform Audio Player Qmmp)

    开源多媒体播放器(Audio-Player),简洁的界面,最看重它的是跨平台的特性. 开源 跨平台(Linux.Windows) 简洁 支持音乐格式(mp3/ogg......) 全局快捷键 播放中打 ...

  7. Spinlock

    Spinlock From Wikipedia, the free encyclopedia This article needs additional citations for verificat ...

  8. MySQL 5.6 警告信息 command line interface can be insecure 修复

    在命令行输入密码,就会提示这些安全警告信息. Warning: Using a password on the command line interface can be insecure.   注: ...

  9. Who is the best at Dataset X?

    推荐一个关于分类.目标检测.姿态估计的数据集收藏的网页. Did you ever want to quickly learn?which paper provides the best result ...

  10. brew 更新

    更新: brew update brew update —system 安装, 如:brew install unrar 卸载, 如:brew uninstall unrar