题目链接 

Dhinwaji is an acclaimed poet and likes to play with words and letters. He has bought some stickers where each sticker has a lower case english letter (a-z). The letters are indexed from 1 - 26 i.e. a has index 1, b has index 2 and so on. He has ai stickers with letter i on it. He needs to create a new word having exactly n letters. Being a programmer, Dhinwaji imposed another constraint: a letter with index j can only be placed at position i in the word if i % j = 0 i.e. i should be a multiple of j. Note that everything is 1-indexed. Note also that not all the stickers need to be used.

Dhinwaji wonders what is the lexicographically smallest word he can create that follows the above constraints. Since he is too busy writing poems, you have to help him find this word. Note that it is possible that there is no valid word of n letters that can be formed.

Input

  • The first line will have a number T indicating the number of test cases. T lines follow.
  • The first line of each test case will have one integer, n, denoting the required length of the new word.
  • The second line of each test case will have 26 space separated integers a1, a2, ..., a26

Output

  • For each test case, print one line containing the lexicographically smallest word which satisfies the conditions. If no such word is possible, print "#rekt" without the quotes.

Constraints

  • 1 ≤ T ≤ 5
  • 1 ≤ n ≤ 200
  • 0 ≤ ai ≤ n

Example

Input:
3
3
1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
6
3 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
10
1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Output:
abc
aacbab
#rekt

Explanation

Testcase 1: There is 1 sticker with a, b and c each. "abc" is the only valid word possible

Testcase 2: Some valid words are "abcaab", "abcbaa", "ababac" etc. The lexicographically smallest among them is aacbab

Testcase 3: There are a total of 3 letters so a word with 10 letters cannot be formed

题意

给出26个字母的数量,让你构成一个字典序最小的字符串,满足串的位置i与字母编号j的关系为i%j==0。

分析

重点是建模成二分图多重匹配。然后按字典序一个个放,每放一个字母都检查其随后的能不能完全匹配,若不能则回溯。很暴力。。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include <queue>
#include <vector>
#include<bitset>
#include<map>
#include<deque>
#include<stack>
using namespace std;
typedef pair<int,int> pii;
#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define ms(a,b) memset(a,b,sizeof(a))
const int inf = 0x3f3f3f3f;
const int maxn = 1e5+;
const int mod = +;
#define lson l,m,2*rt
#define rson m+1,r,2*rt+1
typedef long long ll; int un,vn;
int g[][];
int linker[][];
bool used[];
int num[];
char ans[];
bool dfs(int u){
for(int v=;v<=vn;v++){
if(g[u][v] &&!used[v]){
used[v]=true;
if(linker[v][]<num[v]){
linker[v][++linker[v][]]=u;
return true;
}
for(int i=;i<=num[v];i++){
if(dfs(linker[v][i])){
linker[v][i]=u;
return true;
}
}
}
}
return false;
}
int hungary(int st){
int res=;
for(int i=;i<=vn;i++) linker[i][]=;
for(int u=st;u<=un;u++){
ms(used,false);
if(dfs(u)) res++;
}
return res;
} bool DFS(int pos){
if(pos == un+) return true;
for(int i=;i<=vn;i++){
if(!num[i]) continue;
if(!g[pos][i]) continue;
ans[pos]='a'+i-;
num[i]--;
if(hungary(pos+) == (un-pos)&&DFS(pos+)) return true;
num[i]++;
}
return false;
} int main(){
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
int t;
scanf("%d",&t);
while(t--){
vn=;
scanf("%d",&un);
for(int i=;i<=un;i++){
for(int j=;j<=vn;j++){
if(i%j==) g[i][j]=;
else g[i][j]=;
}
}
for(int i=;i<=vn;i++) scanf("%d",&num[i]);
if(DFS()){
for(int i=;i<=un;i++) putchar(ans[i]);
puts("");
}else puts("#rekt");
}
return ;
}

CodeChef - AMLEX-Poetic word的更多相关文章

  1. CodeChef A String Game(SG)

    A String Game   Problem code: ASTRGAME   Submit All Submissions   All submissions for this problem a ...

  2. Word/Excel 在线预览

    前言 近日项目中做到一个功能,需要上传附件后能够在线预览.之前也没做过这类似的,于是乎就查找了相关资料,.net实现Office文件预览大概有这几种方式: ① 使用Microsoft的Office组件 ...

  3. C#中5步完成word文档打印的方法

    在日常工作中,我们可能常常需要打印各种文件资料,比如word文档.对于编程员,应用程序中文档的打印是一项非常重要的功能,也一直是一个非常复杂的工作.特别是提到Web打印,这的确会很棘手.一般如果要想选 ...

  4. C# 给word文档添加水印

    和PDF一样,在word中,水印也分为图片水印和文本水印,给文档添加图片水印可以使文档变得更为美观,更具有吸引力.文本水印则可以保护文档,提醒别人该文档是受版权保护的,不能随意抄袭.前面我分享了如何给 ...

  5. 获取打开的Word文档

    using Word = Microsoft.Office.Interop.Word; int _getApplicationErrorCount=0; bool _isMsOffice = true ...

  6. How to accept Track changes in Microsoft Word 2010?

    "Track changes" is wonderful and remarkable tool of Microsoft Word 2010. The feature allow ...

  7. C#将Word转换成PDF方法总结(基于Office和WPS两种方案)

    有时候,我们需要在线上预览word文档,当然我们可以用NPOI抽出Word中的文字和表格,然后显示到网页上面,但是这样会丢失掉Word中原有的格式和图片.一个比较好的办法就是将word转换成pdf,然 ...

  8. 开源Word读写组件DocX 的深入研究和问题总结

    一. 前言 前两天看到了asxinyu大神的[原创]开源Word读写组件DocX介绍与入门,正好我也有类似的自动生成word文档得需求,于是便仔细的研究了这个DocX. 我也把它融入到我的项目当中并进 ...

  9. [.NET] 开头不讲"Hello Word",读尽诗书也枉然 : Word 操作组件介绍 - Spire.Doc

    开头不讲"Hello Word",读尽诗书也枉然 : Word 操作组件介绍 - Spire.Doc [博主]反骨仔 [原文地址]http://www.cnblogs.com/li ...

随机推荐

  1. Installing and removing VNC Connect | Red Hat | VNC Connect

    https://www.realvnc.com/en/connect/docs/redhat-install-remove.html 此软件会和TigerVNC(Server)或者X11VNC Ser ...

  2. activiti 工作流 动态 设置 指定 节点任务人、责任人、组 的实现方式

    首先给大家看一下我的流程图: 流程文件leaveBill.bpmn <?xml version="1.0" encoding="UTF-8"?>&l ...

  3. Windows平台下面Oracle11.2.0.1 升级Oracle11.2.0.4 的简单步骤

    1. 首先查看数据库的版本: 2. ESXi 上面的虚拟机挂在 oracle11.2.0.4的 iso磁盘 3. 执行set 进行升级 4. 安装选项进行选择 升级现有的数据库 5. 注意安装位置必须 ...

  4. DVWA的安装与简单使用

    参考资料: http://www.freebuf.com/articles/web/119150.html 尝试使用linux机器安装,但是因为下载php版本以及各种兼容性的问题耗时较长, 所以后来选 ...

  5. [转帖]GitHub上整理的一些工具

    GitHub上整理的一些工具   技术站点 Hacker News:非常棒的针对编程的链接聚合网站 Programming reddit:同上 MSDN:微软相关的官方技术集中地,主要是文档类 inf ...

  6. Node http和express和mysql

    const http = require("http");const express = require("express");const mysql = re ...

  7. 将ubuntu14.04 从mysql从5.5删除之后安装5.7遇到的一些问题(本篇不讨论热升级)

    五一放假实在无聊 继续玩弄新的服务器.发现有台mysql版本实在有点老,估计是akiho直接使用 apt-get install mysql-server ,然后又没有更新到最新的源,然后无脑安装了5 ...

  8. SVN上线步骤笔记

    项目代码位置: /data/svn/play_out 项目代码目录名称: test SVN创建位置:/data/svn/repos_Websvn线上地址:svn://192.168.1.1/repos ...

  9. Mysql 悲观锁

    转载:http://chenzhou123520.iteye.com/blog/1860954 悲观锁介绍: 悲观锁,正如其名,它指的是对数据被外界(包括本系统当前的其他事务,以及来自外部系统的事务处 ...

  10. JavaScript——DOM树的增查改删总结

    对HTML DOM的操作是前端JavaScript编程时必备的技能,本文是我自己对DOM树操作的总结,主要是方法的罗列,原理性的讲述较少,适合大家用于理清思路或是温习 一.什么是HTML DOM? 是 ...