当前位置:网站首页>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);
}
}
边栏推荐
- 【DesignMode】代理模式(proxy pattern)
- 直接上干货,100%好评
- Master this set of refined Android advanced interview questions analysis, oppoandroid interview questions
- 二叉搜索树(特性篇)
- 深度监听 数组深度监听 watch
- 最新Android面试合集,android视频提取音频
- [designmode] facade patterns
- Binary search tree (features)
- QT video transmission
- LeetCode 300. Daily question of the longest increasing subsequence
猜你喜欢
null == undefined
二叉搜索树(基操篇)
Opencv personal notes
数据中台落地实施之法
Personal notes of graphics (1)
最新2022年Android大厂面试经验,安卓View+Handler+Binder
DNS 系列(一):为什么更新了 DNS 记录不生效?
time标准库
作为Android开发程序员,android高级面试
The process of creating custom controls in QT to encapsulating them into toolbars (II): encapsulating custom controls into toolbars
随机推荐
Pychart ide Download
【PHP】PHP接口继承及接口多继承原理与实现方法
Opportunity interview experience summary
[PHP] PHP interface inheritance and interface multi inheritance principle and implementation method
LeetCode 403. 青蛙过河 每日一题
偶然升职的内心独白
掌握这套精编Android高级面试题解析,oppoAndroid面试题
Geoserver2.18 series (5): connect to SQLSERVER database
Have fun | latest progress of "spacecraft program" activities
LeetCode 1043. Separate the array to get the maximum and daily questions
LeetCode 1626. The best team without contradiction
LeetCode-SQL第一天
网关Gateway的介绍与使用
Find tags in prefab in unity editing mode
Arduino 控制的双足机器人
three. JS create cool snow effect
打造All-in-One应用开发平台,轻流树立无代码行业标杆
Direct dry goods, 100% praise
如何选择合适的自动化测试工具?
01tire+ chain forward star +dfs+ greedy exercise one