【Codevs1322】单词矩阵
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
- W
ABCDEFGHIJKLMNOPQRSUTVWXY - N
20
Sample Output
- 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】单词矩阵的更多相关文章
- codevs1322 单词矩阵
题目描述 Description 对于包含字母A到Y各一次的单词S,将其从上到下从左到右写在一个5*5的矩阵中,如单词ADJPTBEKQUCGLRVFINSWHMOXY写出来如下: A D J P T ...
- P1101 单词方阵(DFS)
题目描述 给一n \times nn×n的字母方阵,内可能蕴含多个"yizhong"单词.单词在方阵中是沿着同一方向连续摆放的.摆放可沿着 88个方向的任一方向,同一单词摆放时不再 ...
- NLP之基于词嵌入(WordVec)的嵌入矩阵生成并可视化
词嵌入 @ 目录 词嵌入 1.理论 1.1 为什么使用词嵌入? 1.2 词嵌入的类比推理 1.3 学习词嵌入 1.4 Word2Vec & Skip-Gram(跳字模型) 1.5 分级& ...
- 第四次作业——WORDSEARCH小游戏
“谁想出来的这么缺德的题目啊!!!!”一个声音在我心中回荡 这个题目很早就在课堂上公布了,我和我的小伙伴都惊呆了! 这是个毛?根本无从下手的感觉 总是觉得这个小游戏不是程序能给出答案的,因为我的第一印 ...
- 【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 ...
- 词向量之Word2vector原理浅析
原文地址:https://www.jianshu.com/p/b2da4d94a122 一.概述 本文主要是从deep learning for nlp课程的讲义中学习.总结google word2v ...
- Semantic Compositionality through Recursive Matrix-Vector Spaces-paper
Semantic Compositionality through Recursive Matrix-Vector Spaces 作者信息:Richard Socher Brody Huval Chr ...
- 机器学习与R语言:NB
#---------------------------------------- # 功能描述:演示NB建模过程 # 数据集:SMS文本信息 # tm包:维也纳财经大学提供 #----------- ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
随机推荐
- JavaScipt30(第八个案例)(主要知识点:canvas)
承接上文,这是第8个案例,要实现的效果是按住鼠标不放,进行拖动时可以在画布上画出不同粗细不同颜色的曲线. 附上项目链接: https://github.com/wesbos/JavaScript30 ...
- 解决vue项目运行过程中,npm run dev 报错问题
[方案1] 错误如下: npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! travel@1.0.0 dev: `webpack-dev-server ...
- ThinkPHP---案例--实现知识管理功能
[一]准备工作 (1)数据表sp_knowledge SQL语句:知识管理数据表结构 create table sp_knowledge( id int(11) not null auto_incre ...
- (独孤九剑)--cURL
[一]概论 日常开发里,cURL使用最多的协议就是HTTP协议的GET.POST请求,其他协议和请求方式用的较少. [二]开启 开发前检验是否开启了cURL模块,开启方法为php.int中打开exte ...
- nginx配置X-Forwarded-For 防止伪造ip
网上常见nginx配置ip请求头 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 风险: 用于可以通过自己设置请求头来伪造ip ...
- HDU - 2041 - 超级楼梯(dp)
题意: 有一楼梯共M级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法? 思路: 如何到第n阶台阶,只能从n-1和n-2台阶上去,那么只需要计算到n-1阶台阶和到n-2阶台 ...
- js中细小点
昨天在写前端代码的时候,遇到一个神奇的问题,我判断序号 num 和数组 arr 的 length 是否相等,如果相等则做想要的处理,伪码如下; if (num === arr.length) { fu ...
- angular环境安装与配置
1.安装npm和nodejs,下载地址:https://nodejs.org/en/download/ node -v npm -v 2.配置淘宝代理,下载node_modules npm con ...
- Spring Tool Suite 安装
第一步:到http://spring.io/tools/sts/all/上下载对应版本.(此处以博主Windows64位系统为例) 第二步: 进入eclipse,依次点击help-->Insta ...
- 神器的方块Magic Squares
题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 我们知道魔板的每一个方格都有一种颜色.这8种颜 ...