google在线测试练习题2

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

思路:先一个一个单词反转,再将整个字符串反转。

#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;
}
时间: 2024-10-17 01:27:13

google在线测试练习题2的相关文章

google在线测试练习题1

Problem You receive a credit C at a local store and would like to buy two items. You first walk through the store and create a list L of all available items. From this list you would like to buy two items that add up to the entire value of the credit

google在线测试练习题3

Problem The Latin alphabet contains 26 characters and telephones only have ten digits on the keypad. We would like to make it easier to write a message to your friend using a sequence of keypresses to indicate the desired characters. The letters are

google在线测试练习2

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

动态规划 Dynamic Programming

March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: 自由转载-非商用-非衍生-保持署名|Creative Commons BY-NC-ND 3.0 ,转载请注明作者及出处. 前言 本文翻译自TopCoder上的一篇文章: Dynamic Programming: From novice to advanced ,并非严格逐字逐句翻译,其中加入了自己的

动态规划:从新手到专家

http://www.hawstein.com/posts/dp-novice-to-advanced.html 前言 本文翻译自TopCoder上的一篇文章: Dynamic Programming: From novice to advanced ,并非严格逐字逐句翻译,其中加入了自己的一些理解.水平有限,还望指摘. 前言_ 我们遇到的问题中,有很大一部分可以用动态规划(简称DP)来解. 解决这类问题可以很大地提升你的能力与技巧,我会试着帮助你理解如何使用DP来解题. 这篇文章是基于实例展开

[转]动态规划

前言_ 我们遇到的问题中,有很大一部分可以用动态规划(简称DP)来解.解决这类问题可以很大地提升你的能力与技巧,我会试着帮助你理解如何使用DP来解题.这篇文章是基于实例展开来讲的,因为干巴巴的理论实在不好理解. 注意:如果你对于其中某一节已经了解并且不想阅读它,没关系,直接跳过它即可. 简介(入门) 什么是动态规划,我们要如何描述它? 动态规划算法通常基于一个递推公式及一个或多个初始状态.当前子问题的解将由上一次子问题的解推出.使用动态规划来解题只需要多项式时间复杂度,因此它比回溯法.暴力法等要

dp新手入门

网址转载链接:  http://bbs.chinaunix.net/thread-4094539-1-1.html 动态规划:从新手到专家 Hawstein翻译 前言 我们遇到的问题中,有很大一部分可以用动态规划(简称DP)来解. 解决这类问题可以很大地提升你的能力与技巧,我会试着帮助你理解如何使用DP来解题. 这篇文章是基于实例展开来讲的,因为干巴巴的理论实在不好理解. 注意:如果你对于其中某一节已经了解并且不想阅读它,没关系,直接跳过它即可. 简介(入门) 什么是动态规划,我们要如何描述它?

动态规划:从新手到专家(转)

动态规划:从新手到专家 March 26, 2013 作者:Hawstein出处:http://hawstein.com/posts/dp-novice-to-advanced.html声明:本文采用以下协议进行授权: 自由转载-非商用-非衍生-保持署名|Creative Commons BY-NC-ND 3.0 ,转载请注明作者及出处. 前言 本文翻译自TopCoder上的一篇文章: Dynamic Programming: From novice to advanced ,并非严格逐字逐句翻

Dynamic programming:from novice to advanced【动态规划】

动态规划:从新手到专家 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: 自由转载-非商用-非衍生-保持署名|Creative Commons BY-NC-ND 3.0 ,转载请注明作者及出处. 前言 本文翻译自TopCoder上的一篇文章: Dynamic Programming: From novice to advanced ,并非严格逐字逐句翻译,其中加入了自己的一些理解.水平有限,还望指摘.