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;
}

最新文章

  1. flex 布局 初次接触这个好使又不是特别好用的布局方法
  2. Frameset的使用
  3. hdu 2896 AC自动机
  4. Castle ActiveRecord框架学习(一)
  5. Linux Centos7下安装Python
  6. PO_PO系列 - 收货管理分析(案例)
  7. 宽度的100%和auto的区别
  8. 感知器算法PLA
  9. chroot
  10. 提供一段Excel获取Title的标题,类似于A、AA、AAA,我们操作Excel的时候通常根据次标题来获取一定的操作范围。
  11. 让win7安装时出现版本选项
  12. CentOS下安装php的mbstring扩展
  13. initWithFrame方法的使用
  14. 渗透测试的理论部分4——开放式Web应用程序安全项目
  15. BrainWeb: Simulated Brain Database使用说明
  16. MySQL命令学习
  17. 2014西安赛区C题
  18. js中const、let、var的区别
  19. hdu 5772 String problem 最大权闭合子图
  20. a new way of thinking about a problem

热门文章

  1. 【英语】Bingo口语笔记(67) - turn系列
  2. Edit Control的Enter响应函数
  3. 数据结构——Java实现单向链表
  4. 基于catalog 创建RMAN存储脚本
  5. hadoop完全分布式模式的安装和配置
  6. 清除Xcode缓存和存档文件
  7. oracle 体系结构解析
  8. c# 运行时替换某文件源代码(将XML 转换成 某个枚举并写入源文件)
  9. jszs 枚举算法
  10. OperateParticleWithCodes