POJ上一道关于信息映射的经典题目,为什么这样的解答也会WA(附原题)

runningdog_1 2006-07-28 10:26:08
Best SMS to Type
Time Limit:1000MS Memory Limit:65536K
Total Submit:1051 Accepted:377

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

信息映射图:
 1    2  3  4  5  6  7  8   9
|‘’|ABC|DEF|GHI|JKL|MNO|PQRS|TUV|WXYZ| 

我的程序:

#include<iostream>
#include<cstring>
using namespace std;
const int ttime[]={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};
const int key[]= {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,10};
int result[100];
int main()
{
for(int i=0;i<100;i++)
result[i]=0;
int tc;
cin>>tc;
int k=tc;
while(k)
{
char mes[1001];
char ch,rch='Z'+1;
int t,w;
cin>>t>>w;
cin.seekg(1);
cin.getline(mes,1001);
for(int i=0;i<strlen(mes);i++)
{
ch=mes[i];
if(ch==' ')
result[tc-k]+=t;
else
{
if(key[ch-'A']==key[rch-'A'])
result[tc-k] += w+ttime[ch-'A']*t;
else
result[tc-k] += ttime[ch-'A']*t;
}
rch=ch;
}
k--;
}
for(i=0;i<tc;i++)
cout<<result[i]<<endl;
return 0;
}
...全文
753 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
OOPhaisky 2006-09-07
  • 打赏
  • 举报
回复
看晕了
mathe 2006-09-01
  • 打赏
  • 举报
回复
呵呵,不需要动态规划,太简单的题目了.
wxspll() 的代码没有错,不过可读性差些.
可以定义两个数组,让代码看起来舒服一些:
int map[26]={2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,9,9};
int cost[26]={1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,4};
len=strlen(s);
prev=-1;
total_cost=0;
for(i=0;i<len;i++){
if(s[i]==' '){
total_cost+=p;
prev=-1;
}else{
if(prev==map[s[i]])total_cost+=w;
total_cost+=cost[s[i]]*p;
prev=map[s[i]];
}
}
「已注销」 2006-08-24
  • 打赏
  • 举报
回复
是动态规划题,我们学校去年比赛时用了这道题目的。
Integrate01 2006-08-09
  • 打赏
  • 举报
回复
留名
runningdog_1 2006-08-09
  • 打赏
  • 举报
回复
楼上说的很对
这么简单的题目不AC还干个窎阿

但我找不出程序的错误
它也不AC

楼上方法也是一种解决之道
可我总觉得此题用查表法爽得多.
wxspll 2006-07-31
  • 打赏
  • 举报
回复
"记得这道题是用动态规划来解的。很想帮你,不过代码找不到了。不好意思。。。"
===============================================================================
Bullshit, 不是DP.
这是道简单模拟,比赛时,10分钟内AC不了就别干了

#include <iostream>
using namespace std;

int main()
{
int t, p, w;
char s[1001];

cin >> t;
while (t--)
{
cin >> p >> w;
getchar();
gets(s);

int time = 0, pre = 0;
for (int i = 0; i < strlen(s); ++i)
{
if (s[i] >= 'A' && s[i] <= 'C')
{
if (pre == 2)
time += w;
time += (s[i] - 'A' + 1) * p;
pre = 2;
}
else if (s[i] >= 'D' && s[i] <= 'F')
{
if (pre == 3)
time += w;
time += (s[i] - 'D' + 1) * p;
pre = 3;
}
else if (s[i] >= 'G' && s[i] <= 'I')
{
if (pre == 4)
time += w;
time += (s[i] - 'G' + 1) * p;
pre = 4;
}
else if (s[i] >= 'J' && s[i] <= 'L')
{
if (pre == 5)
time += w;
time += (s[i] - 'J' + 1) * p;
pre = 5;
}
else if (s[i] >= 'M' && s[i] <= 'O')
{
if (pre == 6)
time += w;
time += (s[i] - 'M' + 1) * p;
pre = 6;
}
else if (s[i] >= 'P' && s[i] <= 'S')
{
if (pre == 7)
time += w;
time += (s[i] - 'P' + 1) * p;
pre = 7;
}
else if (s[i] >= 'T' && s[i] <= 'V')
{
if (pre == 8)
time += w;
time += (s[i] - 'T' + 1) * p;
pre = 8;
}
else if (s[i] >= 'W' && s[i] <= 'Z')
{
if (pre == 9)
time += w;
time += (s[i] - 'W' + 1) * p;
pre = 9;
}
else if (s[i] == ' ')
{
time += p;
pre = 0;
}
}
cout << time << endl;

}
return 0;
}
「已注销」 2006-07-29
  • 打赏
  • 举报
回复
记得这道题是用动态规划来解的。很想帮你,不过代码找不到了。不好意思。。。
Dong 2006-07-29
  • 打赏
  • 举报
回复
楼主,你自己是高手了,这样的问题很难帮到你
runningdog_1 2006-07-28
  • 打赏
  • 举报
回复
信息映射图:
 1  2  3  4  5  6  7  8   9
|‘ ’|ABC|DEF|GHI|JKL|MNO|PQRS|TUV|WXYZ|
runningdog_1 2006-07-28
  • 打赏
  • 举报
回复
信息映射图:
 1  2  3  4  5  6  7  8   9
|‘’|ABC|DEF|GHI|JKL|MNO|PQRS|TUV|WXYZ|

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧