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
Sample Input
Sample Output
#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的更多相关文章
- foj 2111 Problem 2111 Min Number
Problem 2111 Min Number Accept: 1025 Submit: 2022Time Limit: 1000 mSec Memory Limit : 32768 KB ...
- fzu 2111 Min Number
http://acm.fzu.edu.cn/problem.php?pid=2111 Problem 2111 Min Number Accept: 572 Submit: 1106Tim ...
- (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 ...
- (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 + ...
- HDU Problem D [ Humble number ]——基础DP丑数序列
Problem D Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submi ...
- FZOJ2111:Min Number
Problem Description Now you are given one non-negative integer n in 10-base notation, it will only c ...
- 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 ...
- LeetCode Problem 9:Palindrome Number回文数
描述:Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
- [LeetCode&Python] Problem 202. Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
随机推荐
- 在Vue将第三方JS库封装为组件使用
第三方JS库地址:https://github.com/inorganik/CountUp.js 使用NPM进行安装: npm install --save countup 根据官方回答,CountU ...
- shell脚本,awk在需要的行上打打印空行。
注解: 判断每行中是否包含字母a,包含了,就将$1的值赋值给变量a,然后判断变量a是否存在,存在打印一个空行,在将变量的值使用空变量b赋值,最后在打印输出. 结果就是在包含有字符a的行上打印一个空行.
- Linux菜鸟起飞之路【五】权限管理(一)
一.与用户相关的几个文件 1./etc/passwd 储存用户名,格式为 用户名:密码(用密码代位符X代替):UID:GID:用户描述信息:家目录:shell 用户名(login_name):是代表用 ...
- centos安装并配置MySQL
一.卸载掉原有mysql [root@xiaoluo ~]# rpm -qa | grep mysql // 这个命令就会查看该操作系统上是否已经安装了mysql数据库 [root@xiaoluo ~ ...
- Python基础学习总结__Day4
一.装饰器 前戏: 1.函数即变量 (1)函数名为‘门牌号’即内存地址,加括号开始调用 (2)没有变量调用将清空内存 (3)匿名函数(lambda函数):没有‘门牌号’,需要赋值给变量,否则将会被清空 ...
- sql执行过长,如何入手优化
一条sql执行过长的时间,你如何优化,从哪些方面 1.查看sql是否涉及多表的联表或者子查询,如果有,看是否能进行业务拆分,相关字段冗余或者合并成临时表(业务和算法的优化)2.涉及链表的查询,是否能进 ...
- python模块之shutil和zipfile
shutil 模块 高级的 文件.文件夹.压缩包 处理模块 shutil.copyfileobj(fsrc, fdst[, length])将文件内容拷贝到另一个文件中 import shutil s ...
- sublime text3 安装ctags实现函数跟踪跳转
来源:http://blog.csdn.net/menglongfc/article/details/51141084 本人试用平台如下:sublime text3,和谐版 在source insig ...
- Virtual Friends HDU - 3172 (并查集+秩+map)
These days, you can do all sorts of things online. For example, you can use various websites to make ...
- Jack Straws POJ - 1127 (几何计算)
Jack Straws Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5428 Accepted: 2461 Descr ...