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

HDU3363 Ice-sugar Gourd

2015-07-19 23:31 771 查看
Ice-sugar Gourd
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d
& %I64u

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

 

 

题意

给定一个由‘H’和‘T’组成的长度为 n 的字符串,判断能否把字符串分成‘H’和‘T’数量相等的两部分,若能,问最少需要截几次。输出次数和位置。若不能,输出 -1。

分析

计算出字符串中总的‘H’和‘T’的数量 h ,t,根据题意,当 h%2!=0||t%2!=0时,一定不能按要求平分,此时输出 -1 。当满足题目要求时,把整个字符串首尾相接看做一个环,则最少需要截一次,最多需要截两次。先记录 0 到 n/2 时的‘H’的个数记为 h1 ,‘T’的个数记为 t1 。截一次的情况为在整个字符串的中间,即 n/2 处,此时t1 == t/2 && h1 == h/2。当截取两次时,按照顺时针(或逆时针)转动,判断每次转进和转出的字符并且对 h1 和 t1 进行加减操作,当t1 ==
t/2 && h1 == h/2 时,满足条件。

 

AC代码如下

#include <cstdio>
#include <cstring>
#include <queue>
#define maxn 100000+10
using namespace std;

char str[maxn];

int main()
{
int n;
while(~scanf("%d",&n) && n)
{
queue <char> q;		//此处用队列完全是随手复习,不用队列直接判断即可
while(!q.empty()) q.pop();
int h = 0,t = 0;
for(int i = 0 ; i < n ; i++)
{
scanf(" %c",&str[i]);
if(str[i] == 'H') h++;
else t++;
}
if(h % 2 || t % 2)
{
printf("-1\n");
continue;
}
int t1 = 0 , h1 = 0;
for(int i = 0 ; i < n/2 ; i++)
{
if(str[i] == 'H') h1++;
else t1++;
q.push(str[i]);
}
if(h1 == h/2 && t1 == t/2)	//一次的情况
{
printf("1\n%d\n",n/2);
continue;
}
int pos1 = 0,pos2;
for(int i = n/2 ; i < n ; i++)  //两次的情况
{
char c = q.front();
if(c == 'H') h1--;
else t1--;
q.pop();
pos1++;
if(str[i] == 'H') h1++;
else t1++;
if(h1 == h / 2 && t1 == t / 2)
{
pos2 = i+1;
break;
}
}
printf("2\n%d %d\n",pos1,pos2);
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息