Week5: string balance - foot emulated

Title Contents
a string of length n s, which contains only the 'Q', 'W', 'E', 'R' four kinds of characters.

If both the number of n / 4, it is balanced by a string of four kinds of characters appear in the string.

S can now be continuous period of the same length substrings replace any string that contains only four characters, so that it becomes a balanced string, alternatively ask the minimum length of the substring?

If s has balanced outputs 0.

Input format
line represents a given character string s

Output format
a integer answer

Sample input and output
the Input
QWER

Output
0

Input
QQWE

Output
1

Input
QQQW

Output
2

Input
QQQQ

Output
3

Note
. 1 <= n-<= 10. 5 ^;
n-multiple of 4;
string contains only the character 'Q', 'W', 'E' and 'R'.

Problem-solving ideas
to maintain two hands, draw an interval between two pointers.
For all points outside the interval obtained all their points, the number of occurrences of each letter sum1, sum2, sum3, sum4.
Maintains a MAX value, its value is equal to the sum of the biggest.
So we can get from each other letters appear most frequently is still a few that letter.
Provided ans = (MAX-sum1) + (MAX-sum2) + (MAX-sum3) + (MAX-sum4), then we just need to find at least one ans can be used to modify the conditions of the letter can be satisfied.
Then this ans points where to find it? - two pointers zoned out in section.
This was just after the interval by subtracting the number of letters ans can be a multiple of 4 or 0.

If the current interval length to meet the conditions, save up, it may be the ultimate answer to the requirements, then l pointer moved forward, that is trying to narrow the scope of the interval to see if there is a smaller range to meet the requirements.
If the current interval length condition is not satisfied, then the pointer is moved forward r, the range increases to see if there might meet the requirements.
Since the beginning of time possible, without any change in the string to meet the requirements, so the initial two pointers are placed in position 0.

The following shows the code:

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>

using namespace std;

int maxi[4];//Q W E R

void PlusMaxi(char a)
{
	switch (a)
	{
	case 'Q':
		maxi[0]++;
		break;
	case 'W':
		maxi[1]++;
		break;
	case 'E':
		maxi[2]++;
		break;
	case 'R':
		maxi[3]++;
		break;
	}
}

void SubMaxi(char a)
{
	switch (a)
	{
	case 'Q':
		maxi[0]--;
		break;
	case 'W':
		maxi[1]--;
		break;
	case 'E':
		maxi[2]--;
		break;
	case 'R':
		maxi[3]--;
		break;
	}
}

int Fun()
{
	int themax;
	if (maxi[0] > maxi[1]) themax = maxi[0];
	else themax = maxi[1];

	if (themax < maxi[2]) themax = maxi[2];

	if (themax < maxi[3]) themax = maxi[3];

	int ans = (themax - maxi[0]) + (themax - maxi[1]) + (themax - maxi[2]) + (themax - maxi[3]);
	return ans;
}

int main()
{
	memset(maxi, 0, sizeof maxi);

	string input;
	cin >> input;
	int length = input.length();
	for (int i = 0; i < length; i++)
	{
		PlusMaxi(input[i]);
	}

	if (maxi[0] == maxi[1] && maxi[1] == maxi[2] && maxi[2] == maxi[3])
	{
		cout << "0" << endl;
		return 0;
	}

	int theans = 0x3f3f3f3f;

	int l, r;
	l = r = 0;
	SubMaxi(input[0]);
	while (r != length)
	{
		int range = r - l + 1;
		int free = range - Fun();
		if (free >= 0 && free % 4 == 0)
		{
			theans = min(theans, range);
			if (l < r)
			{
				PlusMaxi(input[l]);
				l++;
			}
			else
			{
				PlusMaxi(input[l]);
				l++;
				if (r < length)
				{
					r++;
					SubMaxi(input[r]);
				}
			}
		}
		else
		{
			if (r < length)
			{
				r++;
				SubMaxi(input[r]);
			}
		}

	}

	cout << theans << endl;

	return 0;


}
Published 21 original articles · won praise 3 · Views 407

Guess you like

Origin blog.csdn.net/qq_44506233/article/details/105105530