USACO 5.5 Twofive
Twofive
IOI 2001
In order to teach her young calvess the order of the letters in the alphabet, Bessie has come up with a game to play with them. The calves are given a 5 x 5 grid on which they can put the letters 'A'-'Y', but the one rule is that all the letters going across the columns and down the rows must be in the order they appear in the alphabet.
There are a huge number of possible grids, so Bessie decides that they will be named by the string of letters that is given by reading across each row, going down the rows. For example, the grid:
A B C D E
F G H I J
K L M N O
P Q R S T
U V W X Y
would have the name ABCDEFGHIJKLMNOPQRSTUVWXY, which is coincidentally the first possible grid when the entire set of grids is ordered alphabetically. The second grid that meets this requirement is ABCDEFGHIJKLMNOPQRSUTVWXY, which is formed by switching the 'T' and 'U' in the above grid.
Help the calves gain bragging rights. Given a number, M, find which string is Mth in the list of all possible grids when they are sorted alphabetically, and, given a string of letters, find out what number the corresponding grid is in the list of all possible grids.
PROGRAM NAME: twofive
INPUT FORMAT
The first input line contains one of two letters, either an 'N' or a 'W'.
If the first input line contains an 'N', the second line will contain an integer, M, that corresponds to the number of a valid grid. If the first line contains a 'W', the second line will contain a string of 25 letters, which represents a valid grid.
OUTPUT FORMAT
If the input contained the number of a valid grid (first line 'N'), the output should contain a string of 25 letters on a line, which corresponds to the Mth grid in the sorted list of all possible grids.
If the input contained a string of letters indicating a grid (first line 'W'), the output should contain a single integer on a line, which corresponds to the number of the given grid in the list of all possible grids.
SAMPLE INPUT #1 (file twofive.in)
N
2
SAMPLE OUTPUT #1 (file twofive.out)
ABCDEFGHIJKLMNOPQRSUTVWXY
SAMPLE INPUT #2 (file twofive.in)
W
ABCDEFGHIJKLMNOPQRSUTVWXY
SAMPLE OUTPUT #2 (file twofive.out)
2
——————————————————————————————————题解
越刷神题越多……
这道题的思路和介个差不多虽然这道没有做出来
一开始的思路就是赤裸裸的暴力然后map,结果跑不动qwq
然后我要开始写一份详细的解题报告了
如果是编码转单词:
初始计数器s=0,编码为d
计算AB开头所有的单词x,如果s+x>=d,那么我们可以发现AB开头的所有单词囊括了所有1-d的单词,所以第二位就是B
如果s+x<d,那么1-d中包含了AB开头所有单词,那么我们继续枚举下一位,直到满足s+x>=d
由此可以计算出这个单词
如果是单词转编码:
初始计数器s=0
假如一个串AFKPUBGLQVCHMRWDINSXEJOTY
这个单词之前一定有AB,AC,AD,AE开头的所有单词,用s累加上他们的和
然后处理AFB开头的所有单词(处理出来会是0)AFC……AFJ开头的所有单词和,以下位同理
最后s+1就是我们想要的解
那么我们如何计算一个固定前缀开头所有单词的和,用记忆化搜索f[a][b][c][d][e]表示第一行有a个字符,第二行有b个字符,第三行有c个字符,第四行有d个字符,第五行有e个字符,其中这些字符是前a+b+c+d+e个字符,且a->e递减,我们再记一个ans序列是我们固定好的前缀,如果搜索到该位置要往里面填一个字母,这个位置没有字母或者这个位置的字母正好是我们ans里记录的,我们就填,否则跳过
这个记忆化搜索的边界条件是搜满了之后填1
据说用5位六进制数可以让程序变得美观然而我没有
/*
ID: ivorysi
LANG: C++
PROG: twofive
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <string.h>
#include <cmath>
#include <stack>
#include <map>
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
#define inf 0x5f5f5f5f
#define ivorysi
#define mo 97797977
#define hash 974711
#define base 47
#define pss pair<string,string>
#define MAXN 5000
#define fi first
#define se second
#define pii pair<int,int>
#define esp 1e-8
typedef long long ll;
using namespace std;
ll f[][][][][],d,s;
int used[];
char ans[],str[];
void init() {
siji(i,,) {
siji(j,,) {
siji(k,,) {
siji(z,,) {
siji(o,,) {
f[i][j][k][z][o]=-;
}
}
}
}
}
}
int dfs(int a1,int a2,int a3,int a4,int a5,char t) {
if(f[a1][a2][a3][a4][a5]!=-)return f[a1][a2][a3][a4][a5];
if(t>'Y') return f[a1][a2][a3][a4][a5]=;
f[a1][a2][a3][a4][a5]=;
if(a1+<= && (ans[a1+]==t || ans[a1+]==))
f[a1][a2][a3][a4][a5]+=dfs(a1+,a2,a3,a4,a5,t+);
if(a2+<=a1 && (ans[a2+]==t || ans[a2+]==))
f[a1][a2][a3][a4][a5]+=dfs(a1,a2+,a3,a4,a5,t+);
if(a3+<=a2 && (ans[a3+]==t || ans[a3+]==))
f[a1][a2][a3][a4][a5]+=dfs(a1,a2,a3+,a4,a5,t+);
if(a4+<=a3 && (ans[a4+]==t || ans[a4+]==))
f[a1][a2][a3][a4][a5]+=dfs(a1,a2,a3,a4+,a5,t+);
if(a5+<=a4 && (ans[a5+]==t || ans[a5+]==))
f[a1][a2][a3][a4][a5]+=dfs(a1,a2,a3,a4,a5+,t+);
return f[a1][a2][a3][a4][a5];
}
void solve() {
scanf("%s",str+);
if(str[]=='N') {
scanf("%d",&d);
ans[]='A';
siji(i,,) {
siji(j,,) {
if(!used[j]) {
ans[i]='A'+j;
used[j]=;
init();
int x=dfs(,,,,,'A');
if(s+x>=d) break;
else {used[j]=;s+=x;}
}
}
}
printf("%s\n",ans+);
}
else {
scanf("%s",str+);
ans[]='A';
//@used[1]=1这里多了一句这句话,实际上used是记录'A'+j被用过,如果这样我们就无法填B了
siji(i,,) {
xiaosiji(j,,str[i]-'A'){
if(!used[j]) {
ans[i]='A'+j;
init();
s+=dfs(,,,,,'A');
}
}
ans[i]=str[i];
used[str[i]-'A']=;
}
printf("%d\n",s+);
}
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("twofive.in","r",stdin);
freopen("twofive.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
solve();
return ;
}
USACO 5.5 Twofive的更多相关文章
- USACO 5.4 Twofive(DP)
非常不容易的一题,思路就是DP之后输出路径.但是此题,路径和DP的方式不一样,路径要按字典序输出. 开始写了一个版本,N 10000的时候就是过不了,后来才发现,自己的写法有问题,无法保证字典序.看了 ...
- USACO 5.5 章节
Picture 题目大意 IOI 1998 求n (<=5000)个矩形 覆盖的图形 的周长(包括洞), 坐标范围[-10000,10000] 题解 一眼离散化+2维线段树,但仔细一想 空间不太 ...
- USACO . Your Ride Is Here
Your Ride Is Here It is a well-known fact that behind every good comet is a UFO. These UFOs often co ...
- 【USACO 3.1】Stamps (完全背包)
题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...
- USACO翻译:USACO 2013 NOV Silver三题
USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...
- USACO翻译:USACO 2013 DEC Silver三题
USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...
- USACO翻译:USACO 2014 DEC Silver三题
USACO 2014 DEC SILVER 一.题目概览 中文题目名称 回程 马拉松 奶牛慢跑 英文题目名称 piggyback marathon cowjog 可执行文件名 piggyback ma ...
- USACO翻译:USACO 2012 FEB Silver三题
USACO 2012 FEB SILVER 一.题目概览 中文题目名称 矩形草地 奶牛IDs 搬家 英文题目名称 planting cowids relocate 可执行文件名 planting co ...
- USACO翻译:USACO 2012 JAN三题(3)
USACO 2012JAN(题目三) 一.题目概览 中文题目名称 放牧 登山 奶牛排队 英文题目名称 grazing climb lineup 可执行文件名 grazing climb lineup ...
随机推荐
- vue 开发过程中遇到的问题
1. gitlab团队协作开发 2. element ui 问题集锦 3. 使用vue和ElementUI快速开发后台管理系统
- hdu 3065病毒侵袭持续中
病毒侵袭持续中 http://acm.hdu.edu.cn/showproblem.php?pid=3065 Time Limit: 2000/1000 MS (Java/Others) Mem ...
- TOMCAT添加管理用户认证
添加配置文件 --原配置文件: # tail -5 /usr/local/tomcat/conf/tomcat-users.xml <user username="tomcat&quo ...
- Redis学习二:Redis入门介绍
一.入门概述 1.是什么 Redis:REmote DIctionary Server(远程字典服务器) 是完全开源免费的,用C语言编写的,遵守BSD协议,是一个高性能的(key/value)分布式内 ...
- MongoDB - MongoDB CRUD Operations, Bulk Write Operations
Overview MongoDB provides clients the ability to perform write operations in bulk. Bulk write operat ...
- js检测上传文件大小
前言: 项目中经常用到需要上传文件.照片等功能,同时需要限制所上传文件的大小.很多插件都会采用后台请求验证,前端Js校验比较少.本篇介绍一个前端JS便捷判断上传文件大小的方法. 代码很简单,关键就是怎 ...
- 织梦自定义表单通过ajax提交的实现方法
自定义表单通过ajax判断,提交不用跳转页面,提高用户体验.具体方法如下: html表单代码部分,就提交按钮改成botton,,添加onclick事件 表单代码: <form action=&q ...
- Linux常用的20个命令
以下为20个命令 1.ls命令:ls命令式列出目录内容(List Directory Contents)的意思.运行它就是列出文件夹里面的内容,可能是文件也可能是文件夹. root@tecmint:~ ...
- UCenter在JAVA项目中实现的单点登录应用实例
Comsenz(康盛)的UCenter当前在国内的单点登录领域占据绝对份额,其完整的产品线令UCenter成为了账号集成方面事实上的标准. 基于UCenter,可以将Comsenz旗下的Discuz! ...
- python程序练习题集
1.#输入a,b,c,d4个整数,计算a+b-c*d的结果 a=input("please input a nimber:") b=input("please input ...