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