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

Opencv personal notes

数据中台落地实施之法

Master this promotion path and share interview materials

值得一看,面试考点与面试技巧

爬虫(17) - 面试(2) | 爬虫面试题库

Cesium(3):ThirdParty/zip. js
ByteDance Android gold, silver and four analysis, Android interview question app

Pisa-Proxy SQL 解析之 Lex & Yacc

Binary search tree (basic operation)

skimage学习(1)
随机推荐
01tire+ chain forward star +dfs+ greedy exercise one
LeetCode 213. Home raiding II daily question
【DesignMode】享元模式(Flyweight Pattern)
Opencv configuration 2019vs
Localstorage and sessionstorage
字节跳动Android金三银四解析,android面试题app
dapp丨defi丨nft丨lp单双币流动性挖矿系统开发详细说明及源码
LeetCode 1774. 最接近目标价格的甜点成本 每日一题
Record the migration process of a project
The process of creating custom controls in QT to encapsulating them into toolbars (II): encapsulating custom controls into toolbars
深度监听 数组深度监听 watch
[medical segmentation] attention Unet
运算符
二叉搜索树(特性篇)
Binary search tree (features)
数据中台落地实施之法
在哪个期货公司开期货户最安全?
skimage学习(2)——RGB转灰度、RGB 转 HSV、直方图匹配
如何选择合适的自动化测试工具?
【Seaborn】组合图表:FacetGrid、JointGrid、PairGrid