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分,说不准多十分你就保送,进队,金牌,一等奖,降分……

来看下效果:

之后

【Codevs1322】单词矩阵的更多相关文章

  1. codevs1322 单词矩阵

    题目描述 Description 对于包含字母A到Y各一次的单词S,将其从上到下从左到右写在一个5*5的矩阵中,如单词ADJPTBEKQUCGLRVFINSWHMOXY写出来如下: A D J P T ...

  2. P1101 单词方阵(DFS)

    题目描述 给一n \times nn×n的字母方阵,内可能蕴含多个"yizhong"单词.单词在方阵中是沿着同一方向连续摆放的.摆放可沿着 88个方向的任一方向,同一单词摆放时不再 ...

  3. NLP之基于词嵌入(WordVec)的嵌入矩阵生成并可视化

    词嵌入 @ 目录 词嵌入 1.理论 1.1 为什么使用词嵌入? 1.2 词嵌入的类比推理 1.3 学习词嵌入 1.4 Word2Vec & Skip-Gram(跳字模型) 1.5 分级& ...

  4. 第四次作业——WORDSEARCH小游戏

    “谁想出来的这么缺德的题目啊!!!!”一个声音在我心中回荡 这个题目很早就在课堂上公布了,我和我的小伙伴都惊呆了! 这是个毛?根本无从下手的感觉 总是觉得这个小游戏不是程序能给出答案的,因为我的第一印 ...

  5. 【NLP CS224N笔记】Lecture 2 - Word Vector Representations: word2vec

    I. Word meaning Meaning的定义有很多种,其中有: the idea that is represented by a word,phrase,etc. the idea that ...

  6. 词向量之Word2vector原理浅析

    原文地址:https://www.jianshu.com/p/b2da4d94a122 一.概述 本文主要是从deep learning for nlp课程的讲义中学习.总结google word2v ...

  7. Semantic Compositionality through Recursive Matrix-Vector Spaces-paper

    Semantic Compositionality through Recursive Matrix-Vector Spaces 作者信息:Richard Socher Brody Huval Chr ...

  8. 机器学习与R语言:NB

    #---------------------------------------- # 功能描述:演示NB建模过程 # 数据集:SMS文本信息 # tm包:维也纳财经大学提供 #----------- ...

  9. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

随机推荐

  1. JMeter在linux上分布式压测步骤(二)

    哈喽,我又来了~ 前提:三台linux虚拟机,一台作为master,另外两台作为slave. 一.server端 1.修改1099端口,client和server通信的端口,可以不修改,默认就是109 ...

  2. org-table

    ‎ Table of Contents 1. table 1.1. 创建方式 1.2. 重新对齐 1.3. 行列编辑 1.4. 区域 1.5. 计算 1.6. 其他的 1.7. 行宽度 1.8. 列分 ...

  3. spring boot+mybatis+mysql增删改查分页

    server: port: servlet: context-path: /springBootMybatis spring: datasource: name: test url: jdbc:mys ...

  4. 瑞芯微ROCK960 RK3399烧录image后扩容rootfs

    问题描述: RK3399开发板烧录官网提供的ubuntu镜像: Ubuntu 16.04 Server arm64(下载地址:https://www.96boards.org/documentatio ...

  5. python3.x Day3 集合

    python中的集合 集合定义:一个无序的去重的数据集,主要特性就是去重和关系测试,关系测试不改变集合中的数据值 定义集合:set(list) 可以将list转化为集合set 示例: 定义一个集合:l ...

  6. buf.writeInt16BE()函数详解

    buf.writeInt16BE(value, offset[, noAssert]) buf.writeInt16LE(value, offset[, noAssert]) value {Numbe ...

  7. 根据屏幕的大小改变rem的参考值

    移动端一半会选用rem+flex布局的方式,下面是根据屏幕的宽度,动态的改变rem的参考值 var screenWidth;             var html = document.getEl ...

  8. 2014年武汉的IT行情好像不太好(续):20个月过后,再看当时面试过的几个公司--武汉财富基石-崩盘,辣妈萌宝-创业失败,朋友公司转交他人管理

     2014年9月的时候,写过一篇面试的总结性质的文章,"2014年武汉的IT行情好像不太好". 原文地址:blog.csdn.net/fansunion/article/detai ...

  9. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  10. IDA 宏定义

    /* This file contains definitions used by the Hex-Rays decompiler output. It has type definitions an ...