zoj 3983 Crusaders Quest(~水~)

链接

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.


  • 分西:
    暴力题,暴力都不太会了

  • 代码:

#include 
#define INF 0x3fffffff

#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
using namespace std;
typedef long long ll;


struct pa
{
	int id,num;
	pa()
	{id=0,num=0;}
};


int get_ans(char str[],char c)
{
	int ans=0;
	stack<pa> st;
	int len=strlen(str);
	for(int j=0;j<len;j++)
	{
		if(str[j]==c)
			continue;
		else if(str[j]=='g')
		{
			pa t;
			t.id=1;
			t.num=1;
			if(st.empty())
				st.push(t);
			else
			{
				pa pos=st.top();
				st.pop();
				if(pos.id==t.id)
				{
					
					pos.num+=t.num;
					if(pos.num>=3)
						ans++;
					else
					{
						st.push(pos);
					}
				}
				else 
				{
					st.push(pos);
					st.push(t);
				}
			}
		}
		else if(str[j]=='a')
		{
			pa t;
			t.id=2;
			t.num=1;
			if(st.empty())
			st.push(t);
			else
			{
				pa pos=st.top();
				st.pop();
				if(pos.id==t.id)
				{
					pos.num+=t.num;
					if(pos.num>=3)
						ans++;
					else
					{
						st.push(pos);
					}
				}
				else
				{
					st.push(pos);
					st.push(t);
				}
			}
			
		}
		else 
		{
			pa t;
			t.id=3;
			t.num=1;
			if(st.empty())
				st.push(t);
			else
			{
				pa pos=st.top();
				st.pop();
				if(pos.id==t.id)
				{
					pos.num+=t.num;
					if(pos.num>=3)
						ans++;
					else
					{
						st.push(pos);
					}
				}
				else
				{
					st.push(pos);
					st.push(t);
				}
			}
			
		}
	}
	return ans;
}


char str[10];

int main()
{
	int t;
	
	scanf("%d",&t);
	
	for(int i=0;i<t;i++)
	{
		
		scanf("%s",str);    
		int ans=get_ans(str,'0');
		
		ans=max(ans,get_ans(str,'g'));
		ans=max(ans,get_ans(str,'a'));
		ans=max(ans,get_ans(str,'o'));
		ans=max(1,ans);
		printf("%d\n",ans);
		
	}
	return 0;
}


你可能感兴趣的:(zoj 3983 Crusaders Quest(~水~))