poj 2895-字符串的处理

做这个题目的收获就是:知道了gets(str)的作用。可以忽略掉字符串里面的空格。。。还有就是,字符数组可以保存字符的大小

Best SMS to Type
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions:3768 Accepted: 1584

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. 
 
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

代码:

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring> 
#include<cmath>
#include<queue>
using namespace std;
#define sf scanf
#define pf printf
#define INF 1<<29
#define eps 1e-6
const double PI=acos(-1.0);
#define lint __int64
#define LL long long 
#define ULLint unsigned long long //2^64-1>1.8*10^19
#define clr(x) memset(x,0,sizeof(x))
#define Clr(x) memset(x,-1,sizeof(x))
int where[1000];
int has[1000];
char str[1000];
int main(){
	// freopen("a.txt","r",stdin);
	where['A']=where['B']=where['C']=1;
	where['D']=where['E']=where['F']=2;
	where['G']=where['H']=where['I']=3;
	where['J']=where['K']=where['L']=4;
	where['M']=where['N']=where['O']=5;
	where['P']=where['Q']=where['R']=where['S']=6;
	where['T']=where['U']=where['V']=7;
	where['W']=where['X']=where['Y']=where['Z']=8;
	has['A']=has['D']=has['G']=has['J']=has['M']=has['P']=has['T']=has['W']=1;
	has['B']=has['E']=has['H']=has['K']=has['N']=has['Q']=has['U']=has['X']=2;
	has['C']=has['F']=has['I']=has['L']=has['O']=has['R']=has['V']=has['Y']=3;
	has['S']=has['Z']=4;
	int cas;
	scanf("%d",&cas);
	while(cas--){
		int nu,wait;
		scanf("%d%d\n",&nu,&wait);
		gets(str);
		int len=strlen(str);
		int i;
		int out=0;
		for(i=0;i<len;i++){
			if(str[i]==' '){
				out+=nu;
				continue;
			}
			out+=has[str[i]]*nu;
			if(where[str[i]]==where[str[i+1]]){
				out+=wait;
			}
		}
		printf("%d\n",out);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值