hdu 3819 A and B problem
来源:互联网 发布:双十一数据作假 编辑:程序博客网 时间:2024/05/20 11:34
A and B Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 613 Accepted Submission(s): 190
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我的想法是先统计‘A’字符的个数sum.然后在sum长度区间找到最多的一段使'A',个数最多,那么剩下的sum-max即为要移动的字符的个数。用ct[]统计每个位置到第一个位置上'A'的个数。然后计算A的个数即可。代码:#include<iostream>#include<cmath> #include<string>using namespace std;char s[110000];int ct[110000];int main(){ int t,i,sum,len,j,max,move; scanf("%d",&t); //getchar(); for(i=1;i<=t;i++) { scanf("%s",&s); //getchar(); len=strlen(s); memset(ct,0,sizeof(ct)); sum=0; for(j=0;j<len;j++) { if(s[j]=='A'){sum++;} ct[j]=sum; } max=ct[sum-1]; for(j=0;j+sum<=len;j++) { move=ct[j+sum-1]-ct[j-1]; if (move>max) max=move; } for(;j<len;j++) { move=ct[(j+sum)%len-1]+ct[len-1]-ct[j-1]; if (move>max) max=move; } printf("Case %d: %d\n",i,sum-max); } return 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
- GridVIew中下载文件的方法探讨,方法(7)最佳。
- 正式版苹果雪豹操作系统光盘镜像
- Spring依赖注入的三种方式
- windowsphone7下图片格式的转换
- 解决Plugin和Checkbox组合时 双击checkbox报错的问题
- hdu 3819 A and B problem
- 散列容器
- Shell笔记第三天 后台运行程序
- VirtualBox中双虚拟机安装部署OpenStack指南
- Hibernate主键的增长策略
- 窗口向上滚动
- sjtu 1004 西西弗斯式的命运
- Android给TextView设置透明背景、圆角边框
- Jumbo Frame