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的更多相关文章

  1. USACO 5.4 Twofive(DP)

    非常不容易的一题,思路就是DP之后输出路径.但是此题,路径和DP的方式不一样,路径要按字典序输出. 开始写了一个版本,N 10000的时候就是过不了,后来才发现,自己的写法有问题,无法保证字典序.看了 ...

  2. USACO 5.5 章节

    Picture 题目大意 IOI 1998 求n (<=5000)个矩形 覆盖的图形 的周长(包括洞), 坐标范围[-10000,10000] 题解 一眼离散化+2维线段树,但仔细一想 空间不太 ...

  3. 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 ...

  4. 【USACO 3.1】Stamps (完全背包)

    题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...

  5. USACO翻译:USACO 2013 NOV Silver三题

    USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...

  6. USACO翻译:USACO 2013 DEC Silver三题

    USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...

  7. USACO翻译:USACO 2014 DEC Silver三题

    USACO 2014 DEC SILVER 一.题目概览 中文题目名称 回程 马拉松 奶牛慢跑 英文题目名称 piggyback marathon cowjog 可执行文件名 piggyback ma ...

  8. USACO翻译:USACO 2012 FEB Silver三题

    USACO 2012 FEB SILVER 一.题目概览 中文题目名称 矩形草地 奶牛IDs 搬家 英文题目名称 planting cowids relocate 可执行文件名 planting co ...

  9. USACO翻译:USACO 2012 JAN三题(3)

    USACO 2012JAN(题目三) 一.题目概览 中文题目名称 放牧 登山 奶牛排队 英文题目名称 grazing climb lineup 可执行文件名 grazing climb lineup ...

随机推荐

  1. C++函数不能为virtual的场景

    1.类函数不能同时被static和virtual修饰. 2.类的模板函数不能被virtual修饰 未完待续

  2. grep与正则表达式详解和实例

    转载自:http://www.jb51.net/article/31207.htm grep 工具,以前介绍过. grep -[acinv] '搜索内容串' filename -a 以文本文件方式搜索 ...

  3. oracle表结构和数据导出时的一些勾选项说明

    使用pl/sql developer导出oracle数据库的表结构和表数据时,有一些勾选项供用户选择,需要用户根据实际情况进行勾选或取消. 导出方法如下:一.只导出表结构1.使用pl/sql deve ...

  4. Vue入坑教程(二)——项目结构详情介绍

    之前已经介绍了关于Vue的脚手架vue-cli的安装,以及一些文件目录介绍.具体可以查看<vue 入坑教程(一)--搭建vue-cli脚手架> 下面简单说一下具体的文件介绍 (一) pac ...

  5. MVC: Connection String

    背景: 之前项目使用的是DB first/Model first,现在要对EF升级的6.0,并且更换成Code first. 问题: 1. System.Data.Entity.Core.Metada ...

  6. Throwable、Error、Exception、RuntimeException 区别

    1.java将所有的错误封装为一个对象,其根本父类为Throwable, Throwable有两个子类:Error和Exception. 2.Error是Throwable 的子类,用于指示合理的应用 ...

  7. C语言入门教程-(1)简介及搭建环境

    1.谁适合阅读本教程 本教程可以帮助大家从零开始学习C语言,对于有一定基础的人起到夯实基本功的作用.C语言容易学习,非常适合初学者入门,而且也为以后的编程打下基础.借用一句话:“要进入编程行业高手必学 ...

  8. Oracle新建数据库,并导入dmp文件

    1:安装Oracle及新建数据库 Oracle 11g安装图解 http://www.cnblogs.com/qianyaoyuan/archive/2013/05/05/3060471.html h ...

  9. Eureka简介

    Eureka是Spring Cloud Netfix 的一个子模块,也是核心模块之一,用于云端服务发现.一个基于RestFul的服务,用于定位服务,以实现云端中间层服务发现和中间层转移. 服务注册与发 ...

  10. 树形dp(C - Choosing Capital for Treeland CodeForces - 219D )

    题目链接:https://cn.vjudge.net/contest/277955#problem/C 题目大意:输入n,代表有n个城市,然后再输入n-1条有向边,然后让你找出一个改变边数的最小值,使 ...