week5 job C

The meaning of problems:
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 .
input:
line representing a given character string S
Output:
an answer integer
Sample input:
QWER
Sample Output:
0
Sample input:
QQWE
Sample Output:
. 1
Sample input:
QQQW
Sample Output:
2
Sample input:
the QQQQ
Sample Output:
. 3
Note :
. 1 <= n-<= 10. 5 ^
n-4 is a multiple of
the string contains only the character 'Q', 'W', 'E' and 'R'.
ideas:
To determine the number of each character in the input string. After the determination, if the number of characters per line with the requirements of the subject to 0. Otherwise, the output will be greater than n characters -n operation. The remaining characters stored so that it is determined again after a value of 0. The number of each character, which is compared with the number of characters stored before the operation, then operated by left and right boundaries, the condition is satisfied, the left margin is reduced, meet the characters, the number of the character is reduced by 1, is not satisfied, the right to increase the border, right border encounter the characters, the number of characters plus one, constantly moving around the border judge, update the answer, output .

Code:

#include<iostream>
#include<cstring>
#include<map>
using namespace std;
char temp[4]={'Q','W','E','R'};
map<char,int> ran;
int main()
{
	string s;
	cin>>s;
	//int length=s.length();
	for(int i=0;i<s.length();i++)
	{
		if(s[i]=='Q')
		{
			ran[temp[0]]++;
		}
		if(s[i]=='W')
		{
			ran[temp[1]]++;
		}
		if(s[i]=='E')
		{
			ran[temp[2]]++;
		}
		if(s[i]=='R')
		{
			ran[temp[3]]++;
		}
	}
	int n=s.length()/4;
	if(ran[temp[0]]==n&&ran[temp[1]]==n&&ran[temp[2]]==n&&ran[temp[3]]==n)
	{
		cout<<"0";
		return 0;
	}
	int num=0;
	for(int i=0;i<4;i++)
	{
		if(ran[temp[i]]>n)
		{
			ran[temp[i]]=ran[temp[i]]-n;
			num=num+ran[temp[i]];
		}
		else
		{
			ran[temp[i]]=0;
		}
	}
	int left=0,right=0;
	int cm[4]={0};
	int ans=s.length();
	for(int i=left;i<=right;i++)
	{
		if(s[i]=='Q')
		{
			cm[0]++;
		}
		if(s[i]=='W')
		{
			cm[1]++;
		}
		if(s[i]=='E')
		{
			cm[2]++;
		}
		if(s[i]=='R')
		{
			cm[3]++;
		}
		
	}
	while(right<s.length())
	{
		if(cm[0]>=ran[temp[0]]&&cm[1]>=ran[temp[1]]&&cm[2]>=ran[temp[2]]&&cm[3]>=ran[temp[3]])
		{
			if((right-left)<ans)
			   ans=right-left+1;
			if(s[left]=='Q')
			   cm[0]--;
			if(s[left]=='W')
			   cm[1]--;
			if(s[left]=='E')
			   cm[2]--;
			if(s[left]=='R')
			   cm[3]--;
			left++;
		}
		else
		{
			right++;
			if(s[right]=='Q'&&right!=s.length())
			   cm[0]++;
			if(s[right]=='W'&&right!=s.length())
			   cm[1]++;
			if(s[right]=='E'&&right!=s.length())
			   cm[2]++;
			if(s[right]=='R'&&right!=s.length())
			   cm[3]++;
		}
	}
	cout<<ans;
	return 0;
 } 
Published 19 original articles · won praise 0 · Views 206

Guess you like

Origin blog.csdn.net/weixin_45117273/article/details/105331776