HDU 2610 Sequence one

46 篇文章 0 订阅

我用的是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;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值