算法 【滑动窗口】BISHI47 交换到最大 思路 求解代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); int t = Integer.parseInt(br.readLine()); while (t-- > 0) { String str = br.readLine(); char[] s = str.toCharArray(); int n = s.length; for (int i = 0; i < n; i++) { int max_val = s[i] - '0'; int max_index = i; // 在窗口内寻找能产生最大价值的来源 for (int j = i; j < Math.min(i + 10, n); j++) { int cur_val = (s[j] - '0') - (j - i); if (cur_val > max_val) { max_val = cur_val; max_index = j; } } // 物理移动来源字符到当前位置i int tmp_index = max_index; while (tmp_index > i) { char tmp_val = s[tmp_index]; s[tmp_index] = s[tmp_index - 1]; s[tmp_index - 1] = tmp_val; tmp_index--; } // 将当前位置i的字符值更新为计算出的最优值 s[i] = (char) (max_val + '0'); } out.println(new String(s)); } out.flush(); out.close(); br.close(); }