zoukankan      html  css  js  c++  java
  • 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 - 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;
    }
  • 相关阅读:
    转载:人家编写的程序:「雀神 AI」Suphx
    一千六百万单表建联合索引对查询效率的提升
    索引对单列极值查询的显著性影响(百万级别表单列最值查询 Cost由1405变成3)
    经典SQL问题:Top 10%
    区间查询与等效minus查询
    『科学计算』L0、L1与L2范数_理解
    『Python』__getattr__()特殊方法
    『Json』常用方法记录
    『Pickle』数据结构持久化模块_常用方法记录
    『Re』知识工程作业_主体识别
  • 原文地址:https://www.cnblogs.com/fht-litost/p/8918587.html
Copyright © 2011-2022 走看看