题解 | #单词倒排#

单词倒排

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

可以通过把其它字符替换为空格,利用输入符自动去掉空格来完成,但是这道题有个坑,看代码

#include <string>
#include <map>
#include <vector>
#include <sstream>

using namespace std;

int main() {
    string str;
    getline(cin ,str) ;//只能用getline,不能while(cin>>str),不信你试试
        for (int i = 0; i < str.size(); i++)
        {
            if (!isalpha(str[i])) str[i] = ' ';
        }
        stringstream input(str);//这里只能有一个流如果有其他的流,它遇到空白会跳出,但只有一个流他就会一直输入
        //str.clear();
        string word,temp;
        while (input >> word) {
            if (temp.empty())temp = word;
            else temp = word + " " + temp;

        }
        cout << temp;
    }


全部评论

相关推荐

字节一直是我的白月光,考虑到转正还是拒了日常实习。
从今天开始狠狠卷JV...:为什么你释放的offer没流到我头上
点赞 评论 收藏
分享
争当牛马还争不上
码农索隆:1.把简历改哈 2.猛投,狠投 3.把基础打牢 这样你在有机会的时候,才能抓住
点赞 评论 收藏
分享
每晚夜里独自颤抖:把华北改为华南再试一试,应该就没啥问题了。改完可能都不用投,别人主动联系了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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