呵呵,刚刚水了一道题目。简单题就要水!PKU2895,精简到70行代码!
2006-07-23 21:41
567 查看
最近大家都喜欢水题,我觉得简单题才应该水,不用怎么想特殊的算法。基本上可以说是送分题。这个程序我重写了一遍,可以说是水出来的,三十分钟完成,一遍AC!没有什么特别的算法。
我的程序代码:
///*
//Problem B:Best SMS to Type
//Time Limit:1000MS Memory Limit:65536K
//Total Submit:446 Accepted:188
//
//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 "iostream"
#include "string"
bool pandun(char ch1,char ch2)
{
if((ch1=='A'||ch1=='B'||ch1=='C')&&(ch2=='A'||ch2=='B'||ch2=='C'))
return true;
else if((ch1=='D'||ch1=='E'||ch1=='F')&&(ch2=='D'||ch2=='E'||ch2=='F'))
return true;
else if((ch1=='G'||ch1=='H'||ch1=='I')&&(ch2=='G'||ch2=='H'||ch2=='I'))
return true;
else if((ch1=='J'||ch1=='K'||ch1=='L')&&(ch2=='J'||ch2=='K'||ch2=='L'))
return true;
else if((ch1=='M'||ch1=='N'||ch1=='O')&&(ch2=='M'||ch2=='N'||ch2=='O'))
return true;
else if((ch1=='P'||ch1=='Q'||ch1=='R'||ch1=='S')&&(ch2=='P'||ch2=='Q'||ch2=='R'||ch2=='S'))
return true;
else if((ch1=='T'||ch1=='U'||ch1=='V')&&(ch2=='T'||ch2=='U'||ch2=='V'))
return true;
else if((ch1=='W'||ch1=='X'||ch1=='Y'||ch1=='Z')&&(ch2=='W'||ch2=='X'||ch2=='Y'||ch2=='Z'))
return true;
else
return false;
}
int number(char ch1)
{
if((ch1=='A'||ch1=='B'||ch1=='C'))
return (ch1-'A')%3;
else if((ch1=='D'||ch1=='E'||ch1=='F'))
return (ch1-'D')%3;
else if((ch1=='G'||ch1=='H'||ch1=='I'))
return (ch1-'G')%3;
else if((ch1=='J'||ch1=='K'||ch1=='L'))
return (ch1-'J')%3;
else if((ch1=='M'||ch1=='N'||ch1=='O'))
return (ch1-'M')%3;
else if((ch1=='P'||ch1=='Q'||ch1=='R'||ch1=='S'))
return (ch1-'P')%4;
else if((ch1=='T'||ch1=='U'||ch1=='V'))
return (ch1-'T')%3;
else if((ch1=='W'||ch1=='X'||ch1=='Y'||ch1=='Z'))
return (ch1-'W')%4;
else
return 0;
}
using namespace std;
int main()
{
int n,p,w,len,time=0,i;
char ch[1000];
cin>>n;
while(n)
{
cin>>p>>w;
ch[0]=getchar();
gets(ch);
len=strlen(ch);
for(i=0;i+1<len;i++)
{
time+=(p*(1+number(ch[i])));
if(pandun(ch[i],ch[i+1]))
time+=w;
}
time+=(p*(1+number(ch[i])));
cout<<time<<endl;
time=0;
n--;
}
return 0;
}
我的程序代码:
///*
//Problem B:Best SMS to Type
//Time Limit:1000MS Memory Limit:65536K
//Total Submit:446 Accepted:188
//
//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 "iostream"
#include "string"
bool pandun(char ch1,char ch2)
{
if((ch1=='A'||ch1=='B'||ch1=='C')&&(ch2=='A'||ch2=='B'||ch2=='C'))
return true;
else if((ch1=='D'||ch1=='E'||ch1=='F')&&(ch2=='D'||ch2=='E'||ch2=='F'))
return true;
else if((ch1=='G'||ch1=='H'||ch1=='I')&&(ch2=='G'||ch2=='H'||ch2=='I'))
return true;
else if((ch1=='J'||ch1=='K'||ch1=='L')&&(ch2=='J'||ch2=='K'||ch2=='L'))
return true;
else if((ch1=='M'||ch1=='N'||ch1=='O')&&(ch2=='M'||ch2=='N'||ch2=='O'))
return true;
else if((ch1=='P'||ch1=='Q'||ch1=='R'||ch1=='S')&&(ch2=='P'||ch2=='Q'||ch2=='R'||ch2=='S'))
return true;
else if((ch1=='T'||ch1=='U'||ch1=='V')&&(ch2=='T'||ch2=='U'||ch2=='V'))
return true;
else if((ch1=='W'||ch1=='X'||ch1=='Y'||ch1=='Z')&&(ch2=='W'||ch2=='X'||ch2=='Y'||ch2=='Z'))
return true;
else
return false;
}
int number(char ch1)
{
if((ch1=='A'||ch1=='B'||ch1=='C'))
return (ch1-'A')%3;
else if((ch1=='D'||ch1=='E'||ch1=='F'))
return (ch1-'D')%3;
else if((ch1=='G'||ch1=='H'||ch1=='I'))
return (ch1-'G')%3;
else if((ch1=='J'||ch1=='K'||ch1=='L'))
return (ch1-'J')%3;
else if((ch1=='M'||ch1=='N'||ch1=='O'))
return (ch1-'M')%3;
else if((ch1=='P'||ch1=='Q'||ch1=='R'||ch1=='S'))
return (ch1-'P')%4;
else if((ch1=='T'||ch1=='U'||ch1=='V'))
return (ch1-'T')%3;
else if((ch1=='W'||ch1=='X'||ch1=='Y'||ch1=='Z'))
return (ch1-'W')%4;
else
return 0;
}
using namespace std;
int main()
{
int n,p,w,len,time=0,i;
char ch[1000];
cin>>n;
while(n)
{
cin>>p>>w;
ch[0]=getchar();
gets(ch);
len=strlen(ch);
for(i=0;i+1<len;i++)
{
time+=(p*(1+number(ch[i])));
if(pandun(ch[i],ch[i+1]))
time+=w;
}
time+=(p*(1+number(ch[i])));
cout<<time<<endl;
time=0;
n--;
}
return 0;
}
相关文章推荐
- 一道AC的题目的代码,超短,呵呵……
- zoj 1115题比较简单的一道题目。
- 一道简单的ACM题目讨论
- 一道简单的题目引发的思考
- 一道简单的但是经典的动态规划题目
- 又一道图论题PKU1094“Sorting It All Out”我很容易就把代码写长了
- 一道简单的题目引发的思考
- 这可能是最精简的Android6.0运行时权限处理方式,只有70行代码。附:各种权限的详细处理
- 对“C#写的简单的日历,窗体输出。”的改写,精简了一半多代码
- 入坑pwn第一题,在我们学校最厉害的学长帮助下解决了这一道最简单的题目
- 一道简单的题目
- 这可能是最精简的Android6.0运行时权限处理,70行代码的工具类。附:各种权限详细处理
- C++ 一道简单的题目引发的思考
- 一道简单的寻找中位数的题目
- 百度笔试题2005题目大致是这样的: 第一部分选择题: 有几道网络相关的题目,巨简单,比如第一题是TCP、RIP、IP、FTP中哪个协议是传输层的......。有一道linux的 chown使用题目。其他的全是数据结构的题目!什么链,表
- 一道简单的题目引发的思考
- CSDN高校俱乐部编程挑战群一道仅有7人通过的超5星微软比赛题目-------解题思路&优秀代码分享
- 练习题(4) -- 一道简单而有有趣的题目
- 一道仅有7人通过的超5星微软比赛题目-------解题思路&优秀代码分享,邀你来“找茬儿”
- 今天做了一道的题目,简单是因为程序写起来简单,但是感觉题目不错;