Problem 2111 Min Number

Accept: 925    Submit: 1838
Time Limit: 1000 mSec    Memory Limit : 32768
KB

Problem Description

Now you are given one non-negative integer n in 10-base notation, it will only contain digits ('0'-'9'). You are allowed to choose 2 integers i and j, such that: i!=j, 1≤i<j≤|n|, here |n| means the length of n’s 10-base notation. Then we can swap n[i] and n[j].

For example, n=9012, we choose i=1, j=3, then we swap n[1] and n[3], then we get 1092, which is smaller than the original n.

Now you are allowed to operate at most M times, so what is the smallest number you can get after the operation(s)?

Please note that in this problem, leading zero is not allowed!

Input

The first line of the input contains an integer T (T≤100), indicating the number of test cases.

Then T cases, for any case, only 2 integers n and M (0≤n<10^1000, 0≤M≤100) in a single line.

Output

For each test case, output the minimum number we can get after no more than M operations.

Sample Input

3 9012 0 9012 1 9012 2

Sample Output

9012 1092 1029 
 
题意:给定一个数字,这个数字的任意两个数码可以进行调换,作为一次操作,但不管怎么换,不能出现前导0。现在能进行M次操作,问M次操作以内,任意的交换,找到该数字的最小值。
思路:广度优先搜索,从初始的值开始搜索,一层一层的搜,搜到M层为止,找到最小值。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
map<string, int>m;
string s;
int M; void swap(string &s,int i,int j) {
char tmp = s[i];
s[i] = s[j];
s[j] = tmp;
} string bfs(string &s) {
m.insert(make_pair(s,));
queue<string>que;
que.push(s);
string MIN_num = s;
while (!que.empty()) {
string str = que.front();
if (m[str] == M)break;
que.pop();
for (int i = ; i < s.size();i++) {
for (int j = i + ; j < s.size(); j++) {
string tmp = str;
swap(tmp, i, j);
map<string,int>::iterator it = m.find(tmp);
if (it == m.end()&&tmp[]!='') {
que.push(tmp);
m.insert(make_pair(tmp,m[str]+));
if (MIN_num > tmp) {
MIN_num = tmp;
}
}
}
}
}
return MIN_num;
} int main() {
int T;
scanf("%d",&T);
while (T--) {
cin >> s;
scanf("%d",&M);
m.clear();
cout << bfs(s) << endl;;
}
return ;
}

Problem 2111 Min Number的更多相关文章

  1. foj 2111 Problem 2111 Min Number

    Problem 2111 Min Number Accept: 1025    Submit: 2022Time Limit: 1000 mSec    Memory Limit : 32768 KB ...

  2. fzu 2111 Min Number

      http://acm.fzu.edu.cn/problem.php?pid=2111  Problem 2111 Min Number Accept: 572    Submit: 1106Tim ...

  3. (Problem 28)Number spiral diagonals

    Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is forme ...

  4. (Problem 17)Number letter counts

    If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + ...

  5. HDU Problem D [ Humble number ]——基础DP丑数序列

    Problem D Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submi ...

  6. FZOJ2111:Min Number

    Problem Description Now you are given one non-negative integer n in 10-base notation, it will only c ...

  7. Codeforces Round #427 (Div. 2) Problem B The number on the board (Codeforces 835B) - 贪心

    Some natural number was written on the board. Its sum of digits was not less than k. But you were di ...

  8. LeetCode Problem 9:Palindrome Number回文数

    描述:Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...

  9. [LeetCode&Python] Problem 202. Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

随机推荐

  1. laydate时间控件绑定回调事件

    onclick="laydate({istime: true, format: 'YYYY-MM-DD',choose:checkDate});" //回调函数内容 functio ...

  2. linux - mysql 安装教程

    环境介绍>>>>>>>>>>>>>>>>>> 操作系统:Centos 7 mysql数据库版 ...

  3. string 空值

    string str; string mystr = ""; 则 str == mystr;

  4. SCOPE_IDENTITY和@@IDENTITY[转]

    本文转自:http://www.cnblogs.com/daydayupanan/archive/2008/09/04/1283648.html SCOPE_IDENTITY和@@IDENTITY的作 ...

  5. Bootstrap标签页(Tab)插件

    标签页(Tab)在Bootstrap导航元素一章中简介过,通过结合一些data属性,您可以轻松地创建一些标签页界面.通过这个插件您可以把内容放置在标签页或胶囊式标签页甚至是下拉菜单标签页中. 用法 您 ...

  6. Nginx: ubuntu系统上如何判断是否安装了Nginx?

    问题描述:ubuntu系统上,如何查看是否安装了Nginx? 解决方法:输入命令行:ps -ef | grep nginx master process后面就是Nginx的安装目录. 延伸:1. 如何 ...

  7. 接口的定义——默认加public abstract默认全局常量;与继承不同,子类可以同时实现多个接口;抽象类实现接口;接口继承接口

    一. 接口的定义 接口中定义的方法,全部都为抽象方法,默认加public abstract 接口中定义的变量,全部为全局常量,默认加public static final 二.与继承不同,子类可以同时 ...

  8. Repeat a string repeat a string-freecodecamp算法题目

    Repeat a string repeat a string(重复输出字符串) 要求 重复一个指定的字符串 num次 如果num是一个负数则返回一个空字符串. 思路 将给定的字符串赋给定义的变量te ...

  9. c++ 函数指针应用,定义一个方法,传入两个参数和一个函数指针,并返回结果

    #include <iostream> #include <string> using namespace std; double add(double x, double y ...

  10. 学习路由器vue-router

    vue-router:vue官方路由管理器. 功能:嵌套的路由/视图表模块化的.基于组件的路由配置路由参数.查询.通配符基于 Vue.js 过渡系统的视图过渡效果细粒度的导航控制带有自动激活的 CSS ...