我用的是bfs。1046ms,这。。都倒数了。我偷懒用了set。而且c++的string里面居然没有逆置函数,我勒个去,还得自己写,最新的标准才支持从整型转成字符串(to_string)。。这。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <queue>
#include <ctime>
#include <set>
#define ll long long
#define MK make_pair
#define PB push_back
#define SZ(x) ((int)(x).size())
#define FOR(it,c) for ( __typeof((c).begin()) it=(c).begin(); it!=(c).end(); it++ )
using namespace std;
set<string> str;
int n,p,a[1010],cnt;
struct Node{
int last,start;
string str;
}no;
void bfs(){
queue<Node> q;
no.str="";no.last=0;no.start=0;
q.push(no);
while(!q.empty()){
no=q.front();q.pop();
for(int i=no.start;i<n;i++)
if(no.start==0||no.last<=a[i]){
Node nn;
nn.str=no.str;
if(no.start) nn.str+=' ';
int k=a[i];
string tstr="";
while(k){
tstr+=(k%10+'0');
k/=10;
}
for(int j=SZ(tstr)-1;j>=0;j--)
nn.str+=tstr[j];
nn.last=a[i];
nn.start=i+1;
if(str.count(nn.str)) continue;
str.insert(nn.str);
cnt++;
cout<<nn.str<<endl;
if(cnt>=p) return;
q.push(nn);
}
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
#endif
while(cin>>n>>p){
for(int i=0;i<n;i++) cin>>a[i];
cnt=0;
str.clear();
bfs();
puts("");
}
return 0;
}