hdu_3819 A and B Problem (字符串)
来源:互联网 发布:苏伊士运河 知乎 编辑:程序博客网 时间:2024/06/07 23:29
http://acm.hdu.edu.cn/showproblem.php?pid=3819
分析:
由于最终要将两种字符分开,所以一开始,统计其中任一字符的总个数,也即最终会被放在一起的该字符的个数,suma;
然后,以此长度遍历字符串,找到所有子串中具有此字符最多的,而另外一种字符的个数也即需要交换的次数;
另者,需要注意其循环性。处理方法是从第i个字符(从该字符开始到最后一个字符的个数<suma,也即需要从前面取字符)开始到最后一个字符
代码:
#include <iostream>#include <cstdio>#include <string.h>#include <string>using namespace std;#define M 100001int ans[M];int main(){ //freopen("in.txt","r",stdin); int t,i; string s; scanf("%d",&t); for(int ccnt=1;ccnt<=t;ccnt++){ cin>>s; int len=s.length(); int cnt=0; for(i=0;i<len;i++){ if(s[i]=='A') cnt++; ans[i]=cnt; } int suma=ans[len-1]; int cnta=0,tmpa; for(i=0;i+suma<len;i++){ tmpa= ans[i+suma] - ans[i]; if(tmpa>cnta) cnta=tmpa; } for(;i<len;i++){ tmpa=ans[len-1]-ans[i] + ans[suma-(len-i)]; if(tmpa>cnta) cnta=tmpa; } printf("Case %d: %d\n",ccnt,suma-cnta); } return 0;}
0 0
- hdu_3819 A and B Problem (字符串)
- HDU 3819 A and B Problem 对字符串进行处理
- HDUOJAlexandra and A*B Problem
- hdu 3819 A and B problem
- HDU 5109 Alexandra and A*B Problem
- hdu 5109 Alexandra and A*B Problem
- HDU-3819-A and B Problem
- HDU 3819 A and B Problem
- hdu 5109 Alexandra and A*B Problem
- Nyoj 844 A+B Problem[字符串逆序]
- 524 A-B Problem【字符串处理】
- 暴力+字符串——A*B Problem
- 【BestCoder】 HDOJ 5109 Alexandra and A*B Problem
- HDU5109 Alexandra and A*B Problem(数学题)
- Hdu 5109 Alexandra and A*B Problem(枚举)
- Round B APAC Test 2017 Problem A. Sherlock and Parentheses
- NYOJ 524 A-B Problem (字符串&大数)
- A + B Problem II(利用字符串进行四则运算)
- A summary of the first semester
- 有一种黑客叫做工具党
- Memcached 解析二
- 《Start Developing iOS Apps Today》学习笔记(二)应用实现过程
- 取石子游戏 (经典博弈论)
- hdu_3819 A and B Problem (字符串)
- Memcached 解析三
- 判断excel已经打开
- ubuntu 12.04 安装 tomcat
- C.Interface.And.Implementations—sequence的实现
- hdu 2686 Matrix hdu 3376 Matrix Again 费用流
- mapreduce
- ubuntu11.10 安装vmtools
- js继承有5种实现方式