POJ 2895 Best SMS to Type 发送短信的时间计算

http://poj.org/problem?id=2895

 

Best SMS to Type
Time Limit: 1000MS   Memory Limit: 65536K



Description

Using SMS today is more than a pleasing hobby. As the number of messages one sends through this service grows, the need to type them fast is better felt. Sometimes, one wonders how fast a message can be typed. Changing some words to their synonyms, might help type the whole message faster, if we were able to quickly calculate the time needed for a specific message. 

In the following, we assume that each message is a string of capital English letters and space character. The letters 'A' through 'Z' are assigned to keys '2' to '9', as in the following figure. To type a letter, one should press its key 1, 2, 3, or 4 times, depending on the position of the letter from left to right. 

If two consecutive letters of the message are mapped to one key, one should wait for the first letter to be fixed on the screen and then use the key again to type the second one. For instance, to type the letter 'X', one should press '9' twice. If the next letter of the message is not on the same key, one can continue to type the rest of the message. Otherwise, one has to wait for some time, so that the typed 'X' is fixed, and then the next letter ('W', 'X', 'Y', or 'Z') can be typed. To type whitespace, we use the key '1'.As there is no letter mapped to the key '1', the whitespace needs no time to be fixed. 
POJ 2895 Best SMS to Type 发送短信的时间计算_第1张图片 
You are given the time needed to press any key, and the time one should wait for a letter to be fixed. Your program should find the minimum time needed to type a nonempty string, given the above rules.

Input

The input file contains multiple test cases. The first line of the input, contains t, the number of test cases that follow. Each of the following t blocks, describes a test case. 

The first line of each block contains p and w (1 <= p,w <= 1000), which show the amount of time in milliseconds for pressing a letter and waiting for it to be fixed, respectively. The second line contains a non-empty string of length at most 1000, consisting of spaces or capital English letters. There is no leading or trailing spaces in a line. 

Output

For each test case, output one line showing the time needed to type the message in milliseconds. 

Sample Input

1
2 10
ABBAS SALAM

Sample Output

72
/* Author : yan
 * Question : POJ 2895 Best SMS to Type
 * Data && Time : Friday, January 07 2011 11:16 PM
 * Compiler : gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3
*/
#include<stdio.h>
//对应键需要按的次数,最后一个是空格
int dic[]={1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,4,1,2,3,1,2,3,4,1};
//对应字符对应键
int dic1[]={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,1};

char mesg[1005];

int main()
{
	//freopen("input","r",stdin);
	int test;
	int press,wait;
	int i;
	int ans;
	int cnt;//空格个数
	scanf("%d",&test);
	while(test--)
	{
		ans=0;cnt=0;
		scanf("%d %d",&press,&wait);
		getchar();
		gets(mesg);
		for(i=0;mesg[i+1]!='/0';i++)
		{
			if(mesg[i+1]==32) mesg[i+1]='A'+26;
			if( dic1[mesg[i]-'A']==dic1[mesg[i+1]-'A'] && dic1[mesg[i]-'A']!=1 )
			{
				ans+=press*dic[mesg[i]-'A']+wait;
				//printf("%d/n",press*dic[mesg[i]-'A']+wait);
			}
			else
			{
				ans+=press*dic[mesg[i]-'A'];
				//printf("%d/n",press*dic[mesg[i]-'A']);
			}
		}
		//printf("%c",mesg[i]);
		ans+=press*dic[mesg[i]-'A'];
		printf("%d/n",ans);
	}
	return 0;
} 

 

你可能感兴趣的:(POJ 2895 Best SMS to Type 发送短信的时间计算)