Small input 10 points |
Solve B-small
|
Large input 10 points |
Solve B-large
|
The Infinite House of Pancakes has just introduced a new kind of pancake! It has a happy face made of chocolate chips on one side (the "happy side"), and nothing on the other side (the "blank side").
You are the head waiter on duty, and the kitchen has just given you a stack of pancakes to serve to a customer. Like any good pancake server, you have X-ray pancake vision, and you can see whether each pancake in the stack has the happy side up or the blank side up. You think the customer will be happiest if every pancake is happy side up when you serve them.
You know the following maneuver: carefully lift up some number of pancakes (possibly all of them) from the top of the stack, flip that entire group over, and then put the group back down on top of any pancakes that you did not lift up. When flipping a group of pancakes, you flip the entire group in one motion; you do not individually flip each pancake. Formally: if we number the pancakes 1, 2, ..., N from top to bottom, you choose the top ipancakes to flip. Then, after the flip, the stack is i, i-1, ..., 2, 1, i+1, i+2, ..., N. Pancakes 1, 2, ..., i now have the opposite side up, whereas pancakes i+1, i+2, ..., N have the same side up that they had up before.
For example, let's denote the happy side as +
and the blank side as -
. Suppose that the stack, starting from the top, is --+-
. One valid way to execute the maneuver would be to pick up the top three, flip the entire group, and put them back down on the remaining fourth pancake (which would stay where it is and remain unchanged). The new state of the stack would then be -++-
. The other valid ways would be to pick up and flip the top one, the top two, or all four. It would not be valid to choose and flip the middle two or the bottom one, for example; you can only take some number off the top.
You will not serve the customer until every pancake is happy side up, but you don't want the pancakes to get cold, so you have to act fast! What is the smallest number of times you will need to execute the maneuver to get all the pancakes happy side up, if you make optimal choices?
The first line of the input gives the number of test cases, T. T test cases follow. Each consists of one line with a string S, each character of which is either +
(which represents a pancake that is initially happy side up) or -
(which represents a pancake that is initially blank side up). The string, when read left to right, represents the stack when viewed from top to bottom.
For each test case, output one line containing Case #x: y
, where x
is the test case number (starting from 1) and y
is the minimum number of times you will need to execute the maneuver to get all the pancakes happy side up.
1 ≤ T ≤ 100.
Every character in S is either +
or -
.
1 ≤ length of S ≤ 10.
1 ≤ length of S ≤ 100.
In Case #1, you only need to execute the maneuver once, flipping the first (and only) pancake.
In Case #2, you only need to execute the maneuver once, flipping only the first pancake.
In Case #3, you must execute the maneuver twice. One optimal solution is to flip only the first pancake, changing the stack to --
, and then flip both pancakes, changing the stack to ++
. Notice that you cannot just flip the bottom pancake individually to get a one-move solution; every time you execute the maneuver, you must select a stack starting from the top.
In Case #4, all of the pancakes are already happy side up, so there is no need to do anything.
In Case #5, one valid solution is to first flip the entire stack of pancakes to get +-++
, then flip the top pancake to get --++
, then finally flip the top two pancakes to get ++++
.
#include<stdio.h> #include<iostream> #include<string.h> #include<string> #include<ctype.h> #include<math.h> #include<set> #include<map> #include<vector> #include<queue> #include<bitset> #include<algorithm> #include<time.h> using namespace std; #define MS(x,y) memset(x,y,sizeof(x)) #define MC(x,y) memcpy(x,y,sizeof(x)) #define MP(x,y) make_pair(x,y) #define ls o<<1 #define rs o<<1|1 typedef long long LL; typedef unsigned long long UL; typedef unsigned int UI; template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; } template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; } const int N = 1010, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f; int casenum, casei; void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); } char s[105]; int main() { //fre(); scanf("%d", &casenum); for (casei = 1; casei <= casenum; ++casei) { scanf("%s", s + 1); int n = strlen(s + 1); int ans = 0; for (int i = 1; i <= n; ++i)if (s[i - 1] != s[i])++ans; if (s[n] == '+')--ans; printf("Case #%d: %d\n", casei, ans); } } /* 【题意】 给你一个字符串。 字符串中仅有'-'和'+' 我们每次可以选择其一前缀,使得前缀'-'变为'+','+'变为'-'且前后反转。 问你最小的操作次数,使得这个字符串变为全'+' 【类型】 贪心 【分析】 首先,我们发现, 我们把字符串中,连续的'+'或'-'定义为同一个联通块。 那么答案肯定至少为联通块个数-1 为什么呢? 因为我们每一步翻转,最多消除一个联通块。这是毫无置疑的。 然而,可以把答案确定为联通块个数-1吗? NO!这个却是我们做不到的。 比如对于"-",答案就必须要为1。 于是策略需要是—— 1,必须每步消除一个联通块 2,尽量使得,在最后一步消除后,全部都为'+' 然而,2不是我们想尽量就能尽量的,它是取决于最后一个字符是'+'还是'-' 如果最后一个字符是'-',我们的步数还要+1。由此得出最后的ans 【时间复杂度&&优化】 O(n) */