非常不容易的一题,思路就是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. 设计模式学习之策略模式(Strategy,行为型模式)(13)

    转载地址:http://www.cnblogs.com/zhili/p/StragetyPattern.html 一.引言 本文要介绍的策略模式也就是对策略进行抽象,策略的意思就是方法,所以也就是对方 ...

  2. Android 下拉刷新

    以前旧版用的是开源的PullToRefresh第三方库,该库现在已经不再维护了: chrisbanes/Android-PullToRefreshhttps://github.com/chrisban ...

  3. 【JAVA网络流之TCP与UDP 】

    一.ServerSocket java.lang.Object |-java.net.ServerSocket 有子类SSLServerSocket. 此类实现服务器套接字.服务器套接字等待请求通过网 ...

  4. windows7下安装php的imagick和imagemagick扩展教程

    这篇文章主要介绍了windows7下安装php的imagick和imagemagick扩展教程,同样也适应XP操作系统,Win8下就没测试过了,需要的朋友可以参考下 最近的PHP项目中,需要用到切图和 ...

  5. Uncaught ReferenceError: console is not defined

    今天写javascript代码遇到了这个极其神奇的问题,居然报错说内置的console不存在,而且后来我换成了alert也不行.照例说这些都是js代码内置的东西不应该出现这种错误.不过百度之发现貌似没 ...

  6. AIX下禁止crs随ha启动而启动

    /etc/init.crs enable /etc/init.crs disable 查看目前crs是enable还是disable状态 状态记录在一个文本文件里  /etc/oracle/scls_ ...

  7. URAL 1966 Cycling Roads 点在线段上、线段是否相交、并查集

    F - Cycling Roads     Description When Vova was in Shenzhen, he rented a bike and spent most of the ...

  8. python学习第二天

    dict字典 把数据放入dict:直接赋值.初始化时指定 pop删除key set集合 add添加元素 remove删除元素 字符串str是不可变对象,对字符串的操作都会返回新的字符串 pass 什么 ...

  9. Arduino101学习笔记(十三)—— 101六轴传感器

    一.相关API 1.begin:需要在其他CUIREIMU前调用 //***************************************************************** ...

  10. Popupwindow 的简单实用,(显示在控件下方)

    第一步: private PopupWindow mPopupWindow; 第二步:写一个popupwindow的布局文件XML <?xml version="1.0" e ...