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;
}
...全文
750 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|
内容概要:本文提出了一种在单向与双向V2G(Vehicle-to-Grid)并行环境下,对分布式电源与电动汽车充电站进行联合优化配置的方法,并提供了完整的Matlab代码实现。研究综合考虑了分布式光伏、储能系统以及电动汽车充放电行为对配电网的影响,构建了一个多目标优化模型,旨在实现电源、负荷与电网之间的协同规划,提升系统运行的经济性、稳定性和新能源消纳能力。该方法通过优化分布式电源的选址与定容、充电站布局及V2G调度策略,有效降低网络损耗、改善电压质量,并增强配电网对高渗透率电动汽车的承载能力。文中详细阐述了模型构建、求解算法设计、仿真验证流程及结果分析,适用于城市复杂用电场景下的能源基础设施规划与运行优化。; 适合人群:具备电力系统、可再生能源、智能电网等相关专业背景,熟悉Matlab编程与优化算法,从事科研或工程应用的研究生、高校教师及电力行业技术人员;特别适合从事电动汽车与电网互动、分布式能源规划、综合能源系统优化等方向的研究人员。; 使用场景及目标:①用于高校与科研机构开展V2G与分布式能源协同优化的课题研究与论文复现;②支撑电网公司、城市规划部门在充电基础设施布局与电网扩容改造中的科学决策;③为撰写高水平SCI/EI期刊论文提供可复现的技术路线、算法框架与仿真代码支持。; 其他说明:资源包含完整Matlab源代码与仿真案例,建议结合文中模型结构、参数设置与优化目标进行调试学习,可进一步拓展至不确定性建模、多时间尺度优化、鲁棒优化等高级场景,以提升方法的实际应用价值与学术深度。
内容概要:本文提出了一种基于贝叶斯网络的短期电能负荷预测方法,重点解决电力系统中负荷受天气、节假日等多种不确定性因素影响的问题。通过构建贝叶斯网络模型,融合历史负荷数据及相关外部变量,实现对负荷的概率化预测,有效刻画其随机波动特性与复杂依赖关系。该方法不仅能够量化预测不确定性,还能通过条件概率推理分析各因素对负荷的影响程度,提升预测的鲁棒性与实用性。文中配套提供了完整的Python代码实现,涵盖数据预处理、网络结构学习、参数训练及概率推理全过程,便于读者复现与拓展应用。该技术适用于现代智能电网中的调度决策、需求响应管理与能源规划等场景。; 适合人群:具备一定电力系统基础知识和Python编程能力的科研人员、研究生及从事能源预测、智能电网等相关工作的技术人员。; 使用场景及目标:①应用于短期电能负荷预测,特别是在存在显著不确定性和多源影响因素的实际场景中;②为电力系统运行调度、负荷管理、需求响应策略制定以及电网扩容规划提供高可信度的数据支持;③通过动手实践掌握贝叶斯网络在时序预测中的建模流程,深入理解其在处理不确定性问题上的优势与实现细节。; 阅读建议:建议读者结合提供的Python代码逐步实现模型构建过程,重点关注贝叶斯网络的结构设计原则、参数学习算法(如最大似然估计或贝叶斯估计)以及后验概率推理的实现方式,并尝试在不同数据集上进行敏感性分析与模型对比实验,以全面掌握该方法的应用精髓。

3,881

社区成员

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

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