C- string balance

topic:

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? S already balanced if the output 0 .

Input:

A row of characters representing the given string s.

Output:

An integer that represents the answer.

Examples:

Input :
QWER

Output :
0

Input :
QQWE

Output :
1

Input :
QQQW

Output :
2

Input :
QQQQ

Output :
3

Note:

1 <= n <= 10 ^ 5. n is a multiple of four. String contains only the characters 'Q', 'W', 'E' and 'R'.

Topic analysis:

  • Because the answer to solving a continuum and moving left and right end sections have a clear direction to meet the usage of foot emulated.

  • • calculate the total number of the first string 'Q', 'W', 'E' and 'R' with an array appears NUM; when an equal number of times and each character appears exactly equal to the time n / 4, it has been balanced, 0 output.
    • When Otherwise, update num [0], num [1 ], num [2], num [3] respectively record does not contain the interval [l, r] this period, the character 'Q', 'W', 'E' the number and 'R' are.
    • first by replacing that the four types of characters equal to the number, int total = r-l + 1; total - = (maxx-num [0]) + (maxx-num [1]) + (maxx-num [2]) + (num-Maxx [. 3]);
    • Analyzing then the remaining free position is a multiple of 4, and • if multiple total ≥0 4, the meet; num update array, continue to search for a smaller range, l ++.
    • Otherwise, the current [l, r] does not meet the requirements, the update num array, r ++, continue to expand the range of foot to take the interval looking for.

(Wrong choice of language for the Microsoft Visual C ++ 2010 led to ce, and also looking for a long silly mistake, language should be the GNU G ++ 17 7.3.0 fishes)

Code:

#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;

int main()
{
	string s;
	cin>>s;
	int len=s.length();
	int ans=len+1;
	int l=0,r=0;
	int m=len/4;//平衡的话每个字符个数 
	int num[4];
	memset(num,0,sizeof(num)); 
	
	for(int i=0;i<len;i++)//记录每个字符在字符串中出现次数
	{
		if(s[i]=='Q')
		{
			num[0]++;
		}
		else if(s[i]=='W')
		{
			num[1]++;
		}
		else if(s[i]=='E')
		{
			num[2]++;
		}
		else if(s[i]=='R')
		{
			num[3]++;
		}
	}
	if(num[0]==m&&num[1]==m&&num[2]==m&&num[3]==m)//s已平衡 
	{
		ans=0;
		cout<<ans<<endl;
		return 0;
	}
 
	for(int i=l;i<=r;i++)//[l,r]区间内各字符的个数 
	{
		if(s[i]=='Q')
		{
			num[0]--;
		}
		else if(s[i]=='W')
		{
			num[1]--;
		}
		else if(s[i]=='E')
		{
			num[2]--;
		}
		else if(s[i]=='R')
		{
			num[3]--;
		}
	}

	while(r<len&&l<len)//窗口未出界 
	{
		int maxx=max(max(num[0],num[1]),max(num[2],num[3]));
		int total=r-l+1;//可以更改的位置 
		total-=(maxx-num[0])+(maxx-num[1])+(maxx-num[2])+(maxx-num[3]);//填平操作 
        if(total>=0&&total%4==0)
		{
			
			if(s[l]=='Q')
			{
				num[0]++;
			}
			else if(s[l]=='W')
			{
				num[1]++;
			}
			else if(s[l]=='E')
			{
				num[2]++;
			}
			else if(s[l]=='R')
			{
				num[3]++;
			}
			ans=min(ans,r-l+1);
            l++;//继续寻找更小区间 
        }
        else//不符合条件 扩大区间 
        {
 			r++;	
			if(s[r]=='Q')
			{
				num[0]--;
			}
			else if(s[r]=='W')
			{
				num[1]--;
			}
			else if(s[r]=='E')
			{
				num[2]--;
			}
			else if(s[r]=='R')
			{
				num[3]--;
			}
        }
    }
	cout<<ans<<endl;
    return 0;
}

Published 21 original articles · won praise 0 · Views 1092

Guess you like

Origin blog.csdn.net/qq_43746837/article/details/105013951