题解 | #单词倒排#

单词倒排

https://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836

#include <iostream>
#include <string>
#include <stack>
using namespace std;

int main() {
    string s;
    while (getline(cin, s)) { // 注意 while 处理多个 case
        int n = s.length();
        stack<char> stk;
        for (int i = n - 1; i >= 0; i--) {
            int Ascii = s[i] - 'A';
            int ascii = s[i] - 'a';
            if ((Ascii >= 0 && Ascii < 26) || (ascii >= 0 && ascii < 26)) {
                stk.push(s[i]);
            } else if (!stk.empty()) {
                while (!stk.empty()) {
                    cout << stk.top();
                    stk.pop();
                }
                cout << " ";
            }
        }
        while (!stk.empty()) {
            cout << stk.top();
            stk.pop();
        }
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务