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 mapped onto the digits as shown below. To insert the character B for instance, the program would press22.
In order to insert two characters in sequence from the same key, the user must pause before pressing the key a second time. The space character ‘ ‘ should be printed to indicate a
pause. For example, 2 2 indicates AA whereas 22 indicates B.

Input

The first line of input gives the number of cases, NN test cases follow. Each case is a line of text formatted as

desired_message

Each message will consist of only lowercase characters a-z and space characters 
. Pressing zero emits a space.

Output

For each test case, output one line containing "Case #x: " followed by the message translated into the sequence of keypresses.

Limits

1 ≤ N ≤ 100.

Small dataset

1 ≤ length of message in characters ≤ 15.

Large dataset

1 ≤ length of message in characters ≤ 1000.

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
	int n_case;
	ifstream ifile("C-large-practice.in");
	ofstream ofile("result_c2.txt");
	ifile >> n_case;
	int reference[26] = {2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9};
	int replace[26] = {2,22,222,3,33,333,4,44,444,5,55,555,6,66,666,7,77,777,7777,8,88,888,9,99,999,9999};
	for(int i = 0; i <= n_case; i++)
	{
		char line[1002];
		ifile.getline(line, 1002);
		string words(line);
		if(i == 0)
			continue;
		ofile << "Case #" << i << ": ";
		for(int i = 0; i < words.length(); i++)
		{
			if(words[i] == ' ')
			{
				if(i - 1 >= 0 && words[i - 1] == ' ')
					ofile << ' ' << 0;
				else ofile << 0;
			}
			else
			{
				if(i - 1 >= 0 && reference[words[i] - 'a'] == reference[words[i - 1] - 'a'])
					ofile << ' ' << replace[words[i] - 'a'];
				else ofile << replace[words[i] - 'a'];
			}
		}
		ofile << endl;
	}
	return 0;
}
时间: 2024-08-02 18:40:35

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

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在线测试练习题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

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 ,并非严格逐字逐句翻译,其中加入了自己的一些理解.水平有限,还望指摘.