动态规划做的,复杂度O(m*n)
#include<iostream>
#include<vector>
using namespace std;
//string s;
void f(int x,int y,int k,vector<vector<int>> a,string s);
int main()
{
int x=10;
int y=12;
vector<vector<int>> a(y+1,vector<int>(x+1,0));
for(int i=0;i<=y;i++)
a[i][0]=1;
for(int i=0;i<=x;i++)
a[0][i]=1;
for(int i=1;i<=y;i++)
for(int j=1;j<=x;j++)
a[i][j]=a[i-1][j]+a[i][j-1];
for(int i=0;i<=y;i++)
{
cout<<i<<"行: ";
for(int j=0;j<=x;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
string s="";
f(x,y,111,a,s);
return 0;
}
void f(int x,int y,int k,vector<vector<int>> a,string s)
{
if(k>a[y][x]) {cout<<"too big K"<<endl;return;}
if(x==0||y==0)
{for(int i=0;i<x;i++)
s.push_back('a');
for(int i=0;i<y;i++)
s.push_back('z');
cout<<s<<endl;
return;
}
int nu=0;
for(int i=0;i<=x;i++)
if(nu+a[y-1][i]>=k)
{for(int j=0;j<x-i;j++)
s.push_back('a');
s.push_back('z');
f(i,y-1,k-nu,a,s);break;
}
else nu+=a[y-1][i];
}
don