[BFS] Beautiful Now HDU - 6351(未完)

http://acm.hdu.edu.cn/showproblem.php?pid=6351
输入 t k
对于一个数据最多交换K次 找最大最小值
5
12 1
213 2
998244353 1
998244353 2
998244353 3
12 21
123 321
298944353 998544323
238944359 998544332
233944859 998544332
注意0的存在;
思路 找最小的最大的 交换 对重复的 都压入队列 BFS找最小的
(贪心的话 就不太可能在多个最大最小中找到该换的 所以考虑BFS把所有的都试一试搞定)
DFS之后补下
不过BFS相当快

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <list>
#include <bitset>
using namespace std;
typedef long long ll;

int n,k,m,t;
string s,s1,s2;

struct node{
    string tmp;
    int st;
    int sp;
};

string bfs1(){
    queue<node> q;
    q.push(node{s,0,0});
    string res="999999999999";
    while(!q.empty()){
        node p=q.front();q.pop();
        res=min(res,p.tmp);
        if(p.sp==k) continue;
        if(p.st==s.size()-1) continue;
        string tmp=p.tmp;
        int pos=p.st;
        for(int i=p.st+1;i<tmp.size();i++){ 
            if(p.st==0&&tmp[i]=='0') continue;//去0
            if(tmp[i]<tmp[pos]){//huan xiao de
                pos=i;
            }   
        }
        if(pos==p.st){
            q.push(node{p.tmp,p.st+1,p.sp});
        }
        else{
             for(int i=p.st;i<s.size();i++){
                if(tmp[i]==tmp[pos]){
                    swap(tmp[i],tmp[p.st]);
                    q.push(node{tmp,p.st+1,p.sp+1});
                    swap(tmp[i],tmp[p.st]);
                }
            }
        }
    }
    return res;
}

string bfs2(){
    queue<node> q;
    q.push(node{s,0,0});
    string res="0";
    while(!q.empty()){
        node p=q.front();q.pop();
    //  cout<<p.tmp<<endl;
        res=max(res,p.tmp);
        if(p.sp==k) continue;
        if(p.st==s.size()-1) continue;
    //  if(res>=p.tmp) continue;
        string tmp=p.tmp;
        int pos=p.st;
        for(int i=p.st+1;i<tmp.size();i++){

            if(tmp[i]>tmp[pos]){//huan da de
                pos=i;
            }   
        }
        if(pos==p.st){
            q.push(node{p.tmp,p.st+1,p.sp});
        }
        else{
             for(int i=p.st;i<s.size();i++){

                if(tmp[i]==tmp[pos]){
                    swap(tmp[i],tmp[p.st]);
                    q.push(node{tmp,p.st+1,p.sp+1});
                    swap(tmp[i],tmp[p.st]);
                }

            }
        }
    }
    return res;
}

int main(){
    ios::sync_with_stdio(false);
    cin>>t;
    while(t--){
        cin>>s>>k;
        k=min(k,(int)s.size()-1);
        s1=bfs1();//da
        s2=bfs2();//xiao
        cout<<s1<<" "<<s2<<endl; 
    }
    return 0; 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值