【AtCoder】Beginner Contest 380-C.Move Segment

题目链接

Problem Statement

You are given a string S S S of length N N N consisting of 0 and 1.

Move the K K K-th 1-block from the beginning in S S S to immediately after the ( K − 1 ) (K-1) (K−1)-th 1-block, and print the resulting string.

It is guaranteed that S S S contains at least K K K 1-blocks.

Here is a more precise description.

  • Let S l ... r S_{l\ldots r} Sl...r denote the substring of S S S from the l l l-th character through the r r r-th character.

  • We define a substring S l ... r S_{l\ldots r} Sl...r of S S S to be a 1-block if it satisfies all of the following conditions:

    • S l = S l + 1 = ⋯ = S r = S_l = S_{l+1} = \cdots = S_r = Sl=Sl+1=⋯=Sr= 1
    • l = 1 l = 1 l=1 or S l − 1 = S_{l-1} = Sl−1= 0
    • r = N r = N r=N or S r + 1 = S_{r+1} = Sr+1= 0
  • Suppose that all 1-blocks in S S S are S l 1 ... r 1 , ... , S l m ... r m S_{l_1\ldots r_1}, \ldots, S_{l_m\ldots r_m} Sl1...r1,...,Slm...rm, where l 1 ≤ l 2 ≤ . . . ≤ l m l_1 \leq l_2 \leq ... \leq l_m l1≤l2≤...≤lm .

    Then, we define the length N N N string T T T, obtained by moving the K K K-th 1-block to immediately after the ( K − 1 ) (K-1) (K−1)-th 1-block, as follows:

    • T i = S i T_i =S_i Ti=Si for 1 ≤ i ≤ r K − 1 1 \leq i \leq r_{K-1} 1≤i≤rK−1
    • T i = T_i = Ti= 1 for r K − 1 + 1 ≤ i ≤ r K − 1 + ( r K − l K ) + 1 r_{K-1} + 1 \leq i \leq r_{K-1} + (r_K - l_K) + 1 rK−1+1≤i≤rK−1+(rK−lK)+1
    • T i = T_i = Ti= 0 for r K − 1 + ( r K − l K ) + 2 ≤ i ≤ r K r_{K-1} + (r_K - l_K) + 2 \leq i \leq r_K rK−1+(rK−lK)+2≤i≤rK
    • T i = S i T_i =S_i Ti=Si for r K + 1 ≤ i ≤ N r_K + 1 \leq i \leq N rK+1≤i≤N

中文题意

问题陈述

你得到一个长度为 N N N 的字符串 S S S ,由' 0 '和' 1 '组成。

将 K K K 第一个' 1 '块从 S S S 开始移动到紧跟在 ( K − 1 ) (K-1) (K−1) 第一个' 1 '块之后,并打印结果字符串。

可以保证 S S S 至少包含 K K K ' 1 ' -块。

这里有一个更精确的描述。

---让 S l ... r S_{l\ldots r} Sl...r 表示 S S S 的子字符串,从 l l l 到 r r r 。

-我们定义 S S S 的子字符串 S l ... r S_{l\ldots r} Sl...r 为' 1 '块,如果它满足以下所有条件:

  • S l = S l + 1 = ⋯ = S r = S_l = S_{l+1} = \cdots = S_r = Sl=Sl+1=⋯=Sr= ' 1 '
  • l = 1 l = 1 l=1 或 S l − 1 = S_{l-1} = Sl−1= ' 0 '
  • r = N r = N r=N 或 S r + 1 = S_{r+1} = Sr+1= ' 0 '
  • 假设 S S S 中所有的"1"-块都是 S l 1 ... r 1 , ... , S l m ... r m S_{l_1\ldots r_1}, \ldots, S_{l_m\ldots r_m} Sl1...r1,...,Slm...rm ,其中 l 1 ≤ l 2 ≤ . . . ≤ l m l_1 \leq l_2 \leq ... \leq l_m l1≤l2≤...≤lm 。

然后,我们定义长度 N N N 字符串 T T T ,通过将 K K K - ' 1 ' 块移动到紧跟在 ( K − 1 ) (K-1) (K−1) -' 1 ' 块之后得到长度 N N N 字符串 T T T ,如下:

  • T i = S i T_i = S_i Ti=Si 为 1 ≤ i ≤ r K − 1 1 \leq i \leq r_{K-1} 1≤i≤rK−1
  • T i = T_i = Ti= 为 r K − 1 + 1 ≤ i ≤ r K − 1 + ( r K − l K ) + 1 r_{K-1} + 1 \leq i \leq r_{K-1} + (r_K - l_K) + 1 rK−1+1≤i≤rK−1+(rK−lK)+1 的' 1 '
  • T i = T_i = Ti= 为 r K − 1 + ( r K − l K ) + 2 ≤ i ≤ r K r_{K-1} + (r_K - l_K) + 2 \leq i \leq r_K rK−1+(rK−lK)+2≤i≤rK 的' 0 '
  • T i = S i T_i = S_i Ti=Si 替换为 r K + 1 ≤ i ≤ N r_K + 1 \leq i \leq N rK+1≤i≤N

Constraints

  • 1 ≤ N ≤ 5 × 1 0 5 1 \leq N \leq 5 \times 10^5 1≤N≤5×105
  • S S S is a string of length N N N consisting of 0 and 1.
  • 2 ≤ K 2 \leq K 2≤K
  • S S S contains at least K K K 1-blocks.

Input

The input is given from Standard Input in the following format:

N N N K K K
S S S

Output

Print the answer.

Sample Input 1

15 3
010011100011001

Sample Output 1

010011111000001

S S S has four 1-blocks: from the 2nd to the 2nd character, from the 5th to the 7th character, from the 11th to the 12th character, and from the 15th to the 15th character.

Sample Input 2

10 2
1011111111

Sample Output 2

1111111110

解法

解法一:我们将每一个1块使用 c + + c++ c++中的 pair存储起来,first 表示对应的1 块的开始位置,second表示对应的1块的结束位置。之后重组字符串即可。

cpp 复制代码
#include <iostream>
#include <vector>

#define x first
#define y second

using namespace std;
typedef pair<int, int> PII;

int n, k;
string s;

int main() {
    cin >> n >> k;
    cin >> s;
    
    vector<PII> blocks;

    int start = -1;
    for (int i = 0; i <= n; i ++) {
        if(i < n && s[i] == '1') {
            if(start == -1) start = i;
        } else {
            if(start != -1) {
                blocks.emplace_back(start, i - 1);
                start = -1;
            }
        }
    }

    int k_start = blocks[k - 1].x;
    int k_end = blocks[k - 1].y;

    string t = s;
    string b_str = t.substr(k_start, k_end - k_start + 1);
    t.erase(k_start, k_end - k_start + 1);
    t.insert(blocks[k -2].y + 1, b_str);

    cout << t << endl;
    return 0;
}

解法二:

同样的也是用pair来存储每一个块,只不过first存储当前的块是0还是1second存储当前块的长度,使用循环判断当前是第几个1块,然后交换即可。

cpp 复制代码
#include <iostream>
#include <vector>

using namespace std;
typedef pair<int, int> PII;

int n, k;
string s;

int main() {
    cin >> n >> k;
    cin >> s;
    s += '.';
    vector<PII> b;
    int now = s[0], num = 0;
    for (int i = 0; i < s.size(); i ++) {
        if(s[i] == now) num ++;
        else {
            b.push_back({now - '0', num});
            now = s[i], num = 1;    
        } 
    }

    int cnt = 0; // 统计当前是第几个 1 块
    for (int i = 0; i < b.size(); i ++) {
        if(b[i].first == 1) {
            cnt ++;
            if(cnt == k) {
                swap(b[i], b[i - 1]);
            }
        } 
    }

    for (auto a : b) {
        for (int i = 0; i < a.second; i ++) {
            cout << a.first;
        }
    }
    return 0;
}
相关推荐
金士顿1 小时前
MFC 文档模板 每个文档模板需要实例化吧
c++·mfc
ZVAyIVqt0UFji2 小时前
go-zero负载均衡实现原理
运维·开发语言·后端·golang·负载均衡
loop lee2 小时前
Nginx - 负载均衡及其配置(Balance)
java·开发语言·github
SomeB1oody3 小时前
【Rust自学】4.1. 所有权:栈内存 vs. 堆内存
开发语言·后端·rust
toto4123 小时前
线程安全与线程不安全
java·开发语言·安全
水木流年追梦4 小时前
【python因果库实战10】为何需要因果分析
开发语言·python
m0_675988234 小时前
Leetcode2545:根据第 K 场考试的分数排序
python·算法·leetcode
人才程序员4 小时前
QML z轴(z-order)前后层级
c语言·前端·c++·qt·软件工程·用户界面·界面
破-风4 小时前
leetcode---mysql
算法·leetcode·职场和发展
w(゚Д゚)w吓洗宝宝了5 小时前
C vs C++: 一场编程语言的演变与对比
c语言·开发语言·c++