恢复数字序列 od
用例:
432111111111 4 原序列:111 112 113 114
19801211 5 原序列:8 9 10 11 12
91237999988880 7 原序列:87 88 89 90 91 92 93
#include <iostream>
#include <string>
#include "vector"
#include "unordered_map"
using namespace std;bool iseque(unordered_map<int,int>& map_a, unordered_map<int,int>& map_b) {if (map_a.size() != map_b.size()) { //窗口内的map可能和对比的map不一致,因为数字的位数不一致return false;}for (auto i:map_a) {if (map_b.find(i.first) == map_b.end()) {return false;} else if (map_b[i.first] != i.second) {return false;} }return true;
}//整体思路:题目说的有序数组,就是递增序列,第一个想到的就是滑动窗口,为什么是滑动窗口?
//在一个1-1000的数组里面找到一个递增序列,里面每个数字出现的个数和打乱顺序出现的个数一致,这个数组就是打乱顺序之前的状态int main() {string num_str;int n;while (cin >> num_str >> n) {// cout<<num<<endl;// cout<<n<<endl;unordered_map<int,int> map_;for (auto i: num_str) {map_[i - '0']++;}int slow = 1;unordered_map<int,int> map_1;int cur_size = 0;for (int i = 1; i < 1004; i++) {string str = to_string(i);for (int i = 0; i < str.size(); i++) {map_1[str[i] - '0']++;}while (i - slow + 1 == n) {//窗口控制划过的数字个数,也即是题目给的第二个参数if (iseque(map_1, map_)) { //判断哈希表 找到目标窗 打印最小值slowcout<<slow<<endl;int count = slow;// while(count <= i) { //打印还原的数字序列// cout<<count++<<' ';// }// cout<<endl;return 0;}string str_tmp = to_string(slow);//不是目标结果,就开始缩窗for (auto i:str_tmp) {//先缩哈希表 解决当前的slowmap_1[i - '0']--;if (map_1[i-'0'] == 0) {map_1.erase(i - '0');}}slow++;//然后再缩窗 先后顺序一定不要弄错!!!}}}
}
// 64 位输出请用 printf("%lld")