Timus 1567. SMS-Spam

Source: Internet
Author: User

Timus 1567. SMS-spam requires calculation of the charges for SMS messages.

Timus 1567. SMS-Spam

Time Limit: 1.0 second
Memory limit: 64 MB

Petr, a student, decided to start his own business. he offers SMS advertising services to the business owners processing ing 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
ABC
2
Def
3
Ghi
4
Jkl
5
MnO
6
Pqr
7
Stu
8
Vwx
9
YZ
  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

Problem Author:Denis musin
Problem Source:The xiith USU programing championship, October 6, 2007

Question

This topic is about the startup story of Petr, a Russian student. Petr has bought thousands of mobile phone numbers from yecataterinburg residents to provide spam messaging services (unexpectedly, Russia also has a list of illegal mobile phone numbers and spam messages ). Petr sends text messages through his mobile phone. According to his mobile phone keyboard (which is somewhat different from the layout of our common mobile phone keyboard), "a" needs to press the "1" key once, "K" needs to press "4" twice, "!" Press "0" three times. Petr charges fees based on the number of buttons, and one rubles for each button. Our task is to write a program to calculate the amount of money each text message should receive.

Answer

Below is the C # source program:

01:  using System;02:  using System.Collections.Generic;03:  04:  namespace Skyiv.Ben.Timus05:  {06:    // http://acm.timus.ru/problem.aspx?space=1&num=156707:    sealed class T156708:    {09:      static void Main()10:      {11:        var dict = GetPrice();12:        var sum = 0;13:        foreach (var c in Console.ReadLine()) sum += dict[c];14:        Console.WriteLine(sum);15:      }16:      17:      static Dictionary<char, int> GetPrice()18:      {19:        var dict = new Dictionary<char, int>();20:        for (var i = 0; i < 4; i++) dict.Add(".,! "[i], (i % 3) + 1);21:        for (var i = 0; i < 26; i++) dict.Add((char)(i + 'a'), (i % 3) + 1);22:        return dict;23:      }24:    }25:  }

The above program first constructs a toll table with different characters, and then calculates the charge. Note that if the input contains characters not in the billing table, the program will throw keynotfoundexception. But this is an ACM question. We only need to write programs based on the meaning of the question, that is, programming based on the contract, so we do not need to consider unexpected input.

Below is the c source program:

01:  // http://acm.timus.ru/problem.aspx?space=1&num=156702:  03:  #include <stdio.h>04:  05:  int getPrice(char c)06:  {07:    if (c == '.' || c == ' ') return 1;08:    if (c == ',') return 2;09:    if (c == '!') return 3;10:    return 1 + (c - 'a') % 3;11:  }12:  13:  int main()14:  {15:    int i, sum = 0;16:    char buf[1001];17:    gets(buf);18:    for (i = strlen(buf) - 1; i >= 0; i--)19:      sum += getPrice(buf[i]);20:    printf("%d\n", sum);21:    return 0;22:  }

The getprice function in the above program is used to calculate the charge for each character. Note that the gets function in the above program is insecure. If the input exceeds 1000 characters, it will cause a buffer overflow. This is also an ACM question, so don't worry.

The following is the c ++ source code:

01:  // http://acm.timus.ru/problem.aspx?space=1&num=156702:  03:  #include <iostream>04:  #include <string>05:  06:  using namespace std;07:  08:  int getPrice(char c)09:  {10:    if (c == '.' || c == ' ') return 1;11:    if (c == ',') return 2;12:    if (c == '!') return 3;13:    return 1 + (c - 'a') % 3;14:  }15:  16:  int main()17:  {18:    string buf;19:    getline(cin, buf);20:    int sum = 0;21:    for (string::iterator it = buf.begin(); it < buf.end(); it++)22:      sum += getPrice(*it);23:    cout << sum << endl;24:    return 0;25:  }

The above program is basically the same as the C program, but the Getline function is safe.

Below is the F # source program:

01:  // http://acm.timus.ru/problem.aspx?space=1&num=156702:  03:  let getPrice c =04:    match c with05:    | '.' | ' ' -> 106:    | ',' -> 207:    | '!' -> 308:    | x -> 1 + (int x - int 'a') % 309:  10:  let pricing str = str |> Seq.map getPrice |> Seq.sum11:    12:  System.Console.ReadLine() |> pricing |> printfn "%d"

It can be seen that the F # program is the simplest and clearer. seq. Map calculates the charge for each character based on getprice, and sums it with seq. Sum.

Returned directory

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.