枚举n/2个区间,判断 区间内的h和t是不是总数一半,是的话区间左边切一刀,右边切一刀( 一共最多切两刀)
题目:
Ice-sugar Gourd
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 740 Accepted Submission(s): 264
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.
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
Recommend
lcy
代码:
1 //Sat Jan 11 13:25:58 2014 2 //Author:Minshik 3 #include <iostream> 4 #include <algorithm> 5 #include <cstring> 6 #include <cstdio> 7 #include <string> 8 #include <vector> 9 #include <set> 10 #include <stack> 11 #include <queue> 12 #include <deque> 13 #include <memory.h> 14 #include <cctype> 15 #include <cmath> 16 #include <list> 17 using namespace std; 18 19 char sw[100000+10]; 20 int pH[100000+10]; 21 int pT[100000+10]; 22 int main() 23 { 24 freopen("IN","r",stdin); 25 std::ios::sync_with_stdio(false); 26 int n; 27 while(cin>>n && n!=0) 28 { 29 int sh=0,st=0; 30 int ca=0,cb=0; 31 cin.ignore(); 32 for(int i=0;i<n;i++) 33 { 34 cin>>sw[i]; 35 if(sw[i]=='H')sh++; 36 } 37 st= n-sh; 38 39 //cout<<sw<<endl; 40 if(st%2==1 || sh%2==1)cout<< "-1"<<endl; 41 else 42 { 43 int ppt=0,pph=0; 44 45 for(int j=0;j<n/2;j++) 46 { 47 if(sw[j]=='H')pph++; 48 if(sw[j]=='T')ppt++; 49 50 } 51 int flag =false; 52 if(ppt==st/2 && pph==sh/2){cout<<'1'<<endl<<n/2<<endl;flag=true;} 53 54 if(!flag) 55 for(int i=1;i+n/2<n;i++) 56 { 57 if(sw[i-1]=='H'){pph--;} 58 else ppt--; 59 60 if(sw[i+n/2-1]=='H')pph++; 61 else ppt++; 62 63 //cout<<"okok"<<i<<' '<<i+n/2-1<<' '<<pph<<' '<<ppt<<endl; 64 if(ppt==st/2 && pph==sh/2){cout<<'2'<<endl<<i<<' '<<i+n/2<<endl;flag=true;break;} 65 } 66 67 if(!flag) 68 cout<<'-1'<<endl; 69 70 } 71 72 } 73 74 75 return 0; 76 }