LightOJ 1051

A string is called bad if it has 3 vowels in a row, or 5 consonants in a row, or both. A string is called good if it is not bad. You are given a string s, consisting of uppercase letters ('A'-'Z') and question marks ('?'). Return "BAD" if the string is definitely bad (that means you cannot substitute letters for question marks so that the string becomes good), "GOOD" if the string is definitely good, and "MIXED" if it can be either bad or good.

The letters 'A', 'E', 'I', 'O', 'U' are vowels, and all others are consonants.

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case begins with a non-empty string with length no more than 50.

Output

For each case of input you have to print the case number and the result according to the description.

Sample Input

5
FFFF?EE
HELLOWORLD
ABCDEFGHIJKLMNOPQRSTUVWXYZ
HELLO?ORLD
AAA

Output for Sample Input

Case 1: BAD
Case 2: GOOD
Case 3: BAD
Case 4: MIXED
Case 5: BAD

题目大意就是说如果一个字符串有连续5个辅音或者3个元音就是称这个字符串为"BAD",否则称为"GOOD". 对于一个字符串,其中有"?"存在,"?"可以当成元音也可以当成辅音. 所以对于有问号的字符串有可能是"BAD"也有可能是"GOOD".此时称为这个字符串为"MIXED".

现在问所给字符串为哪种类型。

模拟:

  • 先不计"?",判断是否有连续三个元音或者连续五个元音的情况。如果有的话直接就是"BAD"
  • 如果第一步没有的话,则把所有问号当成元音或者辅音,判断这样的情况是否存在连续三个元音或者连续5个元音。如果没有的话直接是"GOOD".
  • 如果有的话就要判断是否可以组成"MIXED". 要想组成"MIXED",必须不能存在连续三个元音或者连续五个元音的情况,所以对于每一个问号就要进行一个特殊判断.

代码如下:

#include<bits/stdc++.h>
using namespace std;

bool judge(char ch)
{
	if(ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
		return true;
	return false;
}

string solve()
{
	string str;
	cin >> str;
	for(int i=0, a=0, b=0; i<str.size(); ++ i)
	{
		if(str[i] == '?')
			a = 0, b = 0;
		else if(judge(str[i]))
			++ a, b = 0;
		else
			++ b, a = 0;

		if(a == 3 || b == 5)
			return string{"BAD"};
	}
	bool is = false;
	for(int i=0, a=0, b=0; i<str.size(); ++ i)
	{
		if(str[i] == '?')
			++ a, ++ b;
		else if(judge(str[i]))
			++ a, b = 0;
		else
			++ b, a = 0;
		if(a == 3 || b == 5)
		{
			is = true;
			break;
		}
	}
	if(is == false)
		return string{"GOOD"};

	for(int i=0; i<str.size(); ++ i)
	{
		if(str[i] == '?')
		{
			int leftvow = 0, leftcon = 0, rightvow = 0, rightcon = 0;
			for(int j=i-1; j>=0; -- j)
			{
				if(str[j] == '?')
					break;

				if(judge(str[j]))
				{
					if(leftcon)
						break;
					++ leftvow;
				}
				else
				{
					if(leftvow)
						break;
					++ leftcon;
				}
			}

			for(int j=i+1; j<str.size(); ++ j)
			{
				if(str[j] == '?')
					break;

				if(judge(str[j]))
				{
					if(rightcon)
						break;
					++ rightvow;
				}
				else
				{
					if(rightvow)
						break;
					++ rightcon;
				}
			}

			if((leftvow == 2 && rightcon == 4) || (leftcon == 4 && rightvow == 2))
				return string{"BAD"};

			if(leftcon == 4 || rightcon == 4)
				str[i] = 'A';

			if(leftvow == 2 || rightvow == 2)
				str[i] = 'B';
		}
	}
	return string{"MIXED"};
}

int main()
{
	ios::sync_with_stdio(false);
	
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);

	int t;
	cin >> t;
	for(int i=1; i<=t; ++ i)
		cout << "Case " << i << ": " << solve().c_str() << endl;
	return 0;
}


原文地址:https://www.cnblogs.com/aiterator/p/6785826.html