当前位置:网站首页>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 :
TABTABBTTTTThe program should output :
3Another example , Input :
TTAAABBThe program should output :
0We 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);
}
}
边栏推荐
- Personal notes of graphics (4)
- 【MySql进阶】索引详解(一):索引数据页结构
- 谈谈 SAP 系统的权限管控和事务记录功能的实现
- 谎牛计数(春季每日一题 53)
- ORACLE进阶(六)ORACLE expdp/impdp详解
- A tour of gRPC:03 - proto序列化/反序列化
- Three. JS series (2): API structure diagram-2
- SqlServer2014+: 创建表的同时创建索引
- [PHP] PHP interface inheritance and interface multi inheritance principle and implementation method
- LeetCode 1774. The dessert cost closest to the target price is one question per day
猜你喜欢
The difference and working principle between compiler and interpreter
掌握这个提升路径,面试资料分享
Opencv configuration 2019vs
As an Android Developer programmer, Android advanced interview
两类更新丢失及解决办法
Cesium(3):ThirdParty/zip. js
Opencv personal notes
浅浅理解.net core的路由
Personal notes of graphics (2)
Have fun | latest progress of "spacecraft program" activities
随机推荐
Have fun | latest progress of "spacecraft program" activities
LeetCode 1981. 最小化目标值与所选元素的差 每日一题
【DesignMode】享元模式(Flyweight Pattern)
A tour of gRPC:03 - proto序列化/反序列化
SqlServer2014+: 创建表的同时创建索引
AutoLISP series (1): function function 1
LeetCode 1155. N ways to roll dice one question per day
使用JSON.stringify()去实现深拷贝,要小心哦,可能有巨坑
LeetCode 120. Triangle minimum path and daily question
Process from creation to encapsulation of custom controls in QT to toolbar (I): creation of custom controls
ByteDance Android gold, silver and four analysis, Android interview question app
预售17.9万,恒驰5能不能火?产品力在线,就看怎么卖
Sort out several important Android knowledge and advanced Android development interview questions
Master this set of refined Android advanced interview questions analysis, oppoandroid interview questions
Temperature sensor chip used in temperature detector
LeetCode 1986. The minimum working time to complete the task is one question per day
二叉搜索树(特性篇)
LeetCode 1186. 删除一次得到子数组最大和 每日一题
3000 words speak through HTTP cache
The latest interview experience of Android manufacturers in 2022, Android view+handler+binder