CodeChef - AMLEX-Poetic word

构建字典序最小字符串
本文介绍了一道算法题的解决思路,通过构建二分图并使用匈牙利算法求解多重匹配问题,最终实现构造满足特定条件的字典序最小字符串。

 

题目链接 

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;
}

 

转载于:https://www.cnblogs.com/fht-litost/p/8918587.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值