您的位置:首页 > 编程语言 > Go语言

HDU3363-Ice-sugar Gourd

2017-01-17 10:01 363 查看


Ice-sugar Gourd

                                                                       Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65536/65536
K (Java/Others)

                                                                                                 Total Submission(s): 1495    Accepted Submission(s): 472


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

 

Source

“光庭杯”第五届华中北区程序设计邀请赛
暨 WHU第八届程序设计竞赛

 

题意:给一个字符串只由”H”和”T”组成,可以在字符之间做切割,问是否可以在切割之后将T和H分别平分成两份。若能,输出需要切割的刀数以及分别在第几个字符处做切割;若不能,输出-1.。
解题思路:若H或T的个数为奇数个则一定不可能。若可以的话,将字符的第一个和最后一个接起来,组成一个圆。过圆心将圆平均分成两份,总有一个分法可以满足题意。如其中一份中的H占总字符串中H的一半,组T一定也占一半

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <set>

using namespace std;

char ch[100005];

int main()
{
int n;
while(~scanf("%d",&n)&&n)
{
scanf("%s",ch);
int H=0,T=0;
for(int i=0; i<n; i++)
{
if(ch[i]=='H') H++;
else T++;
}
if(H%2||T%2)
{
printf("-1\n");
continue;
}
int h=0,t=0;
for(int i=0; i<n/2; i++)
{
if(ch[i]=='H') h++;
else t++;
}
if(h==H/2&&t==T/2)
{
printf("1\n%d\n",n/2);
continue;
}
for(int i=n/2,j=0; i<n; i++,j++)
{
if(t==T/2&&h==H/2)
{
printf("2\n%d %d\n",j,i);
break;
}
if(ch[j]=='H') h--;
else t--;
if(ch[i]=='H') h++;
else t++;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: