Ice-sugar Gourd
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1490 Accepted Submission(s): 470
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.
![](https://i-blog.csdnimg.cn/blog_migrate/a83c4e7059f0631c3c7ed4dff97a8e90.jpeg)
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.
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
//这题可以看成一个环,求解,划分半圆,最多也就两刀,最少平分一刀 #include <iostream> #include<cstdio> #include<cstring> using namespace std; int n; char ch[100005]; int main() { while(~scanf("%d",&n)) { if (n==0) break; scanf("%s",&ch); if(n%2==1) { printf("-1\n"); continue;} int h=0,t=0; for(int i=0;i<n;i++) if (ch[i]=='H') h++; else t++; if (h%2!=0 || t%2!=0) {printf("-1\n"); continue;} int hh=0,tt=0; for(int i=0;i<n/2;i++) if (ch[i]=='H') hh++; else tt++; if (hh*2==h && tt*2==t) {printf("1\n%d\n",n/2); continue;} int i=0; int j=n/2-1; for(int k=1;k<=n/2;k++) { if(ch[i]=='H') hh--; else tt--; if(ch[j+1]=='H') hh++; else tt++; i++; j++; //上面部分不要用for循环再次求数量,应利用原来的数字,否则会tle if(hh*2==h && tt*2==t) { printf("2\n"); printf("%d %d\n",k,k+n/2); break; } } } return 0; }