非常不容易的一题,思路就是DP之后输出路径。但是此题,路径和DP的方式不一样,路径要按字典序输出。

开始写了一个版本,N 10000的时候就是过不了,后来才发现,自己的写法有问题,无法保证字典序。看了看题解,其实也不是很懂。

终于还有3个题,加油了!!

 /*
ID: cuizhe
LANG: C++
TASK: twofive
*/
#include <cstdio>
#include <cstring>
using namespace std;
char str[];
char te[];
int dp[][][][][];
int judge(int x,int step)
{
if(!te[x]||te[x] == step + 'A')
return ;
else
return ;
}
int dfs(int x1,int x2,int x3,int x4,int x5,int step)
{
int ans = ;
if(step == )
return ;
if(dp[x1][x2][x3][x4][x5])
return dp[x1][x2][x3][x4][x5];
if(x1 < &&judge(x1,step))
ans += dfs(x1+,x2,x3,x4,x5,step+);
if(x2 < x1&&judge(x2+,step))
ans += dfs(x1,x2+,x3,x4,x5,step+);
if(x3 < x2&&judge(x3+,step))
ans += dfs(x1,x2,x3+,x4,x5,step+);
if(x4 < x3&&judge(x4+,step))
ans += dfs(x1,x2,x3,x4+,x5,step+);
if(x5 < x4&&judge(x5+,step))
ans += dfs(x1,x2,x3,x4,x5+,step+);
return dp[x1][x2][x3][x4][x5] = ans;
}
int main()
{
char ch[];
freopen("twofive.in","r",stdin);
freopen("twofive.out","w",stdout);
int n,i,temp,ans;
scanf("%s",ch);
if(ch[] == 'N')
{
scanf("%d",&n);
for(i = ;i < ;i ++)
{
for(te[i] = 'A';;te[i] ++)
{
memset(dp,,sizeof(dp));
if((temp = dfs(,,,,,)) < n)
n -= temp;
else
break;
}
}
te[] = '\0';
printf("%s\n",te);
}
else
{
scanf("%s",str);
for(i = ;i < ;i ++)
{
for(te[i] = 'A';te[i] < str[i];te[i] ++)
{
memset(dp,,sizeof(dp));
ans += dfs(,,,,,);
}
}
printf("%d\n",ans+);
}
return ;
}

USACO 5.4 Twofive(DP)的更多相关文章

  1. USACO 3.2 kimbits DP

    自己YY了个DP:设f[n][l]为n位数中包含不超过l个1的总个数 f[n][l]=f[n-1][l]+f[n-1][l-1] 然后用_search()从高位向低位扫描即可,tmp记录当前已记下多少 ...

  2. USACO Cow Pedigrees 【Dp】

    一道经典Dp. 定义dp[i][j] 表示由i个节点,j 层高度的累计方法数 状态转移方程为: 用i个点组成深度最多为j的二叉树的方法树等于组成左子树的方法数 乘于组成右子树的方法数再累计. & ...

  3. USACO 5.5 Twofive

    TwofiveIOI 2001 In order to teach her young calvess the order of the letters in the alphabet, Bessie ...

  4. USACO 2009 Open Grazing2 /// DP+滚动数组oj26223

    题目大意: 输入n,s:n头牛 s个栅栏 输入n头牛的初始位置 改变他们的位置,满足 1.第一头与最后一头的距离尽量大 2.相邻两头牛之间的距离尽量满足 d=(s-1)/(n-1),偏差不超过1 3. ...

  5. [USACO]奶牛抗议(DP+树状数组+离散化)

    Description 约翰家的N头奶牛聚集在一起,排成一列,正在进行一项抗议活动.第i头奶牛的理智度 为Ai,Ai可能是负数.约翰希望奶牛在抗议时保持理性,为此,他打算将所有的奶牛隔离成 若干个小组 ...

  6. poj 2385 Apple Catching(dp)

    Description It and ) in his field, each full of apples. Bessie cannot reach the apples when they are ...

  7. poj 3616 Milking Time(dp)

    Description Bessie ≤ N ≤ ,,) hours (conveniently labeled ..N-) so that she produces as much milk as ...

  8. USACO比赛题泛刷

    随时可能弃坑. 因为不知道最近要刷啥所以就决定刷下usaco. 优先级排在学习新算法和打比赛之后. 仅有一句话题解.难一点的可能有代码. 优先级是Gold>Silver.Platinum刷不动. ...

  9. POJ3280 Cheapest Palindrome 【DP】

    Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6013   Accepted: 29 ...

随机推荐

  1. How many Fibs?【sudt 2321】【大数的加法及其比较】

    How many Fibs? Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Recall the definition of t ...

  2. poj 1701【数学几何】

    The area Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  3. 攻城狮在路上(壹) Hibernate(二)--- 第一个hibernate程序

    1.直接通过JDBC API持久化实体域对象: A.java.sql常用接口和类: DriverManager:驱动程序管理器,负责创建数据库连接. Connection:代表数据库连接. State ...

  4. ThinkPHP3.2判断手机端访问并设置默认访问模块的方法

    ThinkPHP3.2判断是否为手机端访问并跳转到另一个模块的方法 目录结构 公共模块Common,Home模块,Mobile模块 配置Application/Common/Conf/config.p ...

  5. x264 - 高品质 H.264 编码器

    转自:http://www.5i01.cn/topicdetail.php?f=510&t=3735840&r=18&last=48592660 H.264 / MPEG-4 ...

  6. h264 profile & level

    转自:http://blog.csdn.net/sphone89/article/details/17492433 H.264 Profiles H.264有四种profile,每个profile支持 ...

  7. jQuery Mobile Datepicker 使用

    插件引入文件: <meta name="viewport" content="width=device-width, initial-scale=1"&g ...

  8. android:imeOptions属性(转)

    默认情况下软键盘右下角的按钮为“下一个”,点击会到下一个输入框,保持软键盘 设置 android:imeOptions="actionDone" ,软键盘下方变成“完成”,点击后光 ...

  9. orientation和gravity的区别

    orientation:决定UI组件是按行还是列显示 gravity:指定文字对齐方式.

  10. 4.0 和4.5 app 和generic,xaml的问题

    4.0里面不支持Generic.xaml里面 <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source=& ...