Lightoj 1023 - Discovering Permutations
Time Limit: 0.5 second(s) | Memory Limit: 32 MB |
In this problem you have to find the permutations using the first N English capital letters. Since there can be many permutations, you have to print the first K permutations.
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case contains two integers N, K (1 ≤ N ≤ 26, 1 ≤ K ≤ 30).
Output
For each case, print the case number in a line. Then print the first K permutations that contain the first N English capital letters in alphabetical order. If there are less than K permutations then print all of them.
Sample Input |
Output for Sample Input |
2 3 8 10 10 |
Case 1: ABC ACB BAC BCA CAB CBA Case 2: ABCDEFGHIJ ABCDEFGHJI ABCDEFGIHJ ABCDEFGIJH ABCDEFGJHI ABCDEFGJIH ABCDEFHGIJ ABCDEFHGJI ABCDEFHIGJ ABCDEFHIJG |
水dfs
/* ***********************************************
Author :guanjun
Created Time :2016/6/28 14:50:20
File Name :1023.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
int x,y;
};
struct cmp{
bool operator()(Node a,Node b){
if(a.x==b.x) return a.y> b.y;
return a.x>b.x;
}
}; bool cmp(int a,int b){
return a>b;
}
char s[];
int n,k,num,mark;
int vis[];
void dfs(string s,int cnt){
if(cnt==n){
num++;
cout<<s<<endl;
if(num==k)mark=;
return ;
}
if(mark)return ;
for(int i=;i<n;i++){
if(!vis[i]){
vis[i]=;
dfs(s+char(i+'A'),cnt+);
vis[i]=;
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int T;
cin>>T;
for(int t=;t<=T;t++){
printf("Case %d:\n",t);
cin>>n>>k;
cle(vis);
num=mark=;
dfs("",);
}
return ;
}
Lightoj 1023 - Discovering Permutations的更多相关文章
- LightOJ 1023 Discovering Permutations 水题
http://www.lightoj.com/volume_showproblem.php?problem=1023 题意:26字母全排列 思路:用next_permutation或者思维想一下都可以 ...
- LightOJ 1030 Discovering Gold(期望)
Description You are in a cave, a long cave! The cave can be represented by a 1 x N grid. Each cell o ...
- LightOj 1030 - Discovering Gold(dp+数学期望)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1030 题意:在一个1*n 的格子里,每个格子都有相应的金币数,走到相应格子的话,就会得 ...
- LightOJ - 1030 Discovering Gold —— 期望
题目链接:https://vjudge.net/problem/LightOJ-1030 1030 - Discovering Gold PDF (English) Statistics For ...
- LightOJ 1030 Discovering Gold (概率/期望DP)
题目链接:LightOJ - 1030 Description You are in a cave, a long cave! The cave can be represented by a \(1 ...
- LightOJ 1030 Discovering Gold(期望 概率)
正推,到达i的概率为p[i],要注意除了1和n外,到达i的概率并不一定为1 概率表达式为p[i] += p[j] / min(n - j, 6) 从j带过来的期望为exp[i] += exp[j] / ...
- lightoj 1023
题意:让你输出前N个大写字母的前K个排列,按字典序,很水,直接dfs. #include<cstdio> #include<string> #include<cstrin ...
- LightOJ 1030 Discovering Gold
期望,$dp$. 设$ans[i]$为$i$为起点,到终点$n$获得的期望金币值.$ans[i]=(ans[i+1]+ans[i+2]+ans[i+3]+ans[i+4]+ans[i+5]+ans[i ...
- LightOJ 1030 Discovering Gold 数学期望计算
题目大意:给出长度为n的一条隧道,每个位置都有一定数量的财宝.给你一枚骰子,roll到几点就前进几步,如果即将到达的地方超过了这条隧道长度,就重新roll一次,走到n点结束.求这个过程能收获多少财宝. ...
随机推荐
- POJ 3621 Sightseeing Cows (最优比率环 01分数划分)
题意: 给定L个点, P条边的有向图, 每个点有一个价值, 但只在第一经过获得, 每条边有一个花费, 每次经过都要付出这个花费, 在图中找出一个环, 使得价值之和/花费之和 最大 分析: 这道题其实并 ...
- 关于一个多个.cpp文件的项目中,函数出现未定义引用错误
编译的话,必须把全部的c文件都要编译的啊,只编译一个c算怎么个逻辑呢?编译实际上是2个过程,编译和链接.编译过程只检查所有的符号(变量,函数)有没有声明,即只需要h文件生命就够了.但是链接时候,需要找 ...
- ubuntu安装远程桌面连接工具
1. 安装xrdp sudo apt-get -y install xrdp 2.安装vnc4server sudo apt-get install vnc4server 3.安装xubunt ...
- LeetCode:不同路径&不同路径II
不同路径一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“Finish”). 问 ...
- Vijos1655 - 萌萌的糖果博弈
Portal Description 两人轮流操作两堆初始数量分别为\(a,b(a,b\leq2^{127})\)的石子.每人每次进行如下操作: 取走一堆石子,并将另一堆分成两个非零堆. 如果另一堆只 ...
- bzoj 3786 星系探索 dfs+splay
[BZOJ3786]星系探索 Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球 ...
- 静态工具类中使用注解注入service实例
一般需要在一个工具类中使用@Autowired 注解注入一个service.但是由于工具类方法一般都写成static,所以直接注入就存在问题. 使用如下方式可以解决: /** * */ package ...
- 没有上司的舞会(hdu 1520)
题目描述 Description Ural大学有N个职员,编号为1~N.他们有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.每个职员有一个快乐指数.现在有个周年庆宴会 ...
- CF778A:String Game
给出字符串s和t,以及s的长度n的一个全排列,求按照这个排列依次删除s的字符,删到何时s中不含子序列t. 解法一: t中的每个字符的位置在s中跳啊跳,合法的情况下t中的字符在s中的位置应该是单调递增的 ...
- MongoDB基本管理命令操作
1. 查看所有数据库: show dbs 或: show databases 注意: 该命令不会显示新创建的空数据库,若想显示需要向空数据库插入一些数据. MongoDB中默认的数据库为test,若果 ...