题解 | #单词倒排#

单词倒排

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

#include <iostream>
#include <stack>
#include <string>
using namespace std;
class stack<string> st;
int check(){
    string s;
    s=st.top();
    st.pop();
    int t=0;
    for(int i=0;i<s.length();i++){
        if (((s[i]>='a')&&(s[i]<='z'))||((s[i]>='A')&&(s[i]<='Z'))) {
        }else {
            st.push(s.substr(t,i-t));
            t=i+1;
        }
    }
    if (t==0) {
        st.push(s);
    }else {
        st.push(s.substr(t,s.length()-t));
    }
    return 0;
}
int main() {
    string s;
    char c;
    while (cin >> s) { 
            st.push(s);
    }
    while (!st.empty()) {
        check();
        cout<<st.top()<<' ';
        st.pop();
    }
}

全部评论

相关推荐

2025-11-08 21:07
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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