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
大致题意:
给以个长度为n字符串,最少切几次使‘H’与‘T’能均分为两份;
 
第一行输出次数;
第二行输出切的位置;
一开始写的时候老是输出超限,认为可能要多次;后来发现最多切两次就可以使‘H’与‘T’均分;
先判断前半部分是不是‘H’是否是‘H’总数的一半,‘T’的个数是不是‘T’
的总数的一半,如果相等直接从中间切;
如果不相等再从后半查找直到H是其总数的一半T是其总数的一半
#include <bits/stdc++.h>
using namespace std;
char s[100000+10];
//vector <int> p;
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF&&n)
    {
        getchar();
        gets(s);
        int h=0,t=0;
        bool ok=true;
        for(int i=0; i<n; i++)
        {
            if(s[i]=='T')
                t++;
            else if(s[i]=='H')
                h++;
        }
        if(t%2!=0||h%2!=0)
        {
            printf("-1\n");
            continue;
        }
        else if(t%2==0&&h%2==0)
        {
            int h_=0,t_=0,i;
            for( i=0; i<n/2; i++)
            {
                if(s[i]=='H')
                    h_++;
                else if(s[i]=='T')
                    t_++;
            }
            if(h_*2==h&&t_*2==t)
            {
                printf("1\n");
                printf("%d\n",n/2);
                continue;
            }
            else
            {
                bool flag=false;
                for(int j=n/2,l=0; j<n,l<n/2; j++,l++)
                {
                    if(s[j]=='H')
                        h_++;
                    else if(s[j]=='T')
                        t_++;
                    if(s[l]=='H')
                        h_--;
                    else if(s[l]=='T')
                        t_--;
                    if(t_*2==t&&h_*2==h)
                    {
                        printf("2\n");
                        printf("%d %d\n",l+1,j+1);
                        flag=true;
                        break;
                    }
                }
                if(flag)
                    continue;
            }

        }
        printf("-1\n");
    }
    return 0;
}

 
 
 
posted @ 2016-07-26 17:55  AC_dream  阅读(530)  评论(0编辑  收藏  举报