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 ...
随机推荐
- svn服务器搭建(windows)
转载:https://blog.csdn.net/daobantutu/article/details/60467185 本教程会从最基本的下载安装到上传代码,下载代码这条线来详细讲述如何完成SVN服 ...
- 《剑指offer》面试题28:字符串的排列(牛客网版本) java
题目描述 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. 输入描述: 输 ...
- 介绍——基于类的视图(class-based view)
刚开始的时候,django只有基于函数的视图(Function-based views).为了解决开发视图中繁杂的重复代码,基于函数的通用视图( Class-based generic views) ...
- 前端案例分享(一):CSS+JS实现流星雨动画
目录 引言 1.效果图 2.源码 3.案例解析 4.小问题 5.结语 引言 平常会做一些有意思的小案例练手,通常都会发到codepen上,但是codepen不能写分析. 所 ...
- ICDM Winner's Interview: 3rd place, Roberto Diaz
ICDM Winner's Interview: 3rd place, Roberto Diaz This summer, the ICDM 2015 conference sponsored a c ...
- TPL概要
ReaderWriterLockSlim.读写锁,允许多个线程同时获取读锁,但同一时间只允许一个线程获得写锁,因此也称作共享-独占锁.只有等到对象被写入锁占用的时候,才会阻塞 Barrier .它允许 ...
- 第一回 java~简介
Java 简介 Java是由Sun Microsystems公司于1995年5月推出的Java面向对象程序设计语言和Java平台的总称.由James Gosling和同事们共同研发,并在1995年正式 ...
- [转载]C语言程序的内存分配方式
"声明一个数组时,编译器将根据声明所指定的元素数量为数量为数组保留内存空间."其实就是编译器在编译的过程中,会加入几条汇编指令在程序里处理内存分配,并不是说编译时就分配了内存,不要 ...
- 20155325 2016-2017-2 《Java程序设计》第7周学习总结
教材学习内容总结 名称 作用 Calendar 设定时间日期等字段 add() 改变Calendar的时间 roll() 针对日期中某个字段加减 getDefault() 取得默认时区信息 教材学习中 ...
- windows 下 react-native(v0.56) Android 环境搭建踩坑记录
debugservicereact-native 安装官网 https://reactnative.cn/docs/getting-started.html 根据官网步骤一步步执行下去.还能碰到一些问 ...