Petr, a student, decided to start his own business. He offers SMS advertising services to the business owners renting offices in the newly built “Prisma” tower. If an office owner wants to use the service, he devises a slogan and Petr texts it from his personal phone to thousands of Ekaterinburg citizens (he already bought the pirated list of mobile phone numbers). The cost of each slogan sent is a sum of costs of each character typed. Cost of an individual character is determined according to a very simple scheme: each tap at the phone’s keyboard costs 1 rouble.
Petr’s phone doesn’t support sophisticated text input technologies, such as T9, and only the english alphabet can be used.
1 a b c |
2 d e f |
3 g h i |
4 j k l |
5 m n o |
6 p q r |
7 s t u |
8 v w x |
9 y z |
|
0 . , ! |
# _ |
The “_” character in the table denotes whitespace. If you want to, for example, type “a”, you need to press the “1” button once. To type “k”, you press “4” twice. To type “!”, press “0” three times.
Petr has to apply this simple algorithm to calculate the cost of every slogan he sends. However, Petr is a very busy man (and, as a matter of fact, doesn’t bother to learn arithmetics, because he’s a Philosophy student). You just have to help Petr, you are his best friend after all.
Input: The single line of input contains the slogan. Slogan consists of words, spaces, commas, full stops and exclamation marks. All the words consist of lowercase english letters. Slogan can’t be longer than 1000 characters.
Output: Output a single number representing the cost of the given slogan, according to Petr’s pricing.
Sample:
Input |
Output |
pokupaite gvozdi tolko v kompanii gvozdederov i tovarischi! |
114 |
The C++ code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
getline(cin, str);
int strSize = str.size();
int ans = 0;
for(int i = 0; i < strSize; i++)
{
if(str[i] == 97 || str[i] == 100 || str[i] == 103 || str[i] == 106 || str[i] == 109 || str[i] == 112 ||
str[i] == 115 || str[i] == 118 || str[i] == 121 || str[i] == 32 || str[i] == 46)
{
ans+=1;
}
else if(str[i] == 98 || str[i] == 101 || str[i] == 104 || str[i] == 107 || str[i] == 110 ||
str[i] == 113 || str[i] == 116 || str[i] == 119 || str[i] == 122 || str[i] == 44)
{
ans+=2;
}
else
{
ans+=3;
}
}
cout << ans;
return 0;
}
Notice that this time, instead of using Console In (cin >> x)
, I’m using getline(cin, x)
, the reason is that >>
operator is the defined as C’s scanf %s
: «Any number of non-whitespace characters, stopping at the first whitespace character found. A terminating null character is automatically added at the end of the stored sequence.» Since that would be a problem because of the spaces, I had to use getline from the <string> library: «Extracts characters from is and stores them into str until the delimitation character delim is found (or the newline character, ‘\n’, for (2)).». It takes two parameters, the first one is the object from which characters are extracted (istream
) and the second one the object where the extracted line is stored (string
): getline(istream, string)
.
I also used std::string::size
, which simply returns the length of a string in terms of number of characters.
And here’s the much more simpler C# code:
using System;
namespace _1567
{
class Program
{
static void Main(string[] args)
{
string str = Console.ReadLine();
char[] arr;
int ans = 0;
arr = str.ToCharArray(0, str.Length);
foreach(char c in arr)
{
if (c == 97 || c == 100 || c == 103 || c==106 || c==109 || c==112 || c==115 || c==118 || c==121
|| c==32 || c==46) { ans += 1; }
else if(c==98 || c==101 || c==104 || c==107 || c==110 || c==113 || c==116 || c==119 || c==122 || c==44)
{ ans += 2; }
else { ans += 3; }
}
Console.WriteLine(ans);
Console.ReadLine();
}
}
}
I used a method from String which is String.ToCharArray
, it takes two parameters, the starting position of the string and the length we are taking string.ToCharArray(start, length)
. For example:
string str = "Hello world!";
char[] arr;
arr = str.ToCharArray(6,5);
The foreach
statement is very easy to comprehend, however, here’s what MSDN says:
The foreach statement repeats a group of embedded statements for each element in an array or an object collection that implements the System.Collections.IEnumerable or System.Collections.Generic.IEnumerable interface. The foreach statement is used to iterate through the collection to get the information that you want, but can not be used to add or remove items from the source collection to avoid unpredictable side effects. If you need to add or remove items from the source collection, use a for loop.
The embedded statements continue to execute for each element in the array or collection. After the iteration has been completed for all the elements in the collection, control is transferred to the next statement following the foreach block.
At any point within the foreach block, you can break out of the loop by using the break keyword, or step to the next iteration in the loop by using the continue keyword.