Crusaders Quest(模擬)

ZOJ - 3983

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 kkk (k≥1k \ge 1k≥1) 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 k=3k=3k=3 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 TTT (about 50), indicating the number of test cases. For each test case:
The first line contains a string sss (∣s∣=9|s| = 9∣s∣=9) 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.

先貪心將原字符串暴力消解。
如果全部消掉了,答案自然是3。暴力消解不存在消兩對。
如果消去一對,那剩下的把其中一種消掉,留剩下一種湊成一次,此時答案是2。
如果暴力消解時一次都沒消去,那答案不是2就是1。如果任意的字母的最左端到最右端的區間內有其他兩個字母(要消去這兩個字母才能使這個字母的兩端連接起來,如果三種字母都這樣的話,那就只能消去其他兩種才能使得其中一種連接起來了),答案就是1,否則是2。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <unordered_set>
#include <algorithm>
using namespace std;
#define scanf scanf_s

string s;


int main() {
	ios::sync_with_stdio(false);
	int T;
	cin >> T;
	while (T--) {
		int Ans = 0;
		cin >> s;
		string temp = s;
		for (int i = 0; i < s.size(); ++i) {
			if (i + 2 < 9 && s[i] == s[i + 1] && s[i + 1] == s[i + 2]) {
				++Ans;
				s.erase(i, 3);
				i = max(i - 3, -1);
			}
		}

		if (Ans == 1) {
			Ans = 2;
		}
		else if (Ans == 0) {
			auto Find = [&temp](const char ch)->bool {
				int Left = -1, Right = -1;
				for (int i = 0; i < 9; ++i) {
					if (temp[i] == ch) {
						if (Left == -1) {
							Left = i;
						}
						else {
							Right = i;
						}
					}
				}
				char other = 0;
				for (int i = Left; i <= Right; ++i) {
					if (temp[i] != ch) {
						if (other == 0) {
							other = temp[i];
						}
						else if (temp[i] != other) {
							return true;
						}
					}
				}
				return false;
			};
			if (Find('a') && Find('g') && Find('o')) {
				Ans = 1;
			}
			else {
				Ans = 2;
			}
		}
		cout << Ans << endl;
	}

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章