2016資格輪 Problem B. Revenge of the Pancakes

Problem

廚房給每個烙餅都只涂一面的巧克力怜校,烙餅涂抹巧克力的一面叫做happy side了牛,而沒有涂抹巧克力的一面叫做blank side∥赏瘢現(xiàn)在有一堆烙餅药版,疊在一起,你可以通過x光可以看到烙餅有巧克力的面是朝上還是朝下喻犁,你希望給客人的時候所有的烙餅都是happy side槽片。
你可以拿一部分或整堆烙餅,把他們完整翻個面肢础,再放下还栓,
所以假設(shè)一堆烙餅是--+-,那么翻完top3之后是-++-传轰。
最終的要求是剩盒,在最短的翻滾次數(shù)里把所有的餅都翻成正面朝上。

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 i pancakes 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?

Input

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.

Output

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.

Limits

1 ≤ T ≤ 100.
Every character in S is either + or -.

Small dataset

1 ≤ length of S ≤ 10.
Large dataset

1 ≤ length of S ≤ 100.
Sample


Input        Output 
5
-            Case #1: 1
-+           Case #2: 1
+-           Case #3: 2      
+++          Case #4: 0
--+-         Case #5: 3

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 ++++.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末慨蛙,一起剝皮案震驚了整個濱河市辽聊,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌期贫,老刑警劉巖跟匆,帶你破解...
    沈念sama閱讀 221,273評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異通砍,居然都是意外死亡玛臂,警方通過查閱死者的電腦和手機烤蜕,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,349評論 3 398
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來迹冤,“玉大人讽营,你說我怎么就攤上這事∨葆悖” “怎么了橱鹏?”我有些...
    開封第一講書人閱讀 167,709評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長堪藐。 經(jīng)常有香客問我蚀瘸,道長,這世上最難降的妖魔是什么庶橱? 我笑而不...
    開封第一講書人閱讀 59,520評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮贪惹,結(jié)果婚禮上苏章,老公的妹妹穿的比我還像新娘。我一直安慰自己奏瞬,他們只是感情好枫绅,可當(dāng)我...
    茶點故事閱讀 68,515評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著硼端,像睡著了一般并淋。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上珍昨,一...
    開封第一講書人閱讀 52,158評論 1 308
  • 那天县耽,我揣著相機與錄音,去河邊找鬼镣典。 笑死兔毙,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的兄春。 我是一名探鬼主播澎剥,決...
    沈念sama閱讀 40,755評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼赶舆!你這毒婦竟也來了哑姚?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,660評論 0 276
  • 序言:老撾萬榮一對情侶失蹤芜茵,失蹤者是張志新(化名)和其女友劉穎叙量,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體夕晓,經(jīng)...
    沈念sama閱讀 46,203評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡宛乃,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,287評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片征炼。...
    茶點故事閱讀 40,427評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡析既,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出谆奥,到底是詐尸還是另有隱情眼坏,我是刑警寧澤,帶...
    沈念sama閱讀 36,122評論 5 349
  • 正文 年R本政府宣布酸些,位于F島的核電站宰译,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏魄懂。R本人自食惡果不足惜沿侈,卻給世界環(huán)境...
    茶點故事閱讀 41,801評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望市栗。 院中可真熱鬧缀拭,春花似錦、人聲如沸填帽。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,272評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽篡腌。三九已至褐荷,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間嘹悼,已是汗流浹背叛甫。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留绘迁,地道東北人合溺。 一個月前我還...
    沈念sama閱讀 48,808評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像缀台,于是被迫代替她去往敵國和親棠赛。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,440評論 2 359

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,556評論 0 23
  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,489評論 5 6
  • 跨過一座很窄的鐵索橋膛腐,又開始向上攀爬睛约,走了大約40分鐘的時間,我終于發(fā)現(xiàn)哲身,在目力所及的正上方很遠(yuǎn)處一個男孩在向我招...
    小花fayer閱讀 278評論 0 0
  • 我是春天的孩子辩涝,頭戴小花,腳步嫩綠勘天。 我是夏天的孩子怔揩,手遮小臉捉邢,知了歡叫。 我是秋天的孩子商膊,擎著小傘伏伐,淋濕發(fā)梢。 ...
    午夜航行閱讀 151評論 0 0
  • 正在緊鑼密鼓的爭取最后半小時刷題晕拆,朋友阿賢發(fā)來微信藐翎,說她跟男朋友分手了。我很驚訝实幕,因為上周末還跟他一塊吃飯...
    北夢沐曦閱讀 250評論 0 2