Google Code Jam-Problem B. Reverse Words

Problem B. Reverse Words

Problem

Given a list of space separated words, reverse the order of the words. Each line of text contains L letters and W words. A line will only consist of letters and space characters. There will be exactly one space character between each pair of consecutive words.

Input

The first line of input gives the number of cases, N.
N test cases follow. For each test case there will a line of letters and space characters indicating a list of space separated words. Spaces will not appear at the start or end of a line.

Output

For each test case, output one line containing "Case #x: " followed by the list of words in reverse order.

Limits

Small dataset

N = 5
1 ≤ L ≤ 25

Large dataset

N = 100
1 ≤ L ≤ 1000

Sample


Input 

Output 
3
this is a test
foobar
all your base
Case #1: test a is this
Case #2: foobar
Case #3: base your all

#include <iostream>
#include <vector>
using namespace std;
#define IN 1
#define OUT 0

class reverseWord {
    
    vector<string> strVector;
    size_t loopNum;
    vector<vector<string> > wordVector;
    
public:
    
    void inputStr(){
        cin>>loopNum;
        getchar(); //getchar() get the '\n' that left in buffer.
//if there is no getchar(),getline() will get the '\n' in buffer and get null string
        string str;
        
        size_t count=loopNum;
        while (count--) {
            getline(cin,str);
            strVector.push_back(str);
        }
        
    }
    
    void printVector(){
        int count=0;
        for (vector<vector<string> >::const_iterator it=wordVector.begin();it!=wordVector.end(); ++it) {
            cout<<"Case #"<<count++<<": ";
            
            for (vector<string>::const_iterator to=it->begin(); to!=it->end(); ++to) {
                cout<<*to<<" ";
            }
            
            cout<<endl;
            }
            
        
    }//printVector
    
    void seperate(){
        vector<string> tempVector;
        for (int t=0; t<loopNum;++t ) {
            string str=strVector[t];
            size_t length=str.length();
            bool state=OUT;
            size_t record;
            string temp;
            
            for (size_t i=0; i<length; ++i) {
                
                if (str[i]==' '||str[i]=='\n') {
                    state=OUT;
                    temp=str.substr(record,i-record);
                    tempVector.push_back(temp);
                }
                else if(state==OUT){
                    state=IN;
                    record=i;
                }
                
            }//for
            
            temp=str.substr(record,length-record);
            tempVector.push_back(temp);
            wordVector.push_back(tempVector);
            tempVector.clear();
            
        }//for
    }// seperate end
    
    void reverse(){
        //make word in wordVector reverse
        for(size_t i=0;i<loopNum;++i){
            size_t length=wordVector[i].size();
            int begin=0;
            
            while (begin<length) {
                string temp;
                temp=wordVector[i][begin];
                wordVector[i][begin++]=wordVector[i][--length];
                wordVector[i][length]=temp;
            }
        }//for
        
    }//reverse end
    
};

int main(){
    
    reverseWord test;
    
    // input the string
    test.inputStr();
    
    //seperate the string and save it to wordVector
    test.seperate();
     test.reverse();
     test.printVector();
    return 0;
}

getchar() get the '\n' that left in buffer.
//if there is no getchar(),getline() will get the '\n' in buffer and get null string

这是一段 C++ 代码,以下是每行的解释: - `if(NULL != vl)`:如果 `vl` 不为空指针,则执行下面的代码块。 - `{`:代码块的开始。 - `//reverse start;`:注释,表示下面的代码是开始逆向处理。 - `if(rphead && ::is_open_reverse)`:如果 `rphead` 不为空并且全局变量 `::is_open_reverse` 为真,则执行下面的代码块。 - `{`:代码块的开始。 - `rphead->CdrRaw.ncdrid = cdrgetid(rphead->lcoreid);`:将 `rphead->lcoreid` 作为参数调用函数 `cdrgetid` 获得新的 CDR ID (`ncdrid`),并将其赋值给 `rphead->CdrRaw.ncdrid`。 - `rphead->CdrRaw.tstart.tm_cycles = rphead->tstart.tm_cycles;`:将 `rphead->tstart.tm_cycles` 的值赋值给 `rphead->CdrRaw.tstart.tm_cycles`,即将逆向处理的开始时间设置为原始处理的开始时间。 - `rphead->CdrRaw.cdrstat = PACKET_BEGIN;`:将 `rphead->CdrRaw.cdrstat` 的值设置为 `PACKET_BEGIN`,表示逆向处理的 CDR 状态为开始。 - `rphead->btCurStaus = PACKET_BEGIN;`:将 `rphead->btCurStaus` 的值设置为 `PACKET_BEGIN`,表示逆向处理的当前状态为开始。 - `pubSendPkt((void*)rphead);`:调用函数 `pubSendPkt` 发送 `rphead`,即逆向处理的 CDR。 - `vl->SetReverse(rphead->CdrRaw.ncdrid, rphead->CdrRaw.tstart.tm_cycles);`:调用 `vl` 对象的 `SetReverse` 函数,将逆向处理的 CDR ID 和开始时间存储到 `vl` 对象中。 - `}`:代码块的结束。 - `return vl;`:返回 `vl` 对象。 总体来说,这段代码的作用是对一个 CDR 进行逆向处理,并将逆向处理的 CDR ID 和开始时间存储到一个对象中。其中 `rphead` 是一个指向 CDR 的指针,`vl` 是一个对象,`cdrgetid` 是一个函数,`pubSendPkt` 和 `SetReverse` 是两个函数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值