[Programming thinking and practice Week5 job C] balance string

Subject description:

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 asked minimum length of the substring?
If s has balanced outputs 0 .
. 1 <= n-<= 10. 5 ^
n-4 is a multiple of
the string contains only the character 'Q', 'W', 'E' and 'R'.

Input formats:

A line representing a given character string s

Output formats:

An integer that represents the answer

Example:

Here Insert Picture Description

Ideas:

To select a string section, such that any content changes such that the entire string string string is balanced, the substrings should meet the conditions that: the first portion of the string to come up with a number of letters such that the four kinds of the entire string the same number, and the remaining portion should satisfy a multiple of 4, so that four kinds of letters can be divided equally. I.e., it is assumed that there are m substring letters, while the number of the rest of the outer sub-string of four letters, respectively a1, a2, a3, a4; take four numbers the maximum number of Max, the substrings should be removed q = Max-a1 + Max- a2 + Max-a3 + Max-a4 letters to replace the same amount so that four kinds of letters, while the remainder must mq positions equally to such a four-letter string to be balanced, That mq must be divisible by 4.
Know the substring after the selection criteria, the next question is how to select the criterion sub-strings meet, if the selection was eleven O (2 n- complexity), the significant undesirable.
Scale approach used here achieved, i.e. the use of about two pointer entire string, from the beginning to the end of the slide, similar to a variable size sliding window, first left pointer points to the first string, the first string and the right pointer from sliding to the rear , each additional bit substring, determined whether it is satisfied the selection criteria, not satisfied, the results obtained in the left hand end of the slide necessarily meet the selection criteria are not, therefore should be such that the right hand end of the slide, then the determination, if substring to meet the requirements, the results obtained to the right hand end of the slide necessarily satisfy the condition, but substring necessarily becomes long, and does not contribute to the results, at this time should be slid to the rear left pointer to shorten the length of the string, the substring to meet the conditions recorded in a variable length which, compared with the continuous take meet the minimum requirements of the subject results.
However, this method can not determine on the balance of the original string, it returns 1, so this situation should be judged in advance.

Code:

#include <iostream>
#include<map>
#include<string.h>
using namespace std;
const int size=1e5+10;
char str[size];
map<char,int> mp;
int note[4]={0,0,0,0};
int n,l=1,r=0;
bool solve()
{
	int M=note[0];
	for(int i=1;i<4;i++)
		if(note[i]>M)
			M=note[i];
	int t=r-l+1;
	int temp=t-(M-note[0])-(M-note[1])-(M-note[2])-(M-note[3]);
	if(temp>=0&&temp%4==0)
		return true;
	else
		return false;
}
int main(int argc, char** argv) {
	mp['Q']=0;mp['W']=1;mp['E']=2;mp['R']=3;
	int n=1;
	scanf("%s",str+1);
	n=strlen(str+1);
	for(int i=1;i<=n;i++)
		note[mp[str[i]]]++; 
	int ans=size;
	if(note[0]==note[1]&&note[1]==note[2]&&note[2]==note[3])
		ans=0;
	else
	{
		while(r<=n)
		{
			while(l<=r&&solve())
			{
				ans=min(ans,r-l+1);
				note[mp[str[l]]]++;
				l++;
			}
			r++;note[mp[str[r]]]--;
		}
	}	
	printf("%d\n",ans);
	return 0;
}
Published 24 original articles · won praise 8 · views 527

Guess you like

Origin blog.csdn.net/weixin_44034698/article/details/104993791