zoukankan      html  css  js  c++  java
  • 牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上。同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思。例如,“student. a am I”。后来才意识到,这家伙原来把句子单词的顺序翻转了,正确的句子应该是“I am a student.”。Cat对一一的翻转这些单词顺序可不在行,你能帮助他么?

    // test20.cpp : 定义控制台应用程序的入口点。
    //

    #include "stdafx.h"
    #include<iostream>
    #include<vector>
    #include<string>
    #include<queue>
    #include<stack>
    #include<cstring>
    #include<string.h>
    #include<deque>
    
    using namespace std;
    
    class Solution {
    public:
    	string ReverseSentence(string str) {
    		//把单词翻转 利用stack
    		//连接成一个新的单词
    		stack<string> st;
    		string word;
    		string s;
    		int flag = 0;
    		
    		//if (flag < str.length())
    		//{
    		//	word = str.substr(0, flag);
    		//	str = str.substr(flag+1);//存储剩余单词
    		//	st.push(word);
    		//}
    		flag = str.find_first_of(" ");
    		while (flag < str.length())
    		{
    			word = str.substr(0, flag);
    			cout << "word:" << word << endl;
    			str = str.substr(flag + 1);//存储剩余单词
    			st.push(word);
    			flag = str.find_first_of(" ");
    		}
    		st.push(str);
    
    		while (st.size()!=1)
    		{
    			s.append(st.top()+" ");
    			st.pop();
    		}
    		s.append(st.top() + " ");
    		st.pop();
    
    		return s;
    	}
    };
    int main()
    {
    	
    	Solution so;
    	string str = "student. a am I";
    	
    	string s = so.ReverseSentence(str);
    	cout << s << endl;
    
    	cout << endl;
    	return 0;
    }
  • 相关阅读:
    supervised learning 监督式学习
    4.4 day14 内置函数
    4.3 day13 迭代器 生成器
    4.2 homework
    4.2 day12 装饰器-带参数
    3.29 homework
    SQL Server 索引和视图
    SQL Server 事务、异常和游标
    SQL Server 触发器
    SQL Server 存储过程
  • 原文地址:https://www.cnblogs.com/wdan2016/p/6029083.html
Copyright © 2011-2022 走看看