Crusaders Quest
Time Limit: 1 Second Memory Limit: 65536 KB
Crusaders Quest is an interesting mobile game. A mysterious witch has brought great darkness to the game world, and the only hope for your kingdom is to save the Goddesses so that they can unleash their power to fight against the witch.
In order to save the game world, you need to choose three heroes to fight for victory and use their skills wisely. Nine skill blocks of three different types (three blocks per type) will be presented at the bottom of the screen. If () consecutive blocks are of the same type, you can tap on them and eliminate them, thus triggering the powerful skill they represent. After the elimination, the blocks to their left will be connected with the blocks to their right. Moreover, if consecutive blocks of the same type are eliminated, the powerful skill they unleash will be upgraded to a super skill, which is the most powerful skill of all.
DreamGrid is a newbie in this game, and he wants to trigger the super skill as many times as he can. Given nine skill blocks satisfying the description above, please help DreamGrid calculate the maximum number of times he can trigger the super skill.
Input
There are multiple test cases. The first line of input contains an integer (about 50), indicating the number of test cases. For each test case:
The first line contains a string () consisting of three 'g's, three 'a's and three 'o's, representing the nine skill blocks of three different types. Each type of character represents one type of skill block.
Output
For each test case, output an integer denoting the maximum number of times DreamGrid can trigger the super skill.
Sample Input
7
gggaaaooo
aaoogggoa
googgaaao
agogaooag
goooggaaa
gogogoaaa
gaogaogao
Sample Output
3
3
2
1
3
2
1
Hint
For the first sample test case, DreamGrid can first eliminate "aaa" (one super skill triggered), thus changing the skill blocks to "gggooo". He can then eliminate "ggg" (another super skill triggered) and finally eliminate "ooo" (a third super skill triggered). So the answer is 3.
For the second sample test case, DreamGrid can first eliminate "ggg" (one super skill triggered), thus changing the skill blocks to "aaoooa". He can then eliminate "ooo" (another super skill triggered) and finally eliminate "aaa" (a third super skill triggered). So the answer is also 3.
For the third sample test case, DreamGrid can first eliminate "aaa" (one super skill triggered), thus changing the skill blocks to "googgo". He can then eliminate "oo" to obtain "gggo", and eliminate "ggg" (another super skill triggered) to obtain "o". So the answer is 2. It is easy to prove that he cannot trigger the super skill three times under this arrangement of skill blocks.
原文链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5629
思路:先用find查询是不是满足3个都有 如果没有那只有1个或者2个的情况 那么删除字符串 a g o三个之一的字符 看删哪个返回的数字最大 再叠加之前删的个数就好了
AC代码
#include<bits/stdc++.h>
using namespace std;
int findthree(string &num){//先查有没有3个的
int count=0;
for(int i=0;i<3;i++){
if(num.find("ggg")!=string::npos){//表示有
num.erase(num.find("ggg"),3);
count++;
}
else if(num.find("aaa")!=string::npos){
num.erase(num.find("aaa"),3);
count++;
}
else if(num.find("ooo")!=string::npos){
num.erase(num.find("ooo"),3);//然后就删掉
count++;
}
}
return count;//返回数量
}
int deleteOne(string num,char a){//枚举删 g a o
int f=0;
for(int i=0;i<num.length();){
if(a==num[i]){
num.erase(i,1);
f++;//如果有那就删了 index不往上增加
}
else{
i++;
}
}
if(f!=0){//表示有删过了
int p=findthree(num);
if(p==0) return 1;//1是表示至少有一个返回回去
else return p;
}
return 0;
}
int main(){
int T=0;
cin>>T;
while(T--){
string num;
cin>>num;
int k=findthree(num);
if(k<3){//2个和一个的情况
int maxNum=max(deleteOne(num,'a'),deleteOne(num,'g'));
maxNum=max(maxNum,deleteOne(num,'o'));//
cout<<maxNum+k<<endl;
}
else{
cout<<3<<endl;
}
}
return 0;
}