NC15029 吐泡泡 栈 暴力模拟

吐泡泡

http://www.nowcoder.com/questionTerminal/f86fa2221c094b3d8d1fc79bae450d96

很容易想到栈

多组数据 !!! !!!

扫描整个字符串

  • 当栈空,则入栈
  • 循环如下 : 当栈顶和相等
    • 两个都是大写'O'就什么也不做,并
    • 两个都是小写'o'就不要,并把设置成大写'O'
string stk;
for(int i=0; i<n; ) { //注意这里不要i++
    if(!stk.empty() && str[i]=='o' && str[i]==TOP) {
        stk.pop_back();
        str[i] = 'O'; //把当前的'o'修改成大写'O'
    } else if(!stk.empty() && str[i]=='O' && str[i]==TOP) {
        stk.pop_back();
        i ++;
    } else {
        stk.push_back(str[i]);
        i ++;
    }
}

完整代码如下

#define debug
#ifdef debug
#include <time.h>
#include "/home/majiao/mb.h"
#endif

#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>

#define MAXN ((int)1e5+7)
#define ll long long int
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)

using namespace std;

#ifdef debug
#define show(x...) \
do { \
    cout << "\033[31;1m " << #x << " -> "; \
    err(x); \
} while (0)

void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }
#endif

#ifndef debug
namespace FIO {
    template <typename T>
    void read(T& x) {
        int f = 1; x = 0;
        char ch = getchar();

        while (ch < '0' || ch > '9') 
            { if (ch == '-') f = -1; ch = getchar(); }
        while (ch >= '0' && ch <= '9') 
            { x = x * 10 + ch - '0'; ch = getchar(); }
        x *= f;
    }
};
using namespace FIO;
#endif


int n, m, Q, K;

#define TOP (stk.back())

int main() {
#ifdef debug
    freopen("test", "r", stdin);
    clock_t stime = clock();
#endif

    string str;
    while(cin >> str) {
        n = (int)str.length();
        string stk;
        for(int i=0; i<n; ) {
            if(!stk.empty() && str[i]=='o' && str[i]==TOP) {
                stk.pop_back();
                str[i] = 'O';
            } else if(!stk.empty() && str[i]=='O' && str[i]==TOP) {
                stk.pop_back();
                i ++;
            } else {
                stk.push_back(str[i]);
                i ++;
            }
        }
        cout << stk << endl;
    }



#ifdef debug
    clock_t etime = clock();
    printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif 
    return 0;
}




全部评论

相关推荐

牛客5655:其他公司的面试(事)吗
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务