首先可以转变思路,将A队每个人【也就是返回值里每个string】看成一次匹配,匹配总共进行 n 次,匹配的左部是
如果 n≤m 的话很简单,直接匹配 n 次就行了,因为左部和右部元素个数是相等的,都是
#include<algorithm>
#include<string>
#include<vector>
#include<iostream>
using namespace std;
const int maxn=55;
class TheContest
{int map[maxn][maxn],f[maxn],vis[maxn],cnt[maxn],ans[maxn],n,m,t,l;
private:char toc(int x){if (x<=9) return x+'0';if (x<=35) return x-10+'A';return x-36+'a';}int dfs1(int u){if (vis[u]) return 0;vis[u]=1;for (int v=1;v<=t;v++)if (map[u][v]&&(!f[v]||dfs1(f[v]))){f[v]=u;return 1;}return 0;}int dfs2(int u,int from){if (vis[u]) return 0;vis[u]=1;for (int v=1;v<=t;v++)if (map[u][v]&&(!f[v]||f[v]==from||(f[v]>from&&dfs2(f[v],from)))){f[v]=u;return 1;}return 0;}
public:vector<string> getSchedule(int N,int M){vector<string> ret;ret.clear();string s;n=N;m=M;t=max(n,m);for (int i=1;i<=m;i++)for (int j=1;j<=t;j++)map[i][j]=1;for (int K=1;K<=n;K++){for (int i=1;i<=t;i++) f[i]=0;for (int i=1;i<=t;i++)if (cnt[i]+n-K+1==m)for (int j=m+1;j<=t;j++) map[j][i]=0;elsefor (int j=m+1;j<=t;j++) map[j][i]=1;for (int i=1;i<=t;i++){for (int j=1;j<=t;j++) vis[j]=0;dfs1(i);}for (int i=1;i<=t;i++){for (int j=1;j<=t;j++) vis[j]=0;dfs2(i,i);}s="";for (int i=1;i<=t;i++)if (f[i]<=m){map[f[i]][i]=0;cnt[i]++;ans[f[i]]=i;}for (int i=1;i<=m;i++) s+=toc(ans[i]);ret.push_back(s);}return ret;}
};