题意:

给出两个6行5列的字母矩阵,一个密码满足:密码的第i个字母在两个字母矩阵的第i列均出现。

然后找出字典序为k的密码,如果不存在输出NO

分析:

我们先统计分别在每一列均在两个矩阵出现的字母,然后从小到大排好序。

对于第一个样例来说,我们得到ACDW、BOP、GMOX、AP、GSU

则一共有4×3×4×2×3=288种密码,我们先计算这个数列的后缀积:288、72、24、6、3、1

要确定第一个字母,如果1≤k≤72,则是A;如果73≤k≤144,则是C,以此类推。

确定第二个字母是类似的,用k%72+1与24去比较。

代码实现中,字典序是从0开始的。

 //#define DEBUG
#include <cstdio>
#include <cstring> char G[][][], ans[], select[][];
int vis[][], hehe[], cnt[]; int main()
{
//freopen("in.txt", "r", stdin); int T;
scanf("%d", &T);
hehe[] = ;
while(T--)
{
memset(cnt, , sizeof(cnt));
int k;
scanf("%d", &k);
k--; //字典序标号从0开始
for(int i = ; i < ; ++i)
for(int j = ; j < ; ++j)
scanf("%s", G[i][j]); for(int i = ; i < ; ++i)
{
memset(vis, , sizeof(vis));
for(int j = ; j < ; ++j)
for(int k = ; k < ; ++k)
vis[j][G[j][k][i]-'A'] = ;
for(int j = ; j < ; ++j)
if(vis[][j] && vis[][j])
select[i][cnt[i]++] = 'A' + j;
} #ifdef DEBUG
for(int i = ; i < ; ++i)
{
for(int j = ; j < select[i].size(); ++j)
printf("%c", select[i][j]);
puts("");
}
#endif // DEBUG for(int i = ; i >= ; --i)
hehe[i] = cnt[i] * hehe[i+];
if(k >= hehe[])
{
puts("NO");
continue;
}
for(int i = ; i < ; ++i)
{
int m = k / hehe[i+];
ans[i] = select[i][m];
k = k % hehe[i+];
}
ans[] = '\0'; printf("%s\n", ans);
} return ;
}

代码君一

解法二:

因为密码最多有65 = 7776种,所以可以按字典序从小到大枚举。

思维难度小,写起来更快更对。

 #include <cstdio>
#include <cstring> int k, cnt;
char G[][][], ans[]; bool dfs(int col)
{
if(col == )
{
if(++cnt == k)
{
ans[col] = '\0';
printf("%s\n", ans);
return true;
}
return false;
} bool vis[][];
memset(vis, false, sizeof(vis));
for(int i = ; i < ; ++i)
for(int j = ; j < ; ++j)
vis[i][G[i][j][col] - 'A'] = ;
for(int i = ; i < ; ++i)
if(vis[][i] && vis[][i])
{
ans[col] = i + 'A';
if(dfs(col+)) return true;
}
return false;
} int main()
{
//freopen("in.txt", "r", stdin);
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d", &k);
for(int i = ; i < ; ++i)
for(int j = ; j < ; ++j)
scanf("%s", G[i][j]); cnt = ;
if(!dfs()) puts("NO");
} return ;
}

代码君二

UVa 1262 (第k字典序) Password的更多相关文章

  1. 【暑假】[数学]UVa 1262 Password

    UVa 1262  Password 题目: Password   Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld ...

  2. UVA - 1262 数学

    UVA - 1262 题意: 有两个6*5 的大写字母组成的矩阵,需要找出满足条件的字典序第k小的密码:密码中每个字母在两个矩阵的对应的同一列中都出现过 代码: // 先处理出来每一列可以取的字母,例 ...

  3. UVA 1262 Password 暴力枚举

    Password Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID:  ...

  4. UVa 1262 - Password(解码)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  5. UVA 1262 Password

    https://vjudge.net/problem/UVA-1262 字典序第k小 注意两点: 1. k-- 2.去重 #include<cstring> #include<cst ...

  6. UVA - 1262 Password(密码)(暴力枚举)

    题意:给两个6行5列的字母矩阵,找出满足如下条件的“密码”:密码中的每个字母在两个矩阵的对应列中均出现.给定k(1<=k<=7777),你的任务是找出字典序第k小的密码.如果不存在,输出N ...

  7. UVA1262Password(第K字典序)

    题目链接 紫书P323 题意:两个6*5的字母矩阵,两个矩阵每列相同的字母,每列取一个,求按照字典序第k小的序列 分析: 对于第一个样例来说,我们得到{ACDW}.{BOP}.{GMOX}.{AP}. ...

  8. HDOJ-6628(dfs+第k字典序最小差异序列)

    permutation 1 HDOJ-6628 这题使用的暴力深搜,在dfs里面直接从最小的差异开始枚举 注意这里的pre记录前一个数,并且最后答案需要减去排列中最小的数再加一 这里有一个技巧关于求第 ...

  9. 【每日一题】 UVA - 1599 Ideal Path 字典序最短路

    题解:给一个1e5个点2e5条边,每个边有一个值,让你输出一条从1到n边的路径使得:条数最短的前提下字典序最小. 题解:bfs一次找最短路(因为权值都是1,不用dijkstra),再bfs一次存一下路 ...

随机推荐

  1. ADT 怎么删除logcat过滤规则

    刚才新增了一个过滤规则,但是没有找到在哪里删除,也看不到全部的log输出, 解决方案如下:

  2. How to use Android Activity's finish(), onDestory() and System.exit(0) methods

    Activity.finish() Calling this method will let the system know that the programmer wants the current ...

  3. CSS属性--过渡(transtion)

    首先介绍一下transition的属性取值: transition-property : 主要设置对象中的参与过渡的属性,包括(border-color,background-color,color) ...

  4. uva 12086 树状数组

    树状数组 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> #i ...

  5. 转一篇:文档笔记之Django QuerySet

    这个放着,说不定以后作一些更深入的查询时,用得着的. http://www.rapospectre.com/blog/7/ 今天刚刚答完辩体完检就跑来更新博客了!!! 先补上第一篇: 一般情况下,我们 ...

  6. python自定义函数在Python解释器中调用

    https://docs.python.org/2.7/tutorial/modules.html Modules If you quit from the Python interpreter an ...

  7. lintcode 中等题 :Maximum Product Subarray 最大连续乘积子序列

    题目 乘积最大子序列 找出一个序列中乘积最大的连续子序列(至少包含一个数). 样例 比如, 序列 [2,3,-2,4] 中乘积最大的子序列为 [2,3] ,其乘积为6. 解题  法一:直接暴力求解 时 ...

  8. sparse coding稀疏表达入门

    最近在看sparse and redundant representations这本书,进度比较慢,不过力争看过的都懂,不把时间浪费掉.才看完了不到3页吧,书上基本给出了稀疏表达的概念以及传统的求法. ...

  9. 阿里巴巴fastJson

    FastJson解析 一.阿里巴巴FastJson是一个Json处理工具包,包括“序列化”和“反序列化”两部分,它具备如下特征:速度最快,测试表明,fastjson具有极快的性能,超越任其他的Java ...

  10. C语言中时间调用处理的相关函数介绍

    asctime(将时间和日期以字符串格式表示) 相关函数:time,ctime,gmtime,localtime 表头文件:#include<time.h> 定义函数:char * asc ...