Google online test exercise 2

Source: Internet
Author: User

Problem

Given a list of space separated words, reverse the order of the words. Each line of text containsLLetters andWWords. 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.
NTest 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

Idea: first, one word is reversed, and then the entire string is reversed.

#include<iostream>#include<fstream>#include<string>using namespace std;int main(){int n_case;ifstream ifile("B-large-practice.in");ofstream ofile("resultb2.txt");ifile >> n_case;for(int i = 0; i <= n_case; i++){char line[1002];ifile.getline(line, 1002);string words(line);int begin = 0;int end = 0;if(i == 0)continue;for(int i = 1; i < words.length(); i++){if(words[i] ==  ' '){int sum = begin + i - 1;for(int j = begin; j <= sum / 2; j++){char tmp = words[j];words[j] = words[sum - j];words[sum - j] = tmp;}begin = i + 1;}}int sum = begin + words.length() - 1;for(int j = begin; j <= sum / 2; j++){char tmp = words[j];words[j] = words[sum - j];words[sum - j] = tmp;}sum = words.length() - 1;for(int j = 0; j <= sum / 2; j++){char tmp = words[j];words[j] = words[sum - j];words[sum - j] = tmp;}ofile << "Case #" << i << ": " << words << endl;}return 0;}


Google online test exercise 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.