HDU 3305 Ice-sugar Gourd

Source: Internet
Author: User

Ice-sugar gourd

Time limit:5000/2000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 936 Accepted Submission (s): 329


Problem Descriptionice-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 would be like a both sour and sweet ice if you taste it. You're making your mouth water, aren ' t?

I have made a huge ice-sugar gourd by the kinds of fruit, Hawthorn and tangerine, in no particular order. Since I want to share it with II of my friends, Felicia and his girl friend, I need to get an equal cut of the Hawthorns and Tangerines. How many times would I have to cut the stick so, each of my friends gets half the Hawthorns and half the Tangerines? Please notice so can only cut the stick between and the adjacent fruits, that's you cannot cut a fruit in half as this Fru It would is no good to eat.

Inputthe input consists of multiply test cases. The first line of all test case contains a integer, n (1 <= n <= 100000), indicating the number of the fruits on th E 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 was followed by a single line containing one zero.

Outputoutput 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 this cuts on the next line, separated by one space. If you cut the stick after the i-th (indexed from 1) fruit and then you should output number I to indicate this cut. If there is 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 input4hhtt4htht4hhht0

Sample Output21 312-1 starts with a double for loop, time out, or an array of nh[i] to record how many h I have and then the result from the Go
#include <math.h>#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>using namespacestd;#defineN 123456intn,cnt_h,cnt_t;CharA[n];intNh[n];intMain () { while(~SCANF ("%d",&N)) {GetChar (); if(n==0) Break; Cnt_h=0;  for(intI=0; i<n;i++) {scanf ("%c",&A[i]); if(a[i]=='H') cnt_h++; Nh[i]=Cnt_h; }        if(n&1) {cout<<-1<<Endl; Continue; }        if(cnt_h&1) {cout<<-1<<Endl; Continue; }        if(nh[n/2-1]==cnt_h/2) {printf ("1\n%d\n", n/2); Continue; }         for(intI=1; i<n;i++)        {            if(nh[n/2+i-1]-nh[i-1]==cnt_h/2) {printf ("2\n%d%d\n", i,i+n/2);  Break; }        }    }    return 0;}//freopen ("1.txt", "R", stdin); //freopen ("2.txt", "w", stdout);//**************************************

HDU 3305 Ice-sugar Gourd

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.