题解 | 单词倒排

单词倒排

https://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836?tpId=37&tqId=38366&rp=1&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=

#include <iostream>
#include <string>
 
using namespace std;
 
int main()
{
    char c;
    string ans;
    string word;
    while(cin.get(c))//逐个字符输入
    {
        if(isalpha(c)) word+=c;//判断是否是英文字母,如果是的话加到单词里
        else{
            if(!word.empty()){//若有单词还没加入
                if(!ans.empty()){//ans不为空,word加入的时候需要添加空格
                    ans=word+' '+ans;
                }else{//若ans为空,直接加入word
                    ans=word;
                }
            }
            word.clear();//word已经加入了结果中,清空
        }
    }
    cout<<ans<<endl;
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

01-30 22:03
门头沟学院 Java
用微笑面对困难:我滴妈,【俩月】【实习】【主管】仨debuff吃满了,独立设计开发的项目写了绝大占比的运营板块,你独立开发,那维护、问题复盘、日志更新、bug、策划书全是自己整的? 不建议写那么大,可以从小出发更容易
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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