非常不容易的一题,思路就是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. makefile基础(GNU)

    makefile的核心 targets : prerequisites ; commands...   //不分行的情况 targets : prerequisites                 ...

  2. Linux中exec()执行文件系列函数的使用说明

    函数原型: 描述:    exec()系列函数使用新的进程映像替换当前进程映像.    工作方式没有什么差别, 只是参数传递的方式不同罢了. 说明:    1. 这6个函数可分为两大类: execl( ...

  3. LinkedList - java.util.ConcurrentModificationException

    package com.test.io; import java.io.BufferedReader; import java.io.FileNotFoundException; import jav ...

  4. 打造理想的Windows 10 APP开发环境的5个步骤

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:微软即将发布Windows 10手机版,实际上很多人现在已经开始在开发Windows ...

  5. MyBatis传入参数与parameterType

    参考:http://openwares.net/database/mybatis_parametertype.html Mybatis的Mapper文件中的select.insert.update.d ...

  6. HTML Entity Sets - All

    http://www.htmlentities.com/html/entities/ The view below displays the characters used in the offici ...

  7. Codeforces Testing Round #12 C. Subsequences 树状数组

    C. Subsequences     For the given sequence with n different elements find the number of increasing s ...

  8. hdu 3183(贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 思路:比较前后两个相邻的字符,如果前面一个字符大于后面一个字符,就把它去掉. #include ...

  9. Memcache升级版:CouchBase的安装配置与使用说明

    Memcache基本上已经是开发的标配了,但是对于Memcache集群,很多线上部署仍然是很单薄的. 几个存在的问题:不健壮.数据不安全.配置变更可能导致存取异常.后备数据的一致性 鉴于存在以上问题, ...

  10. Redis持久化实践及灾难恢复模拟

    参考资料: Redis Persistence http://redis.io/topics/persistence Google Groups https://groups.google.com/f ...