当前位置:网站首页>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);
}
}
边栏推荐
猜你喜欢

skimage学习(2)——RGB转灰度、RGB 转 HSV、直方图匹配

C语言进阶——函数指针

Skimage learning (3) -- gamma and log contrast adjustment, histogram equalization, coloring gray images

字节跳动高工面试,轻松入门flutter

最新阿里P7技术体系,妈妈再也不用担心我找工作了

【医学分割】attention-unet
作为Android开发程序员,android高级面试

应用在温度检测仪中的温度传感芯片

字节跳动Android面试,知识点总结+面试题解析

Temperature sensor chip used in temperature detector
随机推荐
Localstorage and sessionstorage
LeetCode 213. Home raiding II daily question
Seaborn数据可视化
[designmode] flyweight pattern
【DesignMode】外观模式 (facade patterns)
最新2022年Android大厂面试经验,安卓View+Handler+Binder
The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit
Ray and OBB intersection detection
Lowcode: four ways to help transportation companies enhance supply chain management
预售17.9万,恒驰5能不能火?产品力在线,就看怎么卖
【MySql进阶】索引详解(一):索引数据页结构
爬虫(17) - 面试(2) | 爬虫面试题库
LeetCode 1477. 找两个和为目标值且不重叠的子数组 每日一题
AutoLISP series (1): function function 1
Opencv personal notes
Spark Tuning (III): persistence reduces secondary queries
【DesignMode】模板方法模式(Template method pattern)
Advanced C language -- function pointer
LeetCode 120. Triangle minimum path and daily question
Skimage learning (3) -- gamma and log contrast adjustment, histogram equalization, coloring gray images