CodeChef

阅读: 评论:0

CodeChef

CodeChef

 

题目链接 

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 - 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+5;
const int mod = 77200211+233;
#define lson l,m,2*rt
#define rson m+1,r,2*rt+1
typedef long long ll;int un,vn;
int g[250][250];
int linker[250][250];
bool used[250];
int num[250];
char ans[250];
bool dfs(int u){for(int v=1;v<=vn;v++){if(g[u][v] &&!used[v]){used[v]=true;if(linker[v][0]<num[v]){linker[v][++linker[v][0]]=u;return true;}for(int i=1;i<=num[v];i++){if(dfs(linker[v][i])){linker[v][i]=u;return true;}}}}return false;
}
int hungary(int st){int res=0;for(int i=0;i<=vn;i++) linker[i][0]=0;for(int u=st;u<=un;u++){ms(used,false);if(dfs(u)) res++;}return res;
}bool DFS(int pos){if(pos == un+1) return true;for(int i=1;i<=vn;i++){if(!num[i]) continue;if(!g[pos][i]) continue;ans[pos]='a'+i-1;num[i]--;if(hungary(pos+1) == (un-pos)&&DFS(pos+1)) return true;num[i]++;}return false;
}int main(){
#ifdef LOCALfreopen(&#","r",stdin);
#endif // LOCALint t;scanf("%d",&t);while(t--){vn=26;scanf("%d",&un);for(int i=1;i<=un;i++){for(int j=1;j<=vn;j++){if(i%j==0) g[i][j]=1;else g[i][j]=0;}}for(int i=1;i<=vn;i++) scanf("%d",&num[i]);if(DFS(1)){for(int i=1;i<=un;i++) putchar(ans[i]);puts("");}else puts("#rekt");}return 0;
}

 

转载于:.html

本文发布于:2024-01-31 12:02:01,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170667372428376.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

上一篇:软件构造
标签:CodeChef
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23