Position:


List

Description

对于包含字母A到Y各一次的单词S,将其从上到下从左到右写在一个5*5的矩阵中,如单词ADJPTBEKQUCGLRVFINSWHMOXY写出来如下:
A D J P T
B E K Q U
C G L R V
F I N S W
H M O X Y
若该矩阵满足每一行每一列的字母都是字典序递增的则称S为优美的,如上述单词就是优美的,而ADJPTBEGQUCKLRVFINSWHMOXY则不是(第二列不满足要求)。
Your Task
将所有优美的单词按字典序列出,从小到大编号1,2,……
请你完成以下两种任务:
1. 给定一个优美的单词,求其编号。
2. 给定一个编号,求对应的优美的单词。

Input

第一行一个字母,W表示任务1,N表示任务2
若是任务1,第二行是一个优美的单词,否则第二行是一个正整数,表示某个优美的单词的编号,保证该数不超过优美的单词的总数

Output

一行,若是任务1,输出对应编号,否则输出对应的优美的单词

Sample Input

  1. W
    ABCDEFGHIJKLMNOPQRSUTVWXY
  2. N
    20

Sample Output

  1. 2
  2. ABCDEFGHIJKLMNOPQSUWRTVXY

HINT

Solution

普通搜索显然TLE(枚举每一位填什么字母O(26!))
注意条件每一行每一列的字母都是字典序递增→每个字母必须小于右下方所有的字母
考虑从A到Z依次填入矩阵0~24中,记忆化搜索f[a][b][c][d][e]记录当前状态后继方案数,满足a>b>c>d>e,并且当前已经填入的数要是原样,src(a,b,c,d,e,tot)tot记录要填入哪个字母。
对于第一个任务,求编号,那么每个位置可以填1~s[i]-‘A’+1,统计之前的方案数,最终方案数加上自己即可+1。
对于第二个任务,求方案,那么每个位置从’A’开始填,知道>n,最终求出的即为结果。
码题解的时候发现如果用过的字母就不要用了,加上优化发现快了不少。

Code

 // <twofive.cpp> - Mon Sep 19 08:11:51 2016
// This file is made by YJinpeng,created by XuYike's black technology automatically.
// Copyright (C) 2016 ChangJun High School, Inc.
// I don't know what this program is. #include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#define MOD 1000000007
#define INF 1e9
using namespace std;
typedef long long LL;
const int MAXN=;
const int MAXM=;
inline int max(int &x,int &y) {return x>y?x:y;}
inline int min(int &x,int &y) {return x<y?x:y;}
inline int gi() {
register int w=,q=;register char ch=getchar();
while((ch<''||ch>'')&&ch!='-')ch=getchar();
if(ch=='-')q=,ch=getchar();
while(ch>=''&&ch<='')w=w*+ch-'',ch=getchar();
return q?-w:w;
}
char s[MAXM];int ans[MAXM];
int f[MAXN][MAXN][MAXN][MAXN][MAXN];
inline bool check(int x,int c){return ans[x]?ans[x]==c:;}
inline int src(int a,int b,int c,int d,int e,int tot){
if(tot==)return ;
int &tmp=f[a][b][c][d][e];
if(tmp)return tmp;
if(a<&&check(a,tot+))tmp+=src(a+,b,c,d,e,tot+);
if(b<a&&check(b+,tot+))tmp+=src(a,b+,c,d,e,tot+);
if(c<b&&check(c+,tot+))tmp+=src(a,b,c+,d,e,tot+);
if(d<c&&check(d+,tot+))tmp+=src(a,b,c,d+,e,tot+);
if(e<d&&check(e+,tot+))tmp+=src(a,b,c,d,e+,tot+);
return tmp;
}
int main()
{
freopen("twofive.in","r",stdin);
freopen("twofive.out","w",stdout);
char type=getchar();int n=;
if(type=='W'){
scanf("%s",s);
for(int i=;i<;i++)
for(ans[i]=;ans[i]<s[i]-'A'+;ans[i]++){
memset(f,,sizeof(f));
n+=src(,,,,,);
}
printf("%d",n+);
}else{
n=gi();
for(int i=;i<;i++)
for(ans[i]=;ans[i]<=;ans[i]++){
memset(f,,sizeof(f));
int now=src(,,,,,);
if(now>=n)break;n-=now;
}
for(int i=;i<;i++)putchar(int(ans[i]+'A'-));
}
return ;
}

小优化-s

 // <twofive.cpp> - Mon Sep 19 08:11:51 2016
// This file is made by YJinpeng,created by XuYike's black technology automatically.
// Copyright (C) 2016 ChangJun High School, Inc.
// I don't know what this program is. #include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#define MOD 1000000007
#define INF 1e9
using namespace std;
typedef long long LL;
const int MAXN=;
const int MAXM=;
inline int max(int &x,int &y) {return x>y?x:y;}
inline int min(int &x,int &y) {return x<y?x:y;}
inline int gi() {
register int w=,q=;register char ch=getchar();
while((ch<''||ch>'')&&ch!='-')ch=getchar();
if(ch=='-')q=,ch=getchar();
while(ch>=''&&ch<='')w=w*+ch-'',ch=getchar();
return q?-w:w;
}
char s[MAXM];int ans[MAXM],used[MAXM];
int f[MAXN][MAXN][MAXN][MAXN][MAXN];
inline bool check(int x,int c){return ans[x]?ans[x]==c:;}
inline int src(int a,int b,int c,int d,int e,int tot){
if(tot==)return ;
int &tmp=f[a][b][c][d][e];
if(tmp)return tmp;
if(a<&&check(a,tot+))tmp+=src(a+,b,c,d,e,tot+);
if(b<a&&check(b+,tot+))tmp+=src(a,b+,c,d,e,tot+);
if(c<b&&check(c+,tot+))tmp+=src(a,b,c+,d,e,tot+);
if(d<c&&check(d+,tot+))tmp+=src(a,b,c,d+,e,tot+);
if(e<d&&check(e+,tot+))tmp+=src(a,b,c,d,e+,tot+);
return tmp;
}
int main()
{
freopen("twofive.in","r",stdin);
freopen("twofive.out","w",stdout);
char type=getchar();int n=;
memset(used,false,sizeof(used));
if(type=='W'){
scanf("%s",s);
for(int i=;i<;i++){
for(ans[i]=;ans[i]<s[i]-'A'+;ans[i]++){
if(used[ans[i]])continue;
memset(f,,sizeof(f));
n+=src(,,,,,);
}
used[ans[i]]=;
}
printf("%d",n+);
}else{
n=gi();
for(int i=;i<;i++){
for(ans[i]=;ans[i]<=;ans[i]++){
if(used[ans[i]])continue;
memset(f,,sizeof(f));
int now=src(,,,,,);
if(now>=n)break;n-=now;
}
used[ans[i]]=;
}
for(int i=;i<;i++)putchar(int(ans[i]+'A'-));
}
return ;
}

这里不得不再提起常数优化,算法优化,小优化→大分数。这点意识真的要有,说不准快了0.01s你就多过了10分,说不准多十分你就保送,进队,金牌,一等奖,降分……

来看下效果:

之后

最新文章

  1. 基于HttpModule的简单.NET网站授权方案
  2. Oracle 11g服务详细介绍及哪些服务是必须开启的?
  3. 矢量化的HTML5拓扑图形组件设计
  4. git 日志格式化
  5. BZOJ 3983 Takeover Wars 解题报告
  6. Linux Epoll介绍和程序实例
  7. 【经典面试题】实现平方根函数sqrt
  8. 函数响应式编程及ReactiveObjC学习笔记 (三)
  9. 【Django】Python web开发:几个模板系统的性能对比(转)
  10. RESTful 的总结
  11. Nuget 多平台多目标快速自动打包
  12. 新手入门HTML5有什么推荐的书籍?
  13. python全栈开发day64-模板-变量和(.)的使用,filters和自定义filter
  14. SQL 必知必会&#183;笔记&lt;6&gt;使用数据处理函数
  15. 移动端自动化测试-Windows-Android-Appium环境搭建
  16. Redis和MySQL的结合方案
  17. Java - 20 Java 继承
  18. C#使用System.IO.Path获取文件路径、文件名
  19. 在R中运行Shell命令脚本(Call shell commands from R)
  20. servlet3.0 新特性和springboot Listener和filter案例

热门文章

  1. win10系统杀毒功能
  2. 「 Luogu P1231 」 教辅的组成
  3. css实现圆角效果
  4. Datatable 导出到execl 官网demo
  5. Linux命令介绍
  6. linux下的进程
  7. Microsoft 根证书计划弃用 SHA-1 哈希算法
  8. 【02】bootstrap起步
  9. 定时任务-----Springboot中使用Scheduled做定时任务----http://www.cnblogs.com/lirenqing/p/6596557.html
  10. Codeforces 263A. Appleman and Easy Task