mobile phone keyboard

mobile phone keyboard

Time Limit: 1000 ms  Memory Limit: 65536 KiB

Problem Description

Everyone should have seen the nine-key mobile phone keyboard. The distribution of the letters on the keyboard is shown in the figure below.

When we use this keyboard to input letters, for some letters, we often need to press multiple keys to input.

For example: a, b, c  are all on the " 2" key, you  only need to press once to enter , and you need to press three consecutive times to enter c  . 

The rules for entering multiple letters in a row are as follows:

1. If the two lettersbefore and after are not on the same key, you can directly input the next letter after inputting the previous letter , such as: ad  needs to press the keyboard twice , kz  needs to be pressed   times.

2. If the two letters before and after are on the same key, it takes a while to enter the nextletter after entering the previous letter , such as  ac , after entering  , it takes a while to enter  c .

Now suppose that each time the keyboard is pressed it takes one time period, and the waiting time takes two time periods.

Now given a string containing only lowercase English letters, calculate the time it takes to type it.

Input

 

The input contains multiple sets of test data, for each set of test data:

Input is a one-line string containing only lowercase letters, and the length of the string does not exceed 100 .

Output

 

For each set of test data, the time it takes to output.

Sample Input

bob
www

Sample Output

7
7

Hint

 

Source

"Master's Cup" Shandong University of Technology 5th ACM Programming Competition

One thing to note about this topic is that if two adjacent keys are the same key, there will be a waiting time of 2, don't forget to add this time;
//package hello;

import java.util. *;

public class Main {
	static int position(char a) {
		if (a >= 'a' && a <= 'c')
			return 2;
		else if (a >= 'd' && a <= 'f')
			return 3;
		else if (a >= 'g' && a <= 'i')
			return 4;
		else if (a >= 'j' && a <= 'l')
			return 5;
		else if (a >= 'm' && a <= 'o')
			return 6;
		else if (a >= 'p' && a <= 's')
			return 7;
		else if (a >= 't' && a <= 'v')
			return 8;
		else
			return 9;
	}

	static int time_input(char a) {
		int flag = 0;
		if(a=='a'||a=='d'||a=='g'||a=='j'||a=='m'||a=='p'||a=='t'||a=='w')
			flag = 1;
		else if(a=='b'||a=='e'||a=='h'||a=='k'||a=='n'||a=='q'||a=='u'||a=='x')
			flag = 2;
		else if(a=='c'||a=='f'||a=='i'||a=='l'||a=='o'||a=='r'||a=='v'||a=='y')
			flag = 3;
		else if(a=='s'||a=='z')
			flag = 4;
		return flag;
	}

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		char str[];
		int sum = 0;
		while(cin.hasNext()){
			sum = 0;
			str = cin.next().toCharArray();
			sum += time_input(str[0]);
			for(int i = 1; i < str.length; i++) {
				if(position(str[i]) == position(str[i-1])){
					sum += 2;
					sum += time_input(str[i]);
				}
				else {
					sum += time_input(str[i]);
				}
			}
			System.out.println(sum);
		}
	}

}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325735793&siteId=291194637