PAT B1003 I want to pass! (Full score 20)

Thinking point:
The key to this question is the interpretation of the third condition.
Condition 3: If aPbTc is correct, then aPbATca is also correct, where a, b, and c are either empty strings, or only A string consisting of the letter A.
Interpretation: a, b, c are the number of A respectively. As long as the law is satisfied: a b=c, condition three is established.
You will find that the second condition is a special case of the third condition.
Therefore, the three conditions that need to be met are in short: the
string is correct, it must contain three characters P, A, T;
satisfies a
b=c.
The conversion problem is: Find the number of A between the three characters P, A, T in the input string, and judge the number.

code show as below:

#include<iostream>
#include<string>

using namespace std;

int main()
{
    
    
	int n;
	cin >> n;
	string s;
	string str[10];
	for (int i = 0; i<n; i++)
	{
    
    
		cin >> s;
		str[i] = s;
	}

	int a[10], b[10], c[10];
	int d, e;
	for (int i = 0; i < n; i++)
	{
    
    
		for (int j = 0; j < str[i].size(); j++)
		{
    
    
			if (str[i].at(j) == 'P' || str[i].at(j) == 'A' || str[i].at(j) == 'T')
			{
    
    
				if (str[i].at(j) == 'P') d = j+1;
				else if (str[i].at(j) == 'A');
				else if (str[i].at(j) == 'T') e = j+1;
			}
			else
			{
    
    
				str[i] = "NO";
				break;
			}
		}
		if (str[i] != "NO")
		{
    
    
			a[i] = d - 1;
			b[i] = e - d-1;
			c[i] = str[i].size() - e;
		}
	}

	for (int i = 0; i<n; i++)
	{
    
    
		if (str[i] != "NO") {
    
    
			if (a[i] * b[i] == c[i] && b[i] != 0) str[i] = "YES";// b[i] != 0为了排除PT
			else str[i] = "NO";
		}
	}

	for (int i = 0; i<n; i++)
		cout << str[i] << endl;

	system("pause");
	return 0;
}

Reflection and summary: In
this question, the knowledge examined is very basic and does not involve any algorithms. It mainly examines the basic usage of strings. The ingenious thing about this question is how to transform the question. At the same time, how to count the number of A represented by a, b, and c cleverly.

The code implementation of this question is entirely my own idea, I hope it helps.

Guess you like

Origin blog.csdn.net/qq_27538633/article/details/105896296