当前位置:网站首页>Number of exchanges in the 9th Blue Bridge Cup finals

Number of exchanges in the 9th Blue Bridge Cup finals

2022-07-07 16:59:00 @Little safflower

Problem description


IT The demand for industrial talents is rising . Industry giant Baidu 、 Alibaba 、 tencent ( abbreviation BAT) Recruitment activities on a beach .
The recruitment department is lined up . Because it is free to seize the seat , The seats of the three major companies are staggered randomly , Form like :
ABABTATT, This makes the candidate very uncomfortable .
therefore , Exchange positions as required by the management of the recruitment department , Make the seats of each group close together . That is, the final shape is like :
BBAAATTT This shape , Of course , It could be :
AAABBTTT etc. .

Now? , Suppose you can only exchange 2 seats , And know the current seat distribution ,
Your task is to calculate : How many exchanges are needed to make the recruitment seats of each group close together .

Input is one line n Characters ( Contains only letters B、A or T), Indicates the current seat distribution .
The output is an integer , Indicates at least the number of exchanges .

such as , Input :
TABTABBTTTT

The program should output :
3

Another example , Input :
TTAAABB

The program should output :
0

We have an agreement , The length of the input string n No more than 10 ten thousand

Resource Convention :
Peak memory consumption ( Including virtual machines ) < 256M
CPU Consume  < 1000ms


Please output strictly as required , Don't print like that :“ Please input ...” Extra content of .

All code in the same source file , After debugging , Copy and submit the source code .
Do not use package sentence . Do not use jdk1.7 And above .
The name of the main class must be :Main, Otherwise, it will be treated as invalid code .
 

Java

import java.util.Scanner;

public class  Number of exchanges  {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		char[] ss = scanner.next().toCharArray();
		// Six permutation schemes 
		char[][] c = {
				{'B','A','T'},
				{'B','T','A'},
				{'A','B','T'},
				{'A','T','B'},
				{'T','A','B'},
				{'T','B','A'}
		};
		
		int ans = Integer.MAX_VALUE;
		for(int i = 0;i < 6;i++) {
			ans = Math.min(ans,fun(ss,c[i][0],c[i][2],c[i][2]));
		}
		System.out.println(ans);
	}
	// Divide the whole string into three regions 
	public static int fun(char[] ss,char A,char B,char C) {
		//A In the region a The number of 
		int a = 0;
		//B In the region b The number of 
		int b = 0;
		//A Regional Central Africa a The number of 
		int Abc = 0;
		//A In the region b The number of 
		int Ab = 0;
		//B In the region a The number of 
		int Ba = 0;
		//B In the region c The number of 
		int Bc = 0;
		int len = ss.length;
		// Find out A and B The size of the two areas 
		for(int i = 0;i < len;i++) {
			if(ss[i] == A) a++;
			if(ss[i] == B) b++;
		}
		for(int i = 0;i < a;i++) {
			if(ss[i] != A) Abc++;
			if(ss[i] == B) Ab++;
		}
		for(int i = a;i < a + b;i++) {
			if(ss[i] == A) Ba++;
			if(ss[i] == C) Bc++;
		}
		// The minimum number of exchanges is : take A Regional non a Exchange out ,B Regional non b Exchange out 
		// because A There are... In the area b,B There are... In the area a, They can exchange directly , Then subtract the number of repeated exchanges 
		return Abc + Bc + Ba - Math.min(Ba, Ab);
	}

}

 

原网站

版权声明
本文为[@Little safflower]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071512070227.html