J:   You Win!
You just  achieved the High  Score on your favorite video game! Now, you get to enter 
your name! You have to use the  controller  to enter  your name, which can be awkward. 
Here’s how it works:
  There  are  only  the  26  capital  letters  A  to  Z,  in  order.  There  are  no  numbers, 
spaces, lower case letters, or any other characters.
  Pushing  UP  or  DOWN  changes  the  active  letter  one  letter  forward  (UP)  or 
backward  (DOWN).  The active  letter  starts at  A.  It will not reset  when you move 
around in the name. It also wraps: UP from Z goes to A, DOWN from A goes to Z.
  Pushing  LEFT  or  RIGHT  moves  the  cursor  one  letter  left  or  right  in  the  current 
name. Note that once the cursor is at either end of the current name, it cannot 
move any further in that direction.
  Pushing the FIRE button adds the active letter to the name. 
For example, consider the name ‘ALMA’. One way you could enter ‘ALMA’ is like this:
Action  # of Pushes  Name (| = Cursor)  Active Letter
FIRE  1  A|  A
UP  11  A|  L
FIRE  1  AL|  L
UP  1  AL|  M
FIRE  1  ALM|  M
DOWN  12  ALM|  A
FIRE  1  ALMA|  A
This would take 28 button pushes. However, consider entering ‘ALMA’ like this:
Action  # of Pushes  Name (| = Cursor)  Active Letter
FIRE  1  A|  A
FIRE  1  AA|  A
LEFT  1  A|A  A
UP  11  A|A  L
FIRE  1  AL|A  L
UP  1  AL|A  M
FIRE  1  ALM|A  M 
2013 ACM ICPC Southeast USA Regional Programming Contest
Page 16 of 16    2 November, 2013
This takes only 17  button pushes. Given a  name, what is the fewest number of  button 
pushes needed to enter that name? Assume that the active  letter  starts at A, and that it 
doesn’t matter where the cursor ends up when you’re done.
Input
There will be  several  test cases in the input. Each test  case will  consist of a single string 
on  its  own  line,  with  from  1  to  18  capital  letters,  representing  a  name  that  must  be 
entered into the High Score list. The input will end with a line with a single 0.
Output
For each test case, output a single integer representing the smallest number of  button 
pushes needed to enter the name. Output no spaces, and do not separate answers with 
blank lines.
Sample Input
ALMA
YES
0
Sample Output

17

21

状态压缩 dp, 这里是用记忆化搜索写的

开始做的时候多枚举了很多状态导致超时

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>using namespace std;#define inf  1<<30int f[1<<19][19];
char s[111];
int n,ans;int dis( char a, char b)
{int t= abs( a- b );t= min( 26-t , t);return t;
}int  dfs(int x, int  pos)
{if (f[x][pos]!=-1 )return  f[x][pos];int t=inf,y,step;y =  ( x ^ (1<<pos) );step=0;for (int j=pos-1; j>=0; j--)if ( y & (1<<j) ){t= min( t,  dfs( y, j) + step + dis( s[j], s[pos])+1  );step++;}step=1;for (int j=pos+1; j<n; j++)if ( y & (1<<j) ){t= min( t,  dfs( y, j) + step + dis( s[j], s[pos])+1  );step++;}f[x][pos]=t;return t;
}int main()
{while (scanf("%s",s)!=EOF){if (s[0]=='0')break;n=strlen(s);memset(f,-1,sizeof(f));for (int i=0; i<n; i++)f[1<<i][i]=dis( s[i],'A' ) +1;ans=inf;for (int i=0; i<n; i++)ans= min(ans, dfs( (1<<n) -1  , i)  );printf("%d\n",ans);}return 0;
}

SERC2013 J You Win!相关推荐

  1. PAT甲级排队问题合集 (持续更新中)

    已加入的习题 A1014,A1017 问题1和2共性 1. 都是排队问题 2. 都有一条黄线 3. 都需要找到最先离开人的队伍 4. 都有着服务时间段限制(迟于某个时间点来不予受理) 问题1:1014 ...

  2. 硬币游戏 (博弈入门)

    题目: A 和 B 在玩一个游戏,给定K个数字a1,a2,a3...ak ; 一开始有x个硬币 : A 和 B 轮流取硬币.每次所取硬币的枚数一定在a1,a2,a3..ak ; 里面,A 先取 , 取 ...

  3. pwnable.kr lotto题解

    ssh lotto@pwnable.kr -p2222 (pw:guest) 题目源码: #include <stdio.h> #include <stdlib.h> #inc ...

  4. phpstrom安装bootstrap3插件

    1.步骤 File > > Settings > >Plugins > > 搜索bootstrap 3 然后点击 Browse repositories 就会有一个 ...

  5. HihoCoder - 1591 锦标赛(最大费用最大流)

    题目链接:点击查看 题目分析:给出 n 个选手参加比赛,任意两个选手之间都会存在一场比赛,共场比赛,现在已经知道 k 场比赛的结果了,现在规定如果 i 和 j 比赛,i 取得了胜利,那么将会得到 wi ...

  6. PAT (Advanced Level) 1017. Queueing at Bank (25)

    简单模拟. #include<iostream> #include<cstring> #include<cmath> #include<algorithm&g ...

  7. Java锤子剪刀布大家应该都会玩“锤子剪刀布”的游戏: 现给出两人的交锋记录,请统计双方的胜、平、负次数,并且给出双方分别出什么手势的胜算最大。

    题目描述: 大家应该都会玩"锤子剪刀布"的游戏:现给出两人的交锋记录,请统计双方的胜.平.负次数,并且给出双方分别出什么手势的胜算最大. 输入描述: 输入第1行给出正整数N(< ...

  8. CCF 201403-2 窗口

    问题描述 在某图形操作系统中,有 N 个窗口,每个窗口都是一个两边与坐标轴分别平行的矩形区域.窗口的边界上的点也属于该窗口.窗口之间有层次的区别,在多于一个窗口重叠的区域里,只会显示位于顶层的窗口里的 ...

  9. POJ-2151 Check the difficulty of problems 概率DP

    题目链接:http://poj.org/problem?id=2151 组合数做肯定超时,容易看出是DP.f[i][j]表示某个队的前j个题目做出i个题目的概率,则f[i][j]=f[i][j-1]* ...

  10. CCF201403-2 窗口

    问题描述: 试题编号: 201403-2 试题名称: 窗口 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 在某图形操作系统中,有 N 个窗口,每个窗口都是一个两边与坐标轴分别 ...

最新文章

  1. 微服务后如何做一次系统梳理
  2. 智能合约类代币EOS、BCH、Tezos很有可能会超越BTC
  3. Android判断应用是否拥有某种权限
  4. 简明 Python 编程规范v2
  5. ReentrantLock可以是公平锁,sync只能是非公平锁。
  6. Comet, 下一代反向AJAX(即服务器推送技术- Server-side push)
  7. .NET分布式缓存Redis从入门到实战
  8. Linux 配置yum本地安装源
  9. SELECT * FROM user WHERE username LIKE #{aaa}与SELECT * FROM user WHERE username LIKE '%${value}%'
  10. LeetCode:892. 三维形体的表面积
  11. 平面最近点对问题(分治)
  12. servlet监听器Listener介绍和使用
  13. 深度学习相关优化器以及在tensorflow的使用(转)
  14. jquery常见操作总结
  15. 100个实用的 Linux Shell 脚本经典案例
  16. 傅里叶光学导论_激光位移传感器的光学系统设计
  17. 什么是DOS操作系统
  18. ALTREA cyclone IV e系列程序固化方法
  19. RTKLIB学习总结(六)导航电文、卫星位置计算
  20. 如何快速定位出一个IP地址的归属地?——二分查找变体

热门文章

  1. 复旦计算机系统基础课件,复旦大学软件工程考研(MSE)计算机系统基础复习资料PPT演示课件...
  2. JAVA编译器的作用
  3. 从grub中引导进入windows7的启动管理界面
  4. ie浏览器打不开闪退_点开IE浏览器的时候总是闪退
  5. java计算机毕业设计销售人员绩效管理系统源码+系统+数据库+lw文档(1)
  6. 影楼修片走下神坛 PS磨皮技巧大揭秘
  7. 什么是Ruby on Rails?
  8. Vray材质速查手册(精讲)
  9. 安装SqlServer 2017 时出现“Polybase要求安装Oracle JRE 7 更新51(64位)或更高版本规则失效”报错的解决办法
  10. 修改jsp文件刷新网页没有更新