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