hdu 3363

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

題意:分糖葫蘆,使得分的的兩部分,橙子和山楂的數量相同,問最少需要切多少刀,輸出切得位置。

思路:一開還以爲是求連續段或者是迴文,後來試了好幾組數據,發現,要分成兩部分的話,就是找出含有一半橙子和山楂的段,要麼中間一分剛剛好,要麼切兩刀,中間的剛好夠數,前後的湊一組就行了。又一次弄成了規律題,處理一下就行了。再就是不能分的情況,奇數個橘子或者山楂肯定不行,上面的方法沒找到切得地方也就不行了。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int n;
char s[102400];
int ok;
int main()
{
    while(scanf("%d",&n)&&n)
    {
        scanf("%s",s);
        if(n&1)
        {
            printf("-1\n");
            continue;
        }
        int n1 = 0,n2 = 0;
        for(int i = 0;i < n;i++)
        {
            if(s[i] == 'H')
                n1++;
            else
                n2++;
        }
        if(n1%2 == 1||n2%2 == 1)
        {
            printf("-1\n");
            continue;
        }
        int x = 0,y = 0;
        for(int i = 0;i < n/2;i++)
        {
            if(s[i] == 'H')
                x++;
            else
                y++;
        }
        if(x==n1/2&&y == n2/2)
        {
            printf("1\n%d\n",n/2);
            continue;
        }
        ok = 0;
        for(int i = 0,j = n/2;i<n/2;i++,j++)
        {
            if(s[j] == 'H')
                x++;
            else
                y++;
            if(s[i] == 'H')
                x--;
            else
                y--;
            if(x == n1/2&&y == n2/2)
            {
                printf("2\n%d %d\n",i+1,j+1);
                ok = 1;
                break;
            }
        }
        if(!ok)
        {
            printf("-1\n");
        }
    }
    return 0;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章