题解 | #单词倒排#

单词倒排

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;
    }


全部评论

相关推荐

牛客84809583...:举报了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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