WEEK 5 C balanced string

topic:

A string s of length n, which contains only the four characters 'Q', 'W', 'E', 'R'.

If the number of occurrences of the four characters in the character string is n / 4, it is a balanced character string.

Now you can replace a continuous substring in s with an arbitrary string of the same length that contains only those four characters, making it a balanced string. What is the minimum length of substring to replace?

If s is balanced, output 0.

Input:

A line of characters represents the given string s

Output:

An integer represents the answer

Examples:

Input1

QWER

Output1

0

Input2

TOUCH

Output2

1

Input3

QQQW

Output3

2

Input4

QQQQ

Output4

3

Note

1<=n<=10^5

n is a multiple of 4

The string contains only the characters 'Q', 'W', 'E' and 'R'.

Ideas:

The answer to this problem is a continuous interval, and the movement of the left and right endpoints of the interval has a clear direction, so here we choose to use the ruler method to solve.
If the interval [L, R] meets the condition of an unbalanced string, the interval [L, R + 1] must also meet the condition, but it does not necessarily contribute to the answer, and the interval [L + 1, R] does not necessarily meet the condition . Therefore, if [L, R] meets the condition, then L ++, if [L, R] does not meet the condition, then R ++.
Use the record array to record the number of occurrences of each character, and then compare it with n / 4, set the zero less than n / 4, subtract n / 4 if it is greater than n / 4, and finally add it to get the to be processed The minimum string length sum. In this way, the ruler can be taken. If the number of occurrences of each character in the character string obtained by the ruler is greater than the number of character occurrences in the processed record, then the condition is met, L ++, otherwise R ++, until the ruler obtains the string The length is equal to sum or the ruler reaches the end of the string.

Code:

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

void changep(int* a, char p)
{
	if (p == 'Q')
		a[0]++;
	if (p == 'W')
		a[1]++;
	if (p == 'E')
		a[2]++;
	if (p == 'R')
		a[3]++;
}

void changem(int* a, char p)
{
	if (p == 'Q')
		a[0]--;
	if (p == 'W')
		a[1]--;
	if (p == 'E')
		a[2]--;
	if (p == 'R')
		a[3]--;
}

int slove(string s, int* record, int avg)
{
	int ans = s.size(), n = s.size(), sum = 0, left = 0, right = 1;
	int* rec = new int[4]();
	for (int i = 0; i < 4; i++)
	{
		if (record[i] <= avg)
		{
			record[i] = 0;
		}
		else
		{
			record[i] -= avg;
			sum += record[i];
		}
	}
	if (sum == 0)
		return 0;
	for (int i = left; i <= right; i++)
		changep(rec, s[i]);
	while (right < n)
	{
		if (rec[0] >= record[0] && rec[1] >= record[1] && rec[2] >= record[2] && rec[3] >= record[3])
		{
			ans = min(ans, (right - left + 1));
			changem(rec, s[left]);
			left++;
		}
		else
		{
			right++;
			if (right != n)
				changep(rec, s[right]);
		}
		if (ans == sum)
			break;
	}
	return ans;
}

int main()
{
	int *record = new int[4]();
	string s;
	cin >> s;
	int avg = s.size() / 4;
	for (int i = 0; i < s.size(); i++)
		changep(record, s[i]);
	int ans = slove(s, record, avg);
	cout << ans << endl;
	return 0;
}

Published 32 original articles · Likes0 · Visits 683

Guess you like

Origin blog.csdn.net/qq_43814559/article/details/105001359