呢兩日要做野, 唔得閒玩.
今晚有D時間, 解左個百痴oversize pancake flipper 的 small set case:
YEAH! 好好玩~
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class PancakeFlipper{
public:
string pancake; // The pancake string
int flipperSize; // The flipper size
int flipCnt; // The flip counter
bool pass; // If all happy
PancakeFlipper(); // Always a constructor
// Acceupt the pancake from the world;
void in(string inPancake, int inFlipperSize);
// Output the pancake
void out();
// Cook the pancake, make it all happy side
void cook();
private:
// Check if the cooked pancake are all happy size
void qc();
// Flip the pancake with the filpper
void flip(int position);
void flip(char &pancake);
};
// Empty constructor
PancakeFlipper:

ancakeFlipper(){
}
// Take the pancake
void PancakeFlipper::in(string inPancake, int inFlipperSize) {
pancake = inPancake;
flipperSize = inFlipperSize;
flipCnt = 0;
pass = true;
}
// Output the pancake
void PancakeFlipper::out() {
cout << pancake;
}
// Flip the pancake to happy side by the oversize flipper
void PancakeFlipper::cook() {
int len = pancake.length();
for(int i=0;i<=len-flipperSize;i++){
if(pancake[i] == '-' && i + flipperSize <= len ){
flip(i);
}
}
// Check if all hapoy face
qc();
}
// If all happy face, set qc pass to true
void PancakeFlipper::qc() {
int len = pancake.length();
for(int i=0;i<len;i++){
if(pancake[i] == '-'){
pass = false;
break;
}
}
}
// Flip pancake with oversize pancke flipper
void PancakeFlipper::flip(int pos) {
for(int j=0;j<flipperSize;j++){
this->flip(pancake[pos+j]);
}
flipCnt++;
}
// Flip single pancake in poistion
void PancakeFlipper::flip(char &pancake) {
if(pancake == '-'){
pancake = '+';
}else{
pancake = '-';
}
}
int main() {
PancakeFlipper* pf = new PancakeFlipper();
int t, k;
string s;
cin >> t;
for (int i = 1; i <= t; i++) {
// read pancake fase and flipper size
cin >> s >> k;
// Take the pancake
pf->in(s,k);
// cock the pancake
pf->cook();
// Output
cout << "Case #" << i << ": ";
//cout << std::left << std::setw(11) << s;
//cout << std::left << std::setw(4) << k;
//pf->out();
if(pf->pass){
cout << pf->flipCnt;
}else{
cout << "IMPOSSIBLE";
}
cout << endl;
}
return 0;
}