BNUOJ44586(栈模拟)

来源:互联网 发布:pdf.js 获取当前页数 编辑:程序博客网 时间:2024/05/17 04:32

题目链接:http://www.bnuoj.com/v3/problem_show.php?pid=44586


解题思路:

用栈来模拟,考虑时候要注意字母为奇数时才消除。如果当前栈顶为奇数,那么栈顶+1 == s[ i ]的话才可以入栈。如果当前s[ i ]为奇数的话,那么s[ i ]  + 1 == 栈顶,s[ i ]才可以消除。

简而言之,我们要考虑的字母 i 要是奇数,并且只有 i 和 i+ 1 才是可消除的。


完整代码:

#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <sstream>#include <iomanip>#include <numeric>#include <cstring>#include <climits>#include <cassert>#include <complex>#include <cstdio>#include <string>#include <vector>#include <bitset>#include <queue>#include <stack>#include <cmath>#include <ctime>#include <list>#include <set>#include <map>using namespace std;#pragma comment(linker, "/STACK:102400000,102400000")typedef long long LL;typedef double DB;typedef unsigned uint;typedef unsigned long long uLL;/** Constant List .. **/ //{const int MOD = int(1e9)+7;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f3f3f3f3f3f3fLL;const DB EPS = 1e-9;const DB OO = 1e20;const DB PI = acos(-1.0); //M_PI;string s;stack <char> m;int main(){    #ifdef DoubleQ    freopen("in.txt","r",stdin);    #endif    std::ios::sync_with_stdio(false);    std::cin.tie(0);    int T;    cin >> T;    while(T--)    {        cin >> s;        int len = s.length();        for(int i = 0 ; i < len ; i ++)        {                if( !m.empty() && ( ( (m.top() - 'a' + 1) % 2 != 0 && m.top() - 'a' + 2 == s[i] - 'a' + 1 ) ||                  ( ( s[i] - 'a' + 1) % 2 != 0   && m.top() - 'a' + 1 == s[i] - 'a' + 2 )    ) )                       m.pop();                else                    m.push(s[i]);        }        if(m.empty())        {            cout << "sad!" << endl;            continue;        }        string t = "";        while( !m.empty() )        {            t += m.top();            m.pop();        }        int lent = t.length();        for(int i = lent - 1 ; i >= 1 ; i -- )            cout << t[i];        cout << t[0] << endl;        s = "";    }}


0 0