HDU 3819 A and B Problem
来源:互联网 发布:福建省软件行业协会 编辑:程序博客网 时间:2024/05/02 21:48
A and B Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 786 Accepted Submission(s): 251
Problem Description
After calculating A + B, let’s consider another easy problem which only contains A and B.
You are given a string s containing only the letters 'A' and 'B'. The letters are arranged in a circle, so the last and first characters are adjacent. You will perform a series of swaps until all the 'A's form one consecutive sequence and all the 'B's form another consecutive sequence. In each swap, you can select any two characters and swap them. Find the minimal number of swaps necessary to reach your goal.
You are given a string s containing only the letters 'A' and 'B'. The letters are arranged in a circle, so the last and first characters are adjacent. You will perform a series of swaps until all the 'A's form one consecutive sequence and all the 'B's form another consecutive sequence. In each swap, you can select any two characters and swap them. Find the minimal number of swaps necessary to reach your goal.
Input
The first line contains a single integer T, indicating the number of test cases.
Each test case only contains a string as description.
Technical Specification
1. 1 <= T <= 100
2. 1 <= |S| <= 100000, |S| indicating the length of the string.
Each test case only contains a string as description.
Technical Specification
1. 1 <= T <= 100
2. 1 <= |S| <= 100000, |S| indicating the length of the string.
Output
For each test case, output the case number first, then the minimal number of swaps.
Sample Input
3AABBABABAABABA
Sample Output
Case 1: 0Case 2: 1Case 3: 1
Author
iSea@WHU
Source
The 6th Central China Invitational Programming Contest and 9th Wuhan University Programming Contest Final
可以用循环数组也可以直接扩展成两倍计算。只需要选择一个范围,我们让这个范围大小是A的个数,一格一格的移动过去,判断所有情况中这个范围内B的个数最少是多少,这个就是需要移动的最少的次数了。
#include <vector>#include <map>#include <set>#include <algorithm>#include <iostream>#include <cstdio>#include <cmath>#include <cstdlib>#include <string>#include <cstring>using namespace std;string s,t;int main(){int n;scanf("%d",&n);int zl = 1;while(n--){cin >> s;int cnta = 0;int cnt = 0;for (int i = 0; i < s.size(); i++){if (s[i] == 'A') cnta++;}t = s + s;for (int i = 0; i < cnta; i++){if (t[i] == 'B') cnt++;}int end = cnta -1;int min_cnt = cnt;for (int i = 1; i < s.size(); i++){if (t[i - 1] == 'B') cnt--;if (t[end + 1] == 'B') cnt++;end++;min_cnt = min(cnt,min_cnt);}printf("Case %d: %d\n",zl++,min_cnt);}return 0;}
0 0
- hdu 3819 A and B problem
- HDU-3819-A and B Problem
- HDU 3819 A and B Problem
- HDU 3819 A and B Problem 对字符串进行处理
- HDU 5109 Alexandra and A*B Problem
- hdu 5109 Alexandra and A*B Problem
- hdu 5109 Alexandra and A*B Problem
- hdu A + B problem
- Hdu 5109 Alexandra and A*B Problem(枚举)
- HDUOJAlexandra and A*B Problem
- hdu A + B Problem II
- hdu 3828 A + B problem
- HDU 3828 A+B problem
- hdu 1000 A + B Problem
- [HDU]1000(A+B Problem)
- HDU 1000 A + B Problem
- HDU--1000--A + B Problem
- hdu - 1000 - A + B Problem
- java.lang.Exception: 索引中丢失 IN 或 OUT 参数::2 解决方法
- Git Bug分支
- xml解析
- 项目26.1 成绩等级
- linux的操作
- HDU 3819 A and B Problem
- 前端素材汇总(持续更新中......)
- 解决maven项目出现Dynamic Web Module 3.0 requires Java 1.6 or newer.错误
- Dom解析
- hadoop namenode无法启动,提示Failed on local exception的解决方法
- 第4周SHH数据结构—【项目1-建立单链表】
- 怀揣梦想不曾停歇
- 渣滓一样的页面
- 自定义控件之功能开关