Ice-sugar Gourd

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 936    Accepted Submission(s): 329

Problem Description
Ice-sugar
gourd, “bing tang hu lu”, is a popular snack in Beijing of China. It is
made of some fruits threaded by a stick. The complicated feeling will
be like a both sour and sweet ice when you taste it. You are making your
mouth water, aren’t you?

I
have made a huge ice-sugar gourd by two kinds of fruit, hawthorn and
tangerine, in no particular order. Since I want to share it with two of
my friends, Felicia and his girl friend, I need to get an equal cut of
the hawthorns and tangerines. How many times will I have to cut the
stick so that each of my friends gets half the hawthorns and half the
tangerines? Please notice that you can only cut the stick between two
adjacent fruits, that you cannot cut a fruit in half as this fruit would
be no good to eat.
 
Input
The
input consists of multiply test cases. The first line of each test case
contains an integer, n(1 <= n <= 100000), indicating the number
of the fruits on the stick. The next line consists of a string with
length n, which contains only ‘H’ (means hawthorn) and ‘T’ (means
tangerine).
The last test case is followed by a single line containing one zero.
 
Output
Output
the minimum number of times that you need to cut the stick or “-1” if
you cannot get an equal cut. If there is a solution, please output that
cuts on the next line, separated by one space. If you cut the stick
after the i-th (indexed from 1) fruit, then you should output number i
to indicate this cut. If there are more than one solution, please take
the minimum number of the leftist cut. If there is still a tie, then
take the second, and so on.
 
Sample Input
4
HHTT
4
HTHT
4
HHHT
0
 
Sample Output
2
1 3
1
2
-1
 
 

开始用双重for循环,超时了,还是要用一个数组nh[i]来记录到第i个有多少个h

然后从前往后的结果
 
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 123456
int n,cnt_h,cnt_t;
char a[N];
int nh[N]; int main()
{
while(~scanf("%d",&n))
{
getchar();
if(n==)break; cnt_h=;
for(int i=;i<n;i++)
{
scanf("%c",&a[i]);
if(a[i]=='H')cnt_h++;
nh[i]=cnt_h;
}
if(n&)
{
cout<<-<<endl;
continue;
}
if(cnt_h&)
{
cout<<-<<endl;
continue;
}
if(nh[n/-]==cnt_h/)
{
printf("1\n%d\n",n/);
continue;
}
for(int i=;i<n;i++)
{
if(nh[n/+i-]-nh[i-]==cnt_h/)
{
printf("2\n%d %d\n",i,i+n/);
break;
} }
}
return ;
} //freopen("1.txt", "r", stdin);
//freopen("2.txt", "w", stdout);
//**************************************

最新文章

  1. SQL*Plus生成html文件
  2. MongoDB 维护Replica Set
  3. HTML5 视频(一)
  4. 2016网络大事记 mark
  5. 【BZOJ2038】【2009国家集训队】小Z的袜子(hose) 分块+莫队
  6. Mininet的安装与卸载
  7. hdu 4454 Stealing a Cake 三分法
  8. C# 中的结构类型(struct)
  9. SQL2008附加数据库提示错误:5120
  10. Android系统移植与调试之-------&gt;如何修改Android设备的开机第一阶段Logo
  11. IE 下使用firebug
  12. 2016 ACM Amman Collegiate Programming Contest D Rectangles
  13. Canada Cup 2016 C. Hidden Word
  14. maven 修改默认的JDK版本
  15. 杨晨露 Java 第一周总结
  16. nginx nfs服务
  17. Ajax状态值及状态码整理
  18. Web API中如何获取相对地址的绝对地址 Server.MapPath
  19. python 全栈开发,Day73(django多表添加,基于对象的跨表查询)
  20. Java LinkedList工作原理及实现

热门文章

  1. Hive 执行查询语句报错,由于内存空间不足导致
  2. Hive元数据启动失败,端口被占用
  3. gitlab简介配置和参数修改
  4. linux的自启动服务脚本的(/etc/rc.d/init.d或者其链接/etc/init.d)
  5. 【Luogu】P2617Dynamic Ranking(树状数组套主席树)
  6. BZOJ 3028 食物 ——生成函数
  7. BZOJ 1191: [HNOI2006]超级英雄Hero【二分图匹配】
  8. 洛谷P1435 回文字串
  9. 安装sqlServer2012失败补救
  10. git fetch tag 获取远程tag