码迷,mamicode.com
首页 > 其他好文 > 详细

CodeChef - AMLEX-Poetic word

时间:2018-04-23 13:50:46      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:targe   https   tip   ati   while   algorithm   dex   ret   tick   

 

题目链接 

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 a1a2, ..., 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 LOCAL
    freopen("in.txt","r",stdin);
#endif // LOCAL
    int 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;
}

 

CodeChef - AMLEX-Poetic word

标签:targe   https   tip   ati   while   algorithm   dex   ret   tick   

原文地址:https://www.cnblogs.com/fht-litost/p/8918587.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!