2025.1.5——1200

2025.1.5——1200


Q1. 1200

Kevin discovered a binary string s that starts with 1 in the river at Moonlit River Park and handed it over to you. Your task is to select two non-empty substrings of s (which can be overlapped) to maximize the XOR value of these two substrings.

The XOR of two binary strings a and b is defined as the result of the operation applied to the two numbers obtained by interpreting a and b as binary numbers, with the leftmost bit representing the highest value. Here, denotes the bitwise XOR operation.

The strings you choose may have leading zeros.

A string a is a substring of a string b if a can be obtained from b by the deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.


------------------------独自思考分割线------------------------

  • 2025年第一题...


A1.

  1. 1点就是需要发现两个字符串的长度
  2. 另一点就是考虑最大值要从高位考虑

------------------------代码分割线------------------------

A1.

#include <bits/stdc++.h>
#define int long long //
#define endl '\n'     // 交互/调试 关
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
void _();
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(6);
    int T = 1;
    cin >> T;
    while (T--)
        _();
    return 0;
}

void _()
{
    string s;
    cin >> s;
    int n = s.size();
    s = ' ' + s;
    int has = 1;
    int l = 1, r = n, sub_l = 1, sub_r = 1;
    string t;
    for (int i = 1; i <= n; i++)
        if (s[i] - s[1])
            has = 0;
    if (has)
    {
        cout << l << ' ' << r << ' ' << sub_l << ' ' << sub_r << endl;
        return;
    }

    int f = 0;

    for (int i = 2; i <= n; i++)
        if (!f)
        {
            if (s[i] == '0')
                t += '1', f = 1;
        }
        else
        {
            t += s[i] == '1' ? '0' : '1';
        }
    int m = t.size();
    int res = 0, real_l = 1;
    sub_r = sub_l + m - 1;
    for (; sub_r <= n; sub_l++, sub_r++)
    {
        int ans = 0;
        for (int i = sub_l, j = i - sub_l; i <= sub_r; i++, j++)
            if (s[i] == t[j])
                ans++;
            else
                break;
        if (ans > res)
        {
            res = ans;
            real_l = sub_l;
        }
    }
    sub_l = real_l;
    sub_r = sub_l + m - 1;
    cout << l << ' ' << r << ' ' << sub_l << ' ' << sub_r << endl;
}

posted @   Jkke  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
点击右上角即可分享
微信分享提示